The Free Software Media System - Server Backend & API
convert static remote streaming to use internal interfaces
Luke Pulverentibe1ce0f80275
diff --git a/MediaBrowser.Api/MediaBrowser.Api.csproj b/MediaBrowser.Api/MediaBrowser.Api.csprojindex e105b41ce5..e62ed3e25b 100644--- a/MediaBrowser.Api/MediaBrowser.Api.csproj+++ b/MediaBrowser.Api/MediaBrowser.Api.csproj@@ -54,10 +54,6 @@<Reference Include="Microsoft.CSharp" /><Reference Include="System.Data" /><Reference Include="System.Drawing" />- <Reference Include="System.Net.Http" />- <Reference Include="MoreLinq">- <HintPath>..\packages\morelinq.1.0.16006\lib\net35\MoreLinq.dll</HintPath>- </Reference><Reference Include="System.Xml" /><Reference Include="ServiceStack.Interfaces"><HintPath>..\ThirdParty\ServiceStack\ServiceStack.Interfaces.dll</HintPath>diff --git a/MediaBrowser.Api/Playback/BaseStreamingService.cs b/MediaBrowser.Api/Playback/BaseStreamingService.csindex 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.csindex 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/// </summary>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.csindex 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.Progressivepublic 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;}/// <summary>@@ -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.Progressiveif (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;}/// <summary>diff --git a/MediaBrowser.Api/Playback/Progressive/VideoService.cs b/MediaBrowser.Api/Playback/Progressive/VideoService.csindex 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/// </summary>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.csindex 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/// <summary>/// The _input stream/// </summary>- private readonly HttpResponseMessage _msg;-- private readonly HttpClient _client;+ private readonly HttpResponseInfo _response;/// <summary>/// The _options/// </summary>private readonly IDictionary<string, string> _options = new Dictionary<string, string>();- /// <summary>- /// Initializes a new instance of the <see cref="StaticRemoteStreamWriter"/> class.- /// </summary>- public StaticRemoteStreamWriter(HttpResponseMessage msg, HttpClient client)+ public StaticRemoteStreamWriter(HttpResponseInfo response){- _msg = msg;- _client = client;+ _response = response;}/// <summary>@@ -59,15 +53,9 @@ namespace MediaBrowser.Api.Playback/// <returns>Task.</returns>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.csindex 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.csindex db78fc927c..ebe76e62c7 100644--- a/MediaBrowser.Common/Net/HttpRequestOptions.cs+++ b/MediaBrowser.Common/Net/HttpRequestOptions.cs@@ -70,6 +70,8 @@ namespace MediaBrowser.Common.Netpublic string RequestContent { get; set; }+ public bool BufferContent { get; set; }+private string GetHeaderValue(string name){string value;@@ -85,6 +87,7 @@ namespace MediaBrowser.Common.Netpublic HttpRequestOptions(){EnableHttpCompression = true;+ BufferContent = true;RequestHeaders = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);}diff --git a/MediaBrowser.ServerApplication/ApplicationHost.cs b/MediaBrowser.ServerApplication/ApplicationHost.csindex 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+ }+ });}/// <summary>