Browse Source

convert static remote streaming to use internal interfaces

Luke Pulverenti 11 years ago
parent
commit
be1ce0f802

+ 0 - 4
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>

+ 21 - 0
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;
+                    }
+                }
             }
         }
 

+ 4 - 3
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);

+ 25 - 34
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;
         }
 
         /// <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.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;
         }
 
         /// <summary>

+ 3 - 2
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)
         {
         }
 

+ 7 - 19
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);
             }
         }
     }

+ 20 - 0
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;

+ 3 - 0
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<string, string>(StringComparer.OrdinalIgnoreCase);
         }

+ 26 - 23
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>