convert static remote streaming to use internal interfaces

This commit is contained in:
Luke Pulverenti 2014-02-13 11:38:43 -05:00
parent eec9e04825
commit be1ce0f802
9 changed files with 114 additions and 90 deletions

View File

@ -54,10 +54,6 @@
<Reference Include="Microsoft.CSharp" /> <Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" /> <Reference Include="System.Data" />
<Reference Include="System.Drawing" /> <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="System.Xml" />
<Reference Include="ServiceStack.Interfaces"> <Reference Include="ServiceStack.Interfaces">
<HintPath>..\ThirdParty\ServiceStack\ServiceStack.Interfaces.dll</HintPath> <HintPath>..\ThirdParty\ServiceStack\ServiceStack.Interfaces.dll</HintPath>

View File

@ -1222,6 +1222,27 @@ namespace MediaBrowser.Api.Playback
{ {
request.AudioChannels = int.Parse(val, UsCulture); 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;
}
}
} }
} }

View File

@ -1,4 +1,5 @@
using MediaBrowser.Common.IO; using MediaBrowser.Common.IO;
using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Configuration; using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Drawing; using MediaBrowser.Controller.Drawing;
using MediaBrowser.Controller.Dto; using MediaBrowser.Controller.Dto;
@ -42,8 +43,8 @@ namespace MediaBrowser.Api.Playback.Progressive
/// </summary> /// </summary>
public class AudioService : BaseProgressiveStreamingService 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) 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) : 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); audioTranscodeParams.Add("-ac " + channels.Value);
} }
if (request.AudioSampleRate.HasValue) if (request.AudioSampleRate.HasValue)
{ {
audioTranscodeParams.Add("-ar " + request.AudioSampleRate.Value); audioTranscodeParams.Add("-ar " + request.AudioSampleRate.Value);

View File

@ -1,5 +1,4 @@
using System; using MediaBrowser.Common.IO;
using MediaBrowser.Common.IO;
using MediaBrowser.Common.Net; using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Configuration; using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Drawing; using MediaBrowser.Controller.Drawing;
@ -10,9 +9,9 @@ using MediaBrowser.Controller.MediaInfo;
using MediaBrowser.Controller.Persistence; using MediaBrowser.Controller.Persistence;
using MediaBrowser.Model.Dto; using MediaBrowser.Model.Dto;
using MediaBrowser.Model.IO; using MediaBrowser.Model.IO;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Net.Http;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -24,11 +23,13 @@ namespace MediaBrowser.Api.Playback.Progressive
public abstract class BaseProgressiveStreamingService : BaseStreamingService public abstract class BaseProgressiveStreamingService : BaseStreamingService
{ {
protected readonly IImageProcessor ImageProcessor; 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) : base(serverConfig, userManager, libraryManager, isoManager, mediaEncoder, dtoService, fileSystem, itemRepository, liveTvManager)
{ {
ImageProcessor = imageProcessor; ImageProcessor = imageProcessor;
HttpClient = httpClient;
} }
/// <summary> /// <summary>
@ -157,7 +158,7 @@ namespace MediaBrowser.Api.Playback.Progressive
// // ?? // // ??
// contentFeatures = "DLNA.ORG_PN=WMVHIGH_BASE"; // contentFeatures = "DLNA.ORG_PN=WMVHIGH_BASE";
//} //}
if (!string.IsNullOrEmpty(contentFeatures)) if (!string.IsNullOrEmpty(contentFeatures))
{ {
@ -194,7 +195,7 @@ namespace MediaBrowser.Api.Playback.Progressive
if (request.Static && state.IsRemote) if (request.Static && state.IsRemote)
{ {
AddDlnaHeaders(state, responseHeaders, true); AddDlnaHeaders(state, responseHeaders, true);
return GetStaticRemoteStreamResult(state.MediaPath, responseHeaders, isHeadRequest).Result; return GetStaticRemoteStreamResult(state.MediaPath, responseHeaders, isHeadRequest).Result;
} }
@ -230,44 +231,34 @@ namespace MediaBrowser.Api.Playback.Progressive
{ {
responseHeaders["Accept-Ranges"] = "none"; responseHeaders["Accept-Ranges"] = "none";
var httpClient = new HttpClient(); var response = await HttpClient.GetResponse(new HttpRequestOptions
using (var message = new HttpRequestMessage(HttpMethod.Get, mediaPath))
{ {
var useragent = GetUserAgent(mediaPath); Url = mediaPath,
UserAgent = GetUserAgent(mediaPath),
BufferContent = false
if (!string.IsNullOrEmpty(useragent)) }).ConfigureAwait(false);
if (isHeadRequest)
{
using (response.Content)
{ {
message.Headers.Add("User-Agent", useragent); return ResultFactory.GetResult(null, response.ContentType, responseHeaders);
} }
var response = await httpClient.SendAsync(message, HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false);
response.EnsureSuccessStatusCode();
var contentType = response.Content.Headers.ContentType.MediaType;
// Headers only
if (isHeadRequest)
{
response.Dispose();
httpClient.Dispose();
return ResultFactory.GetResult(null, contentType, responseHeaders);
}
var result = new StaticRemoteStreamWriter(response, httpClient);
result.Options["Content-Type"] = contentType;
// Add the response headers to the result object
foreach (var header in responseHeaders)
{
result.Options[header.Key] = header.Value;
}
return result;
} }
var result = new StaticRemoteStreamWriter(response);
result.Options["Content-Type"] = response.ContentType;
// Add the response headers to the result object
foreach (var header in responseHeaders)
{
result.Options[header.Key] = header.Value;
}
return result;
} }
/// <summary> /// <summary>

View File

@ -1,4 +1,5 @@
using MediaBrowser.Common.IO; using MediaBrowser.Common.IO;
using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Configuration; using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Drawing; using MediaBrowser.Controller.Drawing;
using MediaBrowser.Controller.Dto; using MediaBrowser.Controller.Dto;
@ -57,8 +58,8 @@ namespace MediaBrowser.Api.Playback.Progressive
/// </summary> /// </summary>
public class VideoService : BaseProgressiveStreamingService 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) 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) : base(serverConfig, userManager, libraryManager, isoManager, mediaEncoder, dtoService, fileSystem, itemRepository, liveTvManager, imageProcessor, httpClient)
{ {
} }

View File

@ -1,7 +1,7 @@
using ServiceStack.Web; using MediaBrowser.Common.Net;
using ServiceStack.Web;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Net.Http;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace MediaBrowser.Api.Playback namespace MediaBrowser.Api.Playback
@ -14,22 +14,16 @@ namespace MediaBrowser.Api.Playback
/// <summary> /// <summary>
/// The _input stream /// The _input stream
/// </summary> /// </summary>
private readonly HttpResponseMessage _msg; private readonly HttpResponseInfo _response;
private readonly HttpClient _client;
/// <summary> /// <summary>
/// The _options /// The _options
/// </summary> /// </summary>
private readonly IDictionary<string, string> _options = new Dictionary<string, string>(); private readonly IDictionary<string, string> _options = new Dictionary<string, string>();
/// <summary> public StaticRemoteStreamWriter(HttpResponseInfo response)
/// Initializes a new instance of the <see cref="StaticRemoteStreamWriter"/> class.
/// </summary>
public StaticRemoteStreamWriter(HttpResponseMessage msg, HttpClient client)
{ {
_msg = msg; _response = response;
_client = client;
} }
/// <summary> /// <summary>
@ -59,15 +53,9 @@ namespace MediaBrowser.Api.Playback
/// <returns>Task.</returns> /// <returns>Task.</returns>
public async Task WriteToAsync(Stream responseStream) public async Task WriteToAsync(Stream responseStream)
{ {
using (_client) using (var remoteStream = _response.Content)
{ {
using (_msg) await remoteStream.CopyToAsync(responseStream, 819200).ConfigureAwait(false);
{
using (var remoteStream = await _msg.Content.ReadAsStreamAsync().ConfigureAwait(false))
{
await remoteStream.CopyToAsync(responseStream, 819200).ConfigureAwait(false);
}
}
} }
} }
} }

View File

@ -276,6 +276,26 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager
{ {
options.CancellationToken.ThrowIfCancellationRequested(); 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)) using (var response = await httpWebRequest.GetResponseAsync().ConfigureAwait(false))
{ {
var httpResponse = (HttpWebResponse)response; var httpResponse = (HttpWebResponse)response;

View File

@ -70,6 +70,8 @@ namespace MediaBrowser.Common.Net
public string RequestContent { get; set; } public string RequestContent { get; set; }
public bool BufferContent { get; set; }
private string GetHeaderValue(string name) private string GetHeaderValue(string name)
{ {
string value; string value;
@ -85,6 +87,7 @@ namespace MediaBrowser.Common.Net
public HttpRequestOptions() public HttpRequestOptions()
{ {
EnableHttpCompression = true; EnableHttpCompression = true;
BufferContent = true;
RequestHeaders = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase); RequestHeaders = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
} }

View File

@ -259,32 +259,35 @@ namespace MediaBrowser.ServerApplication
// Not there, no big deal // Not there, no big deal
} }
try Task.Run(() =>
{ {
Directory.Delete(Path.Combine(ApplicationPaths.DataPath, "remote-images"), true); try
} {
catch (IOException) Directory.Delete(Path.Combine(ApplicationPaths.DataPath, "remote-images"), true);
{ }
// Not there, no big deal catch (IOException)
} {
// Not there, no big deal
}
try try
{ {
Directory.Delete(Path.Combine(ApplicationPaths.DataPath, "extracted-video-images"), true); Directory.Delete(Path.Combine(ApplicationPaths.DataPath, "extracted-video-images"), true);
} }
catch (IOException) catch (IOException)
{ {
// Not there, no big deal // Not there, no big deal
} }
try try
{ {
Directory.Delete(Path.Combine(ApplicationPaths.DataPath, "extracted-audio-images"), true); Directory.Delete(Path.Combine(ApplicationPaths.DataPath, "extracted-audio-images"), true);
} }
catch (IOException) catch (IOException)
{ {
// Not there, no big deal // Not there, no big deal
} }
});
} }
/// <summary> /// <summary>