From 9b98d8b2e1e970a8ae23851c8c5f3155f6ff4e88 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Thu, 28 Jul 2016 02:29:14 -0400 Subject: update stream management --- .../Playback/Progressive/BaseProgressiveStreamingService.cs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'MediaBrowser.Api/Playback/Progressive/BaseProgressiveStreamingService.cs') diff --git a/MediaBrowser.Api/Playback/Progressive/BaseProgressiveStreamingService.cs b/MediaBrowser.Api/Playback/Progressive/BaseProgressiveStreamingService.cs index d75b8947a8..449100fc42 100644 --- a/MediaBrowser.Api/Playback/Progressive/BaseProgressiveStreamingService.cs +++ b/MediaBrowser.Api/Playback/Progressive/BaseProgressiveStreamingService.cs @@ -175,7 +175,9 @@ namespace MediaBrowser.Api.Playback.Progressive ResponseHeaders = responseHeaders, ContentType = contentType, IsHeadRequest = isHeadRequest, - Path = outputPath + Path = outputPath, + FileShare = FileShare.ReadWrite + }).ConfigureAwait(false); } finally @@ -187,8 +189,7 @@ namespace MediaBrowser.Api.Playback.Progressive // Need to start ffmpeg try { - return await GetStreamResult(state, responseHeaders, isHeadRequest, cancellationTokenSource) - .ConfigureAwait(false); + return await GetStreamResult(state, responseHeaders, isHeadRequest, cancellationTokenSource).ConfigureAwait(false); } catch { -- cgit v1.2.3 From 24003580e7977b5336661921f55a919a62a3194f Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Sat, 6 Aug 2016 10:08:38 -0400 Subject: improve stopping of progressive streams --- MediaBrowser.Api/ApiEntryPoint.cs | 2 +- .../Progressive/BaseProgressiveStreamingService.cs | 20 +++++++++-- .../Progressive/ProgressiveStreamWriter.cs | 42 +++++++++++++--------- 3 files changed, 44 insertions(+), 20 deletions(-) (limited to 'MediaBrowser.Api/Playback/Progressive/BaseProgressiveStreamingService.cs') diff --git a/MediaBrowser.Api/ApiEntryPoint.cs b/MediaBrowser.Api/ApiEntryPoint.cs index dbc1179e22..1a7f4a2b12 100644 --- a/MediaBrowser.Api/ApiEntryPoint.cs +++ b/MediaBrowser.Api/ApiEntryPoint.cs @@ -349,7 +349,7 @@ namespace MediaBrowser.Api return; } - 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.cs index 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.Progressive var 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.Progressive ContentType = contentType, IsHeadRequest = isHeadRequest, Path = state.MediaPath + }).ConfigureAwait(false); } } @@ -170,13 +172,25 @@ namespace MediaBrowser.Api.Playback.Progressive try { + 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.Progressive outputHeaders[item.Key] = item.Value; } - Func streamWriter = stream => new ProgressiveFileCopier(FileSystem, job, Logger).StreamFile(outputPath, stream, CancellationToken.None); + Func 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.cs index 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.Progressive public 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 CopyToAsyncInternal(Stream source, Stream destination, Int32 bufferSize, CancellationToken cancellationToken) -- cgit v1.2.3 From 433254c498d2e43acfd34e5c4fcee2fdcc2e767b Mon Sep 17 00:00:00 2001 From: softworkz Date: Fri, 5 Aug 2016 06:08:11 +0200 Subject: Async stream handling: Use interface instead of Func No functional changes --- .../Progressive/BaseProgressiveStreamingService.cs | 4 +- .../Progressive/ProgressiveStreamWriter.cs | 23 +++- .../MediaBrowser.Controller.csproj | 1 + MediaBrowser.Controller/Net/IAsyncStreamSource.cs | 18 +++ MediaBrowser.Controller/Net/IHttpResultFactory.cs | 2 +- .../HttpServer/AsyncStreamWriter.cs | 59 ++++++++ .../HttpServer/AsyncStreamWriterEx.cs | 153 +++++++++++++++++++++ .../HttpServer/AsyncStreamWriterFunc.cs | 56 -------- .../HttpServer/HttpResultFactory.cs | 9 +- .../MediaBrowser.Server.Implementations.csproj | 3 +- 10 files changed, 263 insertions(+), 65 deletions(-) create mode 100644 MediaBrowser.Controller/Net/IAsyncStreamSource.cs create mode 100644 MediaBrowser.Server.Implementations/HttpServer/AsyncStreamWriter.cs create mode 100644 MediaBrowser.Server.Implementations/HttpServer/AsyncStreamWriterEx.cs delete mode 100644 MediaBrowser.Server.Implementations/HttpServer/AsyncStreamWriterFunc.cs (limited to 'MediaBrowser.Api/Playback/Progressive/BaseProgressiveStreamingService.cs') diff --git a/MediaBrowser.Api/Playback/Progressive/BaseProgressiveStreamingService.cs b/MediaBrowser.Api/Playback/Progressive/BaseProgressiveStreamingService.cs index 4649499c46..5a5cb80000 100644 --- a/MediaBrowser.Api/Playback/Progressive/BaseProgressiveStreamingService.cs +++ b/MediaBrowser.Api/Playback/Progressive/BaseProgressiveStreamingService.cs @@ -362,9 +362,9 @@ namespace MediaBrowser.Api.Playback.Progressive outputHeaders[item.Key] = item.Value; } - Func streamWriter = stream => new ProgressiveFileCopier(FileSystem, job, Logger).StreamFile(outputPath, stream, CancellationToken.None); + var streamSource = new ProgressiveFileCopier(FileSystem, outputPath, outputHeaders, job, Logger, CancellationToken.None); - return ResultFactory.GetAsyncStreamWriter(streamWriter, outputHeaders); + return ResultFactory.GetAsyncStreamWriter(streamSource); } finally { diff --git a/MediaBrowser.Api/Playback/Progressive/ProgressiveStreamWriter.cs b/MediaBrowser.Api/Playback/Progressive/ProgressiveStreamWriter.cs index 63d71b85ef..8c4e23a397 100644 --- a/MediaBrowser.Api/Playback/Progressive/ProgressiveStreamWriter.cs +++ b/MediaBrowser.Api/Playback/Progressive/ProgressiveStreamWriter.cs @@ -4,28 +4,45 @@ using System.IO; using System.Threading; using System.Threading.Tasks; using CommonIO; +using MediaBrowser.Controller.Net; +using System.Collections.Generic; +using ServiceStack.Web; namespace MediaBrowser.Api.Playback.Progressive { - public class ProgressiveFileCopier + public class ProgressiveFileCopier : IAsyncStreamSource, IHasOptions { private readonly IFileSystem _fileSystem; private readonly TranscodingJob _job; private readonly ILogger _logger; + private readonly string _path; + private readonly CancellationToken _cancellationToken; + private readonly Dictionary _outputHeaders; // 256k private const int BufferSize = 81920; private long _bytesWritten = 0; - public ProgressiveFileCopier(IFileSystem fileSystem, TranscodingJob job, ILogger logger) + public ProgressiveFileCopier(IFileSystem fileSystem, string path, Dictionary outputHeaders, TranscodingJob job, ILogger logger, CancellationToken cancellationToken) { _fileSystem = fileSystem; + _path = path; + _outputHeaders = outputHeaders; _job = job; _logger = logger; + _cancellationToken = cancellationToken; } - public async Task StreamFile(string path, Stream outputStream, CancellationToken cancellationToken) + public IDictionary Options + { + get + { + return _outputHeaders; + } + } + + public async Task WriteToAsync(Stream outputStream) { try { diff --git a/MediaBrowser.Controller/MediaBrowser.Controller.csproj b/MediaBrowser.Controller/MediaBrowser.Controller.csproj index 0462117cb5..e7eaa1dc0b 100644 --- a/MediaBrowser.Controller/MediaBrowser.Controller.csproj +++ b/MediaBrowser.Controller/MediaBrowser.Controller.csproj @@ -236,6 +236,7 @@ + diff --git a/MediaBrowser.Controller/Net/IAsyncStreamSource.cs b/MediaBrowser.Controller/Net/IAsyncStreamSource.cs new file mode 100644 index 0000000000..0f41f60a55 --- /dev/null +++ b/MediaBrowser.Controller/Net/IAsyncStreamSource.cs @@ -0,0 +1,18 @@ +using ServiceStack.Web; +using System.IO; +using System.Threading.Tasks; + +namespace MediaBrowser.Controller.Net +{ + /// + /// Interface IAsyncStreamSource + /// Enables asynchronous writing to http resonse streams + /// + public interface IAsyncStreamSource + { + /// + /// Asynchronously write to the response stream. + /// + Task WriteToAsync(Stream responseStream); + } +} diff --git a/MediaBrowser.Controller/Net/IHttpResultFactory.cs b/MediaBrowser.Controller/Net/IHttpResultFactory.cs index 49d4614d81..8fdb1ce37e 100644 --- a/MediaBrowser.Controller/Net/IHttpResultFactory.cs +++ b/MediaBrowser.Controller/Net/IHttpResultFactory.cs @@ -28,7 +28,7 @@ namespace MediaBrowser.Controller.Net /// System.Object. object GetResult(object content, string contentType, IDictionary responseHeaders = null); - object GetAsyncStreamWriter(Func streamWriter, IDictionary responseHeaders = null); + object GetAsyncStreamWriter(IAsyncStreamSource streamSource); /// /// Gets the optimized result. diff --git a/MediaBrowser.Server.Implementations/HttpServer/AsyncStreamWriter.cs b/MediaBrowser.Server.Implementations/HttpServer/AsyncStreamWriter.cs new file mode 100644 index 0000000000..e44b0c6af1 --- /dev/null +++ b/MediaBrowser.Server.Implementations/HttpServer/AsyncStreamWriter.cs @@ -0,0 +1,59 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Threading.Tasks; +using ServiceStack; +using ServiceStack.Web; +using MediaBrowser.Controller.Net; + +namespace MediaBrowser.Server.Implementations.HttpServer +{ + public class AsyncStreamWriter : IStreamWriter, IAsyncStreamWriter, IHasOptions + { + /// + /// Gets or sets the source stream. + /// + /// The source stream. + private IAsyncStreamSource _source; + + public Action OnComplete { get; set; } + public Action OnError { get; set; } + + /// + /// Initializes a new instance of the class. + /// + public AsyncStreamWriter(IAsyncStreamSource source) + { + _source = source; + } + + public IDictionary Options + { + get + { + var hasOptions = _source as IHasOptions; + if (hasOptions != null) + { + return hasOptions.Options; + } + + return new Dictionary(StringComparer.OrdinalIgnoreCase); + } + } + + /// + /// Writes to. + /// + /// The response stream. + public void WriteTo(Stream responseStream) + { + var task = _source.WriteToAsync(responseStream); + Task.WaitAll(task); + } + + public async Task WriteToAsync(Stream responseStream) + { + await _source.WriteToAsync(responseStream).ConfigureAwait(false); + } + } +} diff --git a/MediaBrowser.Server.Implementations/HttpServer/AsyncStreamWriterEx.cs b/MediaBrowser.Server.Implementations/HttpServer/AsyncStreamWriterEx.cs new file mode 100644 index 0000000000..b98addb317 --- /dev/null +++ b/MediaBrowser.Server.Implementations/HttpServer/AsyncStreamWriterEx.cs @@ -0,0 +1,153 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Threading.Tasks; +using ServiceStack; +using ServiceStack.Web; +using MediaBrowser.Controller.Net; + +namespace MediaBrowser.Server.Implementations.HttpServer +{ + public class AsyncStreamWriterEx : AsyncStreamWriter, IHttpResult + { + /// + /// Gets or sets the source stream. + /// + /// The source stream. + private IAsyncStreamSource _source; + + /// + /// Initializes a new instance of the class. + /// + public AsyncStreamWriterEx(IAsyncStreamSource source) : base(source) + { + _source = source; + } + + public string ContentType + { + get + { + throw new NotImplementedException(); + } + set + { + throw new NotImplementedException(); + } + } + + public List Cookies + { + get { throw new NotImplementedException(); } + } + + public Dictionary Headers + { + get { throw new NotImplementedException(); } + } + + public int PaddingLength + { + get + { + return Result.PaddingLength; + } + set + { + Result.PaddingLength = value; + } + } + + public IRequest RequestContext + { + get + { + return Result.RequestContext; + } + set + { + Result.RequestContext = value; + } + } + + public object Response + { + get + { + return Result.Response; + } + set + { + Result.Response = value; + } + } + + public IContentTypeWriter ResponseFilter + { + get + { + return Result.ResponseFilter; + } + set + { + Result.ResponseFilter = value; + } + } + + public Func ResultScope + { + get + { + return Result.ResultScope; + } + set + { + Result.ResultScope = value; + } + } + + public int Status + { + get + { + return Result.Status; + } + set + { + Result.Status = value; + } + } + + public System.Net.HttpStatusCode StatusCode + { + get + { + return Result.StatusCode; + } + set + { + Result.StatusCode = value; + } + } + + public string StatusDescription + { + get + { + return Result.StatusDescription; + } + set + { + Result.StatusDescription = value; + } + } + + private IHttpResult Result + { + get + { + return _source as IHttpResult; + } + } + } +} diff --git a/MediaBrowser.Server.Implementations/HttpServer/AsyncStreamWriterFunc.cs b/MediaBrowser.Server.Implementations/HttpServer/AsyncStreamWriterFunc.cs deleted file mode 100644 index 5aa01c7062..0000000000 --- a/MediaBrowser.Server.Implementations/HttpServer/AsyncStreamWriterFunc.cs +++ /dev/null @@ -1,56 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Threading.Tasks; -using ServiceStack; -using ServiceStack.Web; - -namespace MediaBrowser.Server.Implementations.HttpServer -{ - public class AsyncStreamWriterFunc : IStreamWriter, IAsyncStreamWriter, IHasOptions - { - /// - /// Gets or sets the source stream. - /// - /// The source stream. - private Func Writer { get; set; } - - /// - /// Gets the options. - /// - /// The options. - public IDictionary Options { get; private set; } - - public Action OnComplete { get; set; } - public Action OnError { get; set; } - - /// - /// Initializes a new instance of the class. - /// - public AsyncStreamWriterFunc(Func writer, IDictionary headers) - { - Writer = writer; - - if (headers == null) - { - headers = new Dictionary(StringComparer.OrdinalIgnoreCase); - } - Options = headers; - } - - /// - /// Writes to. - /// - /// The response stream. - public void WriteTo(Stream responseStream) - { - var task = Writer(responseStream); - Task.WaitAll(task); - } - - public async Task WriteToAsync(Stream responseStream) - { - await Writer(responseStream).ConfigureAwait(false); - } - } -} diff --git a/MediaBrowser.Server.Implementations/HttpServer/HttpResultFactory.cs b/MediaBrowser.Server.Implementations/HttpServer/HttpResultFactory.cs index c0a2a5eb35..f234674d82 100644 --- a/MediaBrowser.Server.Implementations/HttpServer/HttpResultFactory.cs +++ b/MediaBrowser.Server.Implementations/HttpServer/HttpResultFactory.cs @@ -704,9 +704,14 @@ namespace MediaBrowser.Server.Implementations.HttpServer throw error; } - public object GetAsyncStreamWriter(Func streamWriter, IDictionary responseHeaders = null) + public object GetAsyncStreamWriter(IAsyncStreamSource streamSource) { - return new AsyncStreamWriterFunc(streamWriter, responseHeaders); + if (streamSource as IHttpResult != null) + { + return new AsyncStreamWriterEx(streamSource); + } + + return new AsyncStreamWriter(streamSource); } } } \ No newline at end of file diff --git a/MediaBrowser.Server.Implementations/MediaBrowser.Server.Implementations.csproj b/MediaBrowser.Server.Implementations/MediaBrowser.Server.Implementations.csproj index dca7531934..8025e3594e 100644 --- a/MediaBrowser.Server.Implementations/MediaBrowser.Server.Implementations.csproj +++ b/MediaBrowser.Server.Implementations/MediaBrowser.Server.Implementations.csproj @@ -156,7 +156,8 @@ - + + -- cgit v1.2.3 From e52759786ff626d930ff3aa7375546d8efb5b975 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Tue, 9 Aug 2016 01:10:17 -0400 Subject: fix layout quirks --- MediaBrowser.Api/ApiEntryPoint.cs | 11 +++- .../Progressive/BaseProgressiveStreamingService.cs | 68 +++++++++++----------- 2 files changed, 44 insertions(+), 35 deletions(-) (limited to 'MediaBrowser.Api/Playback/Progressive/BaseProgressiveStreamingService.cs') diff --git a/MediaBrowser.Api/ApiEntryPoint.cs b/MediaBrowser.Api/ApiEntryPoint.cs index 1a7f4a2b12..bb9d2b8645 100644 --- a/MediaBrowser.Api/ApiEntryPoint.cs +++ b/MediaBrowser.Api/ApiEntryPoint.cs @@ -63,6 +63,15 @@ namespace MediaBrowser.Api Instance = this; _sessionManager.PlaybackProgress += _sessionManager_PlaybackProgress; + _sessionManager.PlaybackStart += _sessionManager_PlaybackStart; + } + + private void _sessionManager_PlaybackStart(object sender, PlaybackProgressEventArgs e) + { + if (!string.IsNullOrWhiteSpace(e.PlaySessionId)) + { + PingTranscodingJob(e.PlaySessionId, e.IsPaused); + } } void _sessionManager_PlaybackProgress(object sender, PlaybackProgressEventArgs e) @@ -401,7 +410,7 @@ namespace MediaBrowser.Api } } - Logger.Debug("Transcoding kill timer stopped for JobId {0} PlaySessionId {1}. Killing transcoding", job.Id, job.PlaySessionId); + Logger.Info("Transcoding kill timer stopped for JobId {0} PlaySessionId {1}. Killing transcoding", job.Id, job.PlaySessionId); KillTranscodingJob(job, true, path => true); } diff --git a/MediaBrowser.Api/Playback/Progressive/BaseProgressiveStreamingService.cs b/MediaBrowser.Api/Playback/Progressive/BaseProgressiveStreamingService.cs index 4649499c46..d6dea0fe52 100644 --- a/MediaBrowser.Api/Playback/Progressive/BaseProgressiveStreamingService.cs +++ b/MediaBrowser.Api/Playback/Progressive/BaseProgressiveStreamingService.cs @@ -165,40 +165,40 @@ namespace MediaBrowser.Api.Playback.Progressive } } - // Not static but transcode cache file exists - if (isTranscodeCached) - { - var contentType = state.GetMimeType(outputPath); - - try - { - 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, - OnComplete = () => - { - if (transcodingJob != null) - { - ApiEntryPoint.Instance.OnTranscodeEndRequest(transcodingJob); - } - } - - }).ConfigureAwait(false); - } - finally - { - state.Dispose(); - } - } + //// Not static but transcode cache file exists + //if (isTranscodeCached && state.VideoRequest == null) + //{ + // var contentType = state.GetMimeType(outputPath); + + // try + // { + // 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, + // OnComplete = () => + // { + // if (transcodingJob != null) + // { + // ApiEntryPoint.Instance.OnTranscodeEndRequest(transcodingJob); + // } + // } + + // }).ConfigureAwait(false); + // } + // finally + // { + // state.Dispose(); + // } + //} // Need to start ffmpeg try -- cgit v1.2.3 From 93346830eb28c430477c6cdf6285bcbbea1f2f2a Mon Sep 17 00:00:00 2001 From: Softworkz Date: Sat, 13 Aug 2016 04:24:21 +0200 Subject: Fix incorrect calculation of content length --- .../Playback/Progressive/BaseProgressiveStreamingService.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'MediaBrowser.Api/Playback/Progressive/BaseProgressiveStreamingService.cs') diff --git a/MediaBrowser.Api/Playback/Progressive/BaseProgressiveStreamingService.cs b/MediaBrowser.Api/Playback/Progressive/BaseProgressiveStreamingService.cs index d6dea0fe52..f4cc9f4bc6 100644 --- a/MediaBrowser.Api/Playback/Progressive/BaseProgressiveStreamingService.cs +++ b/MediaBrowser.Api/Playback/Progressive/BaseProgressiveStreamingService.cs @@ -383,7 +383,7 @@ namespace MediaBrowser.Api.Playback.Progressive if (totalBitrate > 0 && state.RunTimeTicks.HasValue) { - return Convert.ToInt64(totalBitrate * TimeSpan.FromTicks(state.RunTimeTicks.Value).TotalSeconds); + return Convert.ToInt64(totalBitrate * TimeSpan.FromTicks(state.RunTimeTicks.Value).TotalSeconds / 8); } return null; -- cgit v1.2.3 From 6164049919f85980f79c0c7d75acc56d7f508796 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Sat, 13 Aug 2016 15:52:58 -0400 Subject: update direct stream url for improved caching --- MediaBrowser.Api/Images/ImageService.cs | 4 +--- MediaBrowser.Api/Playback/BaseStreamingService.cs | 4 ++++ .../Playback/Progressive/BaseProgressiveStreamingService.cs | 10 +++++++++- MediaBrowser.Api/Playback/StreamRequest.cs | 1 + MediaBrowser.Controller/Entities/Audio/Audio.cs | 7 +++++++ MediaBrowser.Controller/Entities/Video.cs | 6 ++++++ MediaBrowser.Model/Dlna/StreamInfo.cs | 2 ++ MediaBrowser.Model/Dto/MediaSourceInfo.cs | 1 + 8 files changed, 31 insertions(+), 4 deletions(-) (limited to 'MediaBrowser.Api/Playback/Progressive/BaseProgressiveStreamingService.cs') diff --git a/MediaBrowser.Api/Images/ImageService.cs b/MediaBrowser.Api/Images/ImageService.cs index 5866ad15bc..3280358dfa 100644 --- a/MediaBrowser.Api/Images/ImageService.cs +++ b/MediaBrowser.Api/Images/ImageService.cs @@ -573,11 +573,9 @@ namespace MediaBrowser.Api.Images var outputFormats = GetOutputFormats(request, imageInfo, cropwhitespace, supportedImageEnhancers); - var cacheGuid = new Guid(_imageProcessor.GetImageCacheTag(item, imageInfo, supportedImageEnhancers)); - TimeSpan? cacheDuration = null; - if (!string.IsNullOrEmpty(request.Tag) && cacheGuid == new Guid(request.Tag)) + if (!string.IsNullOrEmpty(request.Tag)) { cacheDuration = TimeSpan.FromDays(365); } diff --git a/MediaBrowser.Api/Playback/BaseStreamingService.cs b/MediaBrowser.Api/Playback/BaseStreamingService.cs index a9489cecce..ac967e1949 100644 --- a/MediaBrowser.Api/Playback/BaseStreamingService.cs +++ b/MediaBrowser.Api/Playback/BaseStreamingService.cs @@ -1589,6 +1589,10 @@ namespace MediaBrowser.Api.Playback videoRequest.EnableSubtitlesInManifest = string.Equals("true", val, StringComparison.OrdinalIgnoreCase); } } + else if (i == 29) + { + request.Tag = val; + } } } diff --git a/MediaBrowser.Api/Playback/Progressive/BaseProgressiveStreamingService.cs b/MediaBrowser.Api/Playback/Progressive/BaseProgressiveStreamingService.cs index f4cc9f4bc6..56842e69dd 100644 --- a/MediaBrowser.Api/Playback/Progressive/BaseProgressiveStreamingService.cs +++ b/MediaBrowser.Api/Playback/Progressive/BaseProgressiveStreamingService.cs @@ -154,12 +154,20 @@ namespace MediaBrowser.Api.Playback.Progressive using (state) { + TimeSpan? cacheDuration = null; + + if (!string.IsNullOrEmpty(request.Tag)) + { + cacheDuration = TimeSpan.FromDays(365); + } + return await ResultFactory.GetStaticFileResult(Request, new StaticFileResultOptions { ResponseHeaders = responseHeaders, ContentType = contentType, IsHeadRequest = isHeadRequest, - Path = state.MediaPath + Path = state.MediaPath, + CacheDuration = cacheDuration }).ConfigureAwait(false); } diff --git a/MediaBrowser.Api/Playback/StreamRequest.cs b/MediaBrowser.Api/Playback/StreamRequest.cs index a8ca6aaa3b..e1a577f525 100644 --- a/MediaBrowser.Api/Playback/StreamRequest.cs +++ b/MediaBrowser.Api/Playback/StreamRequest.cs @@ -74,6 +74,7 @@ namespace MediaBrowser.Api.Playback public string Params { get; set; } public string PlaySessionId { get; set; } public string LiveStreamId { get; set; } + public string Tag { get; set; } } public class VideoStreamRequest : StreamRequest diff --git a/MediaBrowser.Controller/Entities/Audio/Audio.cs b/MediaBrowser.Controller/Entities/Audio/Audio.cs index 6326bbd4f4..1af55a389f 100644 --- a/MediaBrowser.Controller/Entities/Audio/Audio.cs +++ b/MediaBrowser.Controller/Entities/Audio/Audio.cs @@ -5,9 +5,11 @@ using MediaBrowser.Model.Entities; using MediaBrowser.Model.MediaInfo; using System; using System.Collections.Generic; +using System.Globalization; using System.Linq; using System.Runtime.Serialization; using System.Threading; +using MediaBrowser.Common.Extensions; using MediaBrowser.Controller.Channels; namespace MediaBrowser.Controller.Entities.Audio @@ -266,6 +268,11 @@ namespace MediaBrowser.Controller.Entities.Audio Size = i.Size }; + if (info.Protocol == MediaProtocol.File) + { + info.ETag = i.DateModified.Ticks.ToString(CultureInfo.InvariantCulture).GetMD5().ToString("N"); + } + if (string.IsNullOrEmpty(info.Container)) { if (!string.IsNullOrWhiteSpace(i.Path) && locationType != LocationType.Remote && locationType != LocationType.Virtual) diff --git a/MediaBrowser.Controller/Entities/Video.cs b/MediaBrowser.Controller/Entities/Video.cs index 830747d3c9..8809f155c3 100644 --- a/MediaBrowser.Controller/Entities/Video.cs +++ b/MediaBrowser.Controller/Entities/Video.cs @@ -12,6 +12,7 @@ using System.Runtime.Serialization; using System.Threading; using System.Threading.Tasks; using CommonIO; +using MediaBrowser.Common.Extensions; using MediaBrowser.Controller.Channels; namespace MediaBrowser.Controller.Entities @@ -611,6 +612,11 @@ namespace MediaBrowser.Controller.Entities SupportsDirectStream = i.VideoType == VideoType.VideoFile }; + if (info.Protocol == MediaProtocol.File) + { + info.ETag = i.DateModified.Ticks.ToString(CultureInfo.InvariantCulture).GetMD5().ToString("N"); + } + if (i.IsShortcut) { info.Path = i.ShortcutPath; diff --git a/MediaBrowser.Model/Dlna/StreamInfo.cs b/MediaBrowser.Model/Dlna/StreamInfo.cs index f95c6a0707..02239aa484 100644 --- a/MediaBrowser.Model/Dlna/StreamInfo.cs +++ b/MediaBrowser.Model/Dlna/StreamInfo.cs @@ -252,6 +252,8 @@ namespace MediaBrowser.Model.Dlna list.Add(new NameValuePair("TranscodingMaxAudioChannels", item.TranscodingMaxAudioChannels.HasValue ? StringHelper.ToStringCultureInvariant(item.TranscodingMaxAudioChannels.Value) : string.Empty)); list.Add(new NameValuePair("EnableSubtitlesInManifest", item.EnableSubtitlesInManifest.ToString().ToLower())); + list.Add(new NameValuePair("Tag", item.MediaSource.ETag ?? string.Empty)); + return list; } diff --git a/MediaBrowser.Model/Dto/MediaSourceInfo.cs b/MediaBrowser.Model/Dto/MediaSourceInfo.cs index 4e3e600635..bb07d9cb6f 100644 --- a/MediaBrowser.Model/Dto/MediaSourceInfo.cs +++ b/MediaBrowser.Model/Dto/MediaSourceInfo.cs @@ -20,6 +20,7 @@ namespace MediaBrowser.Model.Dto public string Name { get; set; } + public string ETag { get; set; } public long? RunTimeTicks { get; set; } public bool ReadAtNativeFramerate { get; set; } public bool SupportsTranscoding { get; set; } -- cgit v1.2.3