From be1ce0f80275a1718dc89dd65e2919e9eab8eb7b Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Thu, 13 Feb 2014 11:38:43 -0500 Subject: [PATCH] convert static remote streaming to use internal interfaces --- MediaBrowser.Api/MediaBrowser.Api.csproj | 4 -- .../Playback/BaseStreamingService.cs | 21 +++++++ .../Playback/Progressive/AudioService.cs | 7 ++- .../BaseProgressiveStreamingService.cs | 59 ++++++++----------- .../Playback/Progressive/VideoService.cs | 5 +- .../Playback/StaticRemoteStreamWriter.cs | 26 +++----- .../HttpClientManager/HttpClientManager.cs | 20 +++++++ MediaBrowser.Common/Net/HttpRequestOptions.cs | 3 + .../ApplicationHost.cs | 49 +++++++-------- 9 files changed, 109 insertions(+), 85 deletions(-) diff --git a/MediaBrowser.Api/MediaBrowser.Api.csproj b/MediaBrowser.Api/MediaBrowser.Api.csproj index e105b41ce5..e62ed3e25b 100644 --- a/MediaBrowser.Api/MediaBrowser.Api.csproj +++ b/MediaBrowser.Api/MediaBrowser.Api.csproj @@ -54,10 +54,6 @@ - - - ..\packages\morelinq.1.0.16006\lib\net35\MoreLinq.dll - ..\ThirdParty\ServiceStack\ServiceStack.Interfaces.dll diff --git a/MediaBrowser.Api/Playback/BaseStreamingService.cs b/MediaBrowser.Api/Playback/BaseStreamingService.cs index fa49698781..5705d4cd89 100644 --- a/MediaBrowser.Api/Playback/BaseStreamingService.cs +++ b/MediaBrowser.Api/Playback/BaseStreamingService.cs @@ -1222,6 +1222,27 @@ namespace MediaBrowser.Api.Playback { request.AudioChannels = int.Parse(val, UsCulture); } + else if (i == 8) + { + if (videoRequest != null) + { + request.StartTimeTicks = long.Parse(val, UsCulture); + } + } + else if (i == 9) + { + if (videoRequest != null) + { + videoRequest.Profile = val; + } + } + else if (i == 10) + { + if (videoRequest != null) + { + videoRequest.Level = val; + } + } } } diff --git a/MediaBrowser.Api/Playback/Progressive/AudioService.cs b/MediaBrowser.Api/Playback/Progressive/AudioService.cs index 526f81df72..5ce2eeece8 100644 --- a/MediaBrowser.Api/Playback/Progressive/AudioService.cs +++ b/MediaBrowser.Api/Playback/Progressive/AudioService.cs @@ -1,4 +1,5 @@ using MediaBrowser.Common.IO; +using MediaBrowser.Common.Net; using MediaBrowser.Controller.Configuration; using MediaBrowser.Controller.Drawing; using MediaBrowser.Controller.Dto; @@ -42,8 +43,8 @@ namespace MediaBrowser.Api.Playback.Progressive /// public class AudioService : BaseProgressiveStreamingService { - public AudioService(IServerConfigurationManager serverConfig, IUserManager userManager, ILibraryManager libraryManager, IIsoManager isoManager, IMediaEncoder mediaEncoder, IDtoService dtoService, IFileSystem fileSystem, IItemRepository itemRepository, ILiveTvManager liveTvManager, IImageProcessor imageProcessor) - : base(serverConfig, userManager, libraryManager, isoManager, mediaEncoder, dtoService, fileSystem, itemRepository, liveTvManager, imageProcessor) + public AudioService(IServerConfigurationManager serverConfig, IUserManager userManager, ILibraryManager libraryManager, IIsoManager isoManager, IMediaEncoder mediaEncoder, IDtoService dtoService, IFileSystem fileSystem, IItemRepository itemRepository, ILiveTvManager liveTvManager, IImageProcessor imageProcessor, IHttpClient httpClient) + : base(serverConfig, userManager, libraryManager, isoManager, mediaEncoder, dtoService, fileSystem, itemRepository, liveTvManager, imageProcessor, httpClient) { } @@ -94,7 +95,7 @@ namespace MediaBrowser.Api.Playback.Progressive { audioTranscodeParams.Add("-ac " + channels.Value); } - + if (request.AudioSampleRate.HasValue) { audioTranscodeParams.Add("-ar " + request.AudioSampleRate.Value); diff --git a/MediaBrowser.Api/Playback/Progressive/BaseProgressiveStreamingService.cs b/MediaBrowser.Api/Playback/Progressive/BaseProgressiveStreamingService.cs index 708e85d4d5..2837d748e8 100644 --- a/MediaBrowser.Api/Playback/Progressive/BaseProgressiveStreamingService.cs +++ b/MediaBrowser.Api/Playback/Progressive/BaseProgressiveStreamingService.cs @@ -1,5 +1,4 @@ -using System; -using MediaBrowser.Common.IO; +using MediaBrowser.Common.IO; using MediaBrowser.Common.Net; using MediaBrowser.Controller.Configuration; using MediaBrowser.Controller.Drawing; @@ -10,9 +9,9 @@ using MediaBrowser.Controller.MediaInfo; using MediaBrowser.Controller.Persistence; using MediaBrowser.Model.Dto; using MediaBrowser.Model.IO; +using System; using System.Collections.Generic; using System.IO; -using System.Net.Http; using System.Threading; using System.Threading.Tasks; @@ -24,11 +23,13 @@ namespace MediaBrowser.Api.Playback.Progressive public abstract class BaseProgressiveStreamingService : BaseStreamingService { protected readonly IImageProcessor ImageProcessor; + protected readonly IHttpClient HttpClient; - protected BaseProgressiveStreamingService(IServerConfigurationManager serverConfig, IUserManager userManager, ILibraryManager libraryManager, IIsoManager isoManager, IMediaEncoder mediaEncoder, IDtoService dtoService, IFileSystem fileSystem, IItemRepository itemRepository, ILiveTvManager liveTvManager, IImageProcessor imageProcessor) + protected BaseProgressiveStreamingService(IServerConfigurationManager serverConfig, IUserManager userManager, ILibraryManager libraryManager, IIsoManager isoManager, IMediaEncoder mediaEncoder, IDtoService dtoService, IFileSystem fileSystem, IItemRepository itemRepository, ILiveTvManager liveTvManager, IImageProcessor imageProcessor, IHttpClient httpClient) : base(serverConfig, userManager, libraryManager, isoManager, mediaEncoder, dtoService, fileSystem, itemRepository, liveTvManager) { ImageProcessor = imageProcessor; + HttpClient = httpClient; } /// @@ -157,7 +158,7 @@ namespace MediaBrowser.Api.Playback.Progressive // // ?? // contentFeatures = "DLNA.ORG_PN=WMVHIGH_BASE"; //} - + if (!string.IsNullOrEmpty(contentFeatures)) { @@ -194,7 +195,7 @@ namespace MediaBrowser.Api.Playback.Progressive if (request.Static && state.IsRemote) { AddDlnaHeaders(state, responseHeaders, true); - + return GetStaticRemoteStreamResult(state.MediaPath, responseHeaders, isHeadRequest).Result; } @@ -230,44 +231,34 @@ namespace MediaBrowser.Api.Playback.Progressive { responseHeaders["Accept-Ranges"] = "none"; - var httpClient = new HttpClient(); - - using (var message = new HttpRequestMessage(HttpMethod.Get, mediaPath)) + var response = await HttpClient.GetResponse(new HttpRequestOptions { - var useragent = GetUserAgent(mediaPath); + Url = mediaPath, + UserAgent = GetUserAgent(mediaPath), + BufferContent = false - if (!string.IsNullOrEmpty(useragent)) - { - message.Headers.Add("User-Agent", useragent); - } + }).ConfigureAwait(false); - var response = await httpClient.SendAsync(message, HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false); - response.EnsureSuccessStatusCode(); - - var contentType = response.Content.Headers.ContentType.MediaType; - - // Headers only - if (isHeadRequest) + if (isHeadRequest) + { + using (response.Content) { - response.Dispose(); - httpClient.Dispose(); - - return ResultFactory.GetResult(null, contentType, responseHeaders); + return ResultFactory.GetResult(null, response.ContentType, responseHeaders); } + } - var result = new StaticRemoteStreamWriter(response, httpClient); - - result.Options["Content-Type"] = contentType; + var result = new StaticRemoteStreamWriter(response); - // Add the response headers to the result object - foreach (var header in responseHeaders) - { - result.Options[header.Key] = header.Value; - } + result.Options["Content-Type"] = response.ContentType; - return result; + // Add the response headers to the result object + foreach (var header in responseHeaders) + { + result.Options[header.Key] = header.Value; } + + return result; } /// diff --git a/MediaBrowser.Api/Playback/Progressive/VideoService.cs b/MediaBrowser.Api/Playback/Progressive/VideoService.cs index 916f99a102..4bd8c351c2 100644 --- a/MediaBrowser.Api/Playback/Progressive/VideoService.cs +++ b/MediaBrowser.Api/Playback/Progressive/VideoService.cs @@ -1,4 +1,5 @@ using MediaBrowser.Common.IO; +using MediaBrowser.Common.Net; using MediaBrowser.Controller.Configuration; using MediaBrowser.Controller.Drawing; using MediaBrowser.Controller.Dto; @@ -57,8 +58,8 @@ namespace MediaBrowser.Api.Playback.Progressive /// public class VideoService : BaseProgressiveStreamingService { - public VideoService(IServerConfigurationManager serverConfig, IUserManager userManager, ILibraryManager libraryManager, IIsoManager isoManager, IMediaEncoder mediaEncoder, IDtoService dtoService, IFileSystem fileSystem, IItemRepository itemRepository, ILiveTvManager liveTvManager, IImageProcessor imageProcessor) - : base(serverConfig, userManager, libraryManager, isoManager, mediaEncoder, dtoService, fileSystem, itemRepository, liveTvManager, imageProcessor) + public VideoService(IServerConfigurationManager serverConfig, IUserManager userManager, ILibraryManager libraryManager, IIsoManager isoManager, IMediaEncoder mediaEncoder, IDtoService dtoService, IFileSystem fileSystem, IItemRepository itemRepository, ILiveTvManager liveTvManager, IImageProcessor imageProcessor, IHttpClient httpClient) + : base(serverConfig, userManager, libraryManager, isoManager, mediaEncoder, dtoService, fileSystem, itemRepository, liveTvManager, imageProcessor, httpClient) { } diff --git a/MediaBrowser.Api/Playback/StaticRemoteStreamWriter.cs b/MediaBrowser.Api/Playback/StaticRemoteStreamWriter.cs index 7820a26d86..24dce64dfe 100644 --- a/MediaBrowser.Api/Playback/StaticRemoteStreamWriter.cs +++ b/MediaBrowser.Api/Playback/StaticRemoteStreamWriter.cs @@ -1,7 +1,7 @@ -using ServiceStack.Web; +using MediaBrowser.Common.Net; +using ServiceStack.Web; using System.Collections.Generic; using System.IO; -using System.Net.Http; using System.Threading.Tasks; namespace MediaBrowser.Api.Playback @@ -14,22 +14,16 @@ namespace MediaBrowser.Api.Playback /// /// The _input stream /// - private readonly HttpResponseMessage _msg; - - private readonly HttpClient _client; + private readonly HttpResponseInfo _response; /// /// The _options /// private readonly IDictionary _options = new Dictionary(); - /// - /// Initializes a new instance of the class. - /// - public StaticRemoteStreamWriter(HttpResponseMessage msg, HttpClient client) + public StaticRemoteStreamWriter(HttpResponseInfo response) { - _msg = msg; - _client = client; + _response = response; } /// @@ -59,15 +53,9 @@ namespace MediaBrowser.Api.Playback /// Task. public async Task WriteToAsync(Stream responseStream) { - using (_client) + using (var remoteStream = _response.Content) { - using (_msg) - { - using (var remoteStream = await _msg.Content.ReadAsStreamAsync().ConfigureAwait(false)) - { - await remoteStream.CopyToAsync(responseStream, 819200).ConfigureAwait(false); - } - } + await remoteStream.CopyToAsync(responseStream, 819200).ConfigureAwait(false); } } } diff --git a/MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs b/MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs index a5b241b4b8..a49a0a0dbc 100644 --- a/MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs +++ b/MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs @@ -276,6 +276,26 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager { options.CancellationToken.ThrowIfCancellationRequested(); + if (!options.BufferContent) + { + var response = await httpWebRequest.GetResponseAsync().ConfigureAwait(false); + + var httpResponse = (HttpWebResponse)response; + + EnsureSuccessStatusCode(httpResponse); + + options.CancellationToken.ThrowIfCancellationRequested(); + + return new HttpResponseInfo + { + Content = httpResponse.GetResponseStream(), + + StatusCode = httpResponse.StatusCode, + + ContentType = httpResponse.ContentType + }; + } + using (var response = await httpWebRequest.GetResponseAsync().ConfigureAwait(false)) { var httpResponse = (HttpWebResponse)response; diff --git a/MediaBrowser.Common/Net/HttpRequestOptions.cs b/MediaBrowser.Common/Net/HttpRequestOptions.cs index db78fc927c..ebe76e62c7 100644 --- a/MediaBrowser.Common/Net/HttpRequestOptions.cs +++ b/MediaBrowser.Common/Net/HttpRequestOptions.cs @@ -70,6 +70,8 @@ namespace MediaBrowser.Common.Net public string RequestContent { get; set; } + public bool BufferContent { get; set; } + private string GetHeaderValue(string name) { string value; @@ -85,6 +87,7 @@ namespace MediaBrowser.Common.Net public HttpRequestOptions() { EnableHttpCompression = true; + BufferContent = true; RequestHeaders = new Dictionary(StringComparer.OrdinalIgnoreCase); } diff --git a/MediaBrowser.ServerApplication/ApplicationHost.cs b/MediaBrowser.ServerApplication/ApplicationHost.cs index 5af08073be..fe734bcf8c 100644 --- a/MediaBrowser.ServerApplication/ApplicationHost.cs +++ b/MediaBrowser.ServerApplication/ApplicationHost.cs @@ -259,32 +259,35 @@ namespace MediaBrowser.ServerApplication // Not there, no big deal } - try + Task.Run(() => { - Directory.Delete(Path.Combine(ApplicationPaths.DataPath, "remote-images"), true); - } - catch (IOException) - { - // Not there, no big deal - } + try + { + Directory.Delete(Path.Combine(ApplicationPaths.DataPath, "remote-images"), true); + } + catch (IOException) + { + // Not there, no big deal + } - try - { - Directory.Delete(Path.Combine(ApplicationPaths.DataPath, "extracted-video-images"), true); - } - catch (IOException) - { - // Not there, no big deal - } + try + { + Directory.Delete(Path.Combine(ApplicationPaths.DataPath, "extracted-video-images"), true); + } + catch (IOException) + { + // Not there, no big deal + } - try - { - Directory.Delete(Path.Combine(ApplicationPaths.DataPath, "extracted-audio-images"), true); - } - catch (IOException) - { - // Not there, no big deal - } + try + { + Directory.Delete(Path.Combine(ApplicationPaths.DataPath, "extracted-audio-images"), true); + } + catch (IOException) + { + // Not there, no big deal + } + }); } ///