Re-worked async actions in BaseHandler, and changed AudioBitRate to AudioBitRates.

This commit is contained in:
LukePulverenti Luke Pulverenti luke pulverenti 2012-08-11 14:07:07 -04:00
parent 51227bef6f
commit 24d2c441b3
10 changed files with 270 additions and 209 deletions

View File

@ -4,6 +4,7 @@ using System.Diagnostics;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
using System.Threading.Tasks;
using MediaBrowser.Common.Logging; using MediaBrowser.Common.Logging;
using MediaBrowser.Common.Net; using MediaBrowser.Common.Net;
using MediaBrowser.Common.Net.Handlers; using MediaBrowser.Common.Net.Handlers;
@ -12,54 +13,8 @@ using MediaBrowser.Model.Entities;
namespace MediaBrowser.Api.HttpHandlers namespace MediaBrowser.Api.HttpHandlers
{ {
public class AudioHandler : BaseHandler public class AudioHandler : BaseMediaHandler<Audio>
{ {
private Audio _LibraryItem;
/// <summary>
/// Gets the library item that will be played, if any
/// </summary>
private Audio LibraryItem
{
get
{
if (_LibraryItem == null)
{
string id = QueryString["id"];
if (!string.IsNullOrEmpty(id))
{
_LibraryItem = Kernel.Instance.GetItemById(Guid.Parse(id)) as Audio;
}
}
return _LibraryItem;
}
}
public override bool CompressResponse
{
get
{
return false;
}
}
protected override bool IsAsyncHandler
{
get
{
return true;
}
}
public override string ContentType
{
get
{
return MimeTypes.GetMimeType("." + GetOutputFormat());
}
}
public IEnumerable<string> AudioFormats public IEnumerable<string> AudioFormats
{ {
get get
@ -75,18 +30,161 @@ namespace MediaBrowser.Api.HttpHandlers
} }
} }
public int? AudioBitRate public IEnumerable<int> AudioBitRates
{ {
get get
{ {
string val = QueryString["audiobitrate"]; string val = QueryString["audioformats"];
if (string.IsNullOrEmpty(val)) if (string.IsNullOrEmpty(val))
{ {
return null; return new int[] { };
} }
return int.Parse(val); return val.Split(',').Select(v => int.Parse(v));
}
}
private int? GetMaxAcceptedBitRate(string audioFormat)
{
int index = AudioFormats.ToList().IndexOf(audioFormat);
return AudioBitRates.ElementAtOrDefault(index);
}
/// <summary>
/// Determines whether or not the original file requires transcoding
/// </summary>
protected override bool RequiresConversion()
{
string currentFormat = Path.GetExtension(LibraryItem.Path).Replace(".", string.Empty);
// If it's not in a format the consumer accepts, return true
if (!AudioFormats.Any(f => currentFormat.EndsWith(f, StringComparison.OrdinalIgnoreCase)))
{
return true;
}
int? bitrate = GetMaxAcceptedBitRate(currentFormat);
// If the bitrate is greater than our desired bitrate, we need to transcode
if (bitrate.HasValue && bitrate.Value < LibraryItem.BitRate)
{
return true;
}
// If the number of channels is greater than our desired channels, we need to transcode
if (AudioChannels.HasValue && AudioChannels.Value < LibraryItem.Channels)
{
return true;
}
// If the sample rate is greater than our desired sample rate, we need to transcode
if (AudioSampleRate.HasValue && AudioSampleRate.Value < LibraryItem.SampleRate)
{
return true;
}
// Yay
return false;
}
/// <summary>
/// Gets the format we'll be converting to
/// </summary>
protected override string GetOutputFormat()
{
return AudioFormats.First();
}
/// <summary>
/// Creates arguments to pass to ffmpeg
/// </summary>
private string GetAudioArguments()
{
List<string> audioTranscodeParams = new List<string>();
string outputFormat = GetOutputFormat();
int? bitrate = GetMaxAcceptedBitRate(outputFormat);
if (bitrate.HasValue)
{
audioTranscodeParams.Add("-ab " + bitrate.Value);
}
if (AudioChannels.HasValue)
{
audioTranscodeParams.Add("-ac " + AudioChannels.Value);
}
if (AudioSampleRate.HasValue)
{
audioTranscodeParams.Add("-ar " + AudioSampleRate.Value);
}
audioTranscodeParams.Add("-f " + outputFormat);
return "-i \"" + LibraryItem.Path + "\" -vn " + string.Join(" ", audioTranscodeParams.ToArray()) + " -";
}
protected async override Task WriteResponseToOutputStream(Stream stream)
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
startInfo.FileName = ApiService.FFMpegPath;
startInfo.WorkingDirectory = ApiService.FFMpegDirectory;
startInfo.Arguments = GetAudioArguments();
Logger.LogInfo(startInfo.FileName + " " + startInfo.Arguments);
Process process = new Process();
process.StartInfo = startInfo;
try
{
process.Start();
await process.StandardOutput.BaseStream.CopyToAsync(stream);
}
catch (Exception ex)
{
Logger.LogException(ex);
}
finally
{
process.Dispose();
}
}
}
public abstract class BaseMediaHandler<T> : BaseHandler
where T : BaseItem, new()
{
private T _LibraryItem;
/// <summary>
/// Gets the library item that will be played, if any
/// </summary>
protected T LibraryItem
{
get
{
if (_LibraryItem == null)
{
string id = QueryString["id"];
if (!string.IsNullOrEmpty(id))
{
_LibraryItem = Kernel.Instance.GetItemById(Guid.Parse(id)) as T;
}
}
return _LibraryItem;
} }
} }
@ -120,11 +218,27 @@ namespace MediaBrowser.Api.HttpHandlers
} }
} }
public override string ContentType
{
get
{
return MimeTypes.GetMimeType("." + GetOutputFormat());
}
}
public override bool CompressResponse
{
get
{
return false;
}
}
public override void ProcessRequest(HttpListenerContext ctx) public override void ProcessRequest(HttpListenerContext ctx)
{ {
HttpListenerContext = ctx; HttpListenerContext = ctx;
if (!RequiresTranscoding()) if (!RequiresConversion())
{ {
new StaticFileHandler() { Path = LibraryItem.Path }.ProcessRequest(ctx); new StaticFileHandler() { Path = LibraryItem.Path }.ProcessRequest(ctx);
return; return;
@ -133,121 +247,7 @@ namespace MediaBrowser.Api.HttpHandlers
base.ProcessRequest(ctx); base.ProcessRequest(ctx);
} }
/// <summary> protected abstract string GetOutputFormat();
/// Determines whether or not the original file requires transcoding protected abstract bool RequiresConversion();
/// </summary>
private bool RequiresTranscoding()
{
// If it's not in a format the consumer accepts, return true
if (!AudioFormats.Any(f => LibraryItem.Path.EndsWith(f, StringComparison.OrdinalIgnoreCase)))
{
return true;
}
// If the bitrate is greater than our desired bitrate, we need to transcode
if (AudioBitRate.HasValue)
{
if (AudioBitRate.Value < LibraryItem.BitRate)
{
return true;
}
}
// If the number of channels is greater than our desired channels, we need to transcode
if (AudioChannels.HasValue)
{
if (AudioChannels.Value < LibraryItem.Channels)
{
return true;
}
}
// If the sample rate is greater than our desired sample rate, we need to transcode
if (AudioSampleRate.HasValue)
{
if (AudioSampleRate.Value < LibraryItem.SampleRate)
{
return true;
}
}
// Yay
return false;
}
private string GetOutputFormat()
{
string format = AudioFormats.FirstOrDefault(f => LibraryItem.Path.EndsWith(f, StringComparison.OrdinalIgnoreCase));
if (!string.IsNullOrWhiteSpace(format))
{
return format;
}
return AudioFormats.First();
}
/// <summary>
/// Creates arguments to pass to ffmpeg
/// </summary>
private string GetAudioArguments()
{
List<string> audioTranscodeParams = new List<string>();
if (AudioBitRate.HasValue)
{
audioTranscodeParams.Add("-ab " + AudioBitRate.Value);
}
if (AudioChannels.HasValue)
{
audioTranscodeParams.Add("-ac " + AudioChannels.Value);
}
if (AudioSampleRate.HasValue)
{
audioTranscodeParams.Add("-ar " + AudioSampleRate.Value);
}
audioTranscodeParams.Add("-f " + GetOutputFormat());
return "-i \"" + LibraryItem.Path + "\" -vn " + string.Join(" ", audioTranscodeParams.ToArray()) + " -";
}
protected async override void WriteResponseToOutputStream(Stream stream)
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
startInfo.FileName = ApiService.FFMpegPath;
startInfo.WorkingDirectory = ApiService.FFMpegDirectory;
startInfo.Arguments = GetAudioArguments();
Logger.LogInfo("Audio Handler Transcode: " + ApiService.FFMpegPath + " " + startInfo.Arguments);
Process process = new Process();
process.StartInfo = startInfo;
try
{
process.Start();
await process.StandardOutput.BaseStream.CopyToAsync(stream);
}
catch (Exception ex)
{
Logger.LogException(ex);
}
finally
{
DisposeResponseStream();
process.Dispose();
}
}
} }
} }

View File

@ -1,6 +1,7 @@
using System; using System;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Threading.Tasks;
using MediaBrowser.Common.Net.Handlers; using MediaBrowser.Common.Net.Handlers;
using MediaBrowser.Controller; using MediaBrowser.Controller;
using MediaBrowser.Model.Entities; using MediaBrowser.Model.Entities;
@ -148,9 +149,12 @@ namespace MediaBrowser.Api.HttpHandlers
} }
} }
protected override void WriteResponseToOutputStream(Stream stream) protected override Task WriteResponseToOutputStream(Stream stream)
{ {
ImageProcessor.ProcessImage(ImagePath, stream, Width, Height, MaxWidth, MaxHeight, Quality); return Task.Run(() =>
{
ImageProcessor.ProcessImage(ImagePath, stream, Width, Height, MaxWidth, MaxHeight, Quality);
});
} }
private string GetImagePath() private string GetImagePath()

View File

@ -1,4 +1,5 @@
using System.IO; using System.IO;
using System.Threading.Tasks;
using MediaBrowser.Common.Net.Handlers; using MediaBrowser.Common.Net.Handlers;
using MediaBrowser.Common.Serialization; using MediaBrowser.Common.Serialization;
@ -8,9 +9,12 @@ namespace MediaBrowser.Api.HttpHandlers
{ {
protected abstract object ObjectToSerialize { get; } protected abstract object ObjectToSerialize { get; }
protected override void WriteResponseToOutputStream(Stream stream) protected override Task WriteResponseToOutputStream(Stream stream)
{ {
JsonSerializer.SerializeToStream(ObjectToSerialize, stream); return Task.Run(() =>
{
JsonSerializer.SerializeToStream(ObjectToSerialize, stream);
});
} }
} }
} }

View File

@ -0,0 +1,62 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using MediaBrowser.Common.Logging;
using MediaBrowser.Common.Net;
using MediaBrowser.Common.Net.Handlers;
using MediaBrowser.Controller;
using MediaBrowser.Model.Entities;
namespace MediaBrowser.Api.HttpHandlers
{
class VideoHandler : BaseMediaHandler<Video>
{
public IEnumerable<string> VideoFormats
{
get
{
return QueryString["videoformats"].Split(',');
}
}
/// <summary>
/// Gets the format we'll be converting to
/// </summary>
protected override string GetOutputFormat()
{
return VideoFormats.First();
}
protected override bool RequiresConversion()
{
// If it's not in a format the consumer accepts, return true
if (!VideoFormats.Any(f => LibraryItem.Path.EndsWith(f, StringComparison.OrdinalIgnoreCase)))
{
return true;
}
AudioStream audio = LibraryItem.AudioStreams.FirstOrDefault();
if (audio != null)
{
// If the number of channels is greater than our desired channels, we need to transcode
if (AudioChannels.HasValue && AudioChannels.Value < audio.Channels)
{
return true;
}
}
// Yay
return false;
}
protected override Task WriteResponseToOutputStream(Stream stream)
{
throw new NotImplementedException();
}
}
}

View File

@ -63,6 +63,7 @@
<Compile Include="HttpHandlers\StudiosHandler.cs" /> <Compile Include="HttpHandlers\StudiosHandler.cs" />
<Compile Include="HttpHandlers\UserConfigurationHandler.cs" /> <Compile Include="HttpHandlers\UserConfigurationHandler.cs" />
<Compile Include="HttpHandlers\UsersHandler.cs" /> <Compile Include="HttpHandlers\UsersHandler.cs" />
<Compile Include="HttpHandlers\VideoHandler.cs" />
<Compile Include="ImageProcessor.cs" /> <Compile Include="ImageProcessor.cs" />
<Compile Include="Plugin.cs" /> <Compile Include="Plugin.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
@ -87,6 +88,9 @@
<ItemGroup> <ItemGroup>
<EmbeddedResource Include="ffmpeg\ffmpeg.exe" /> <EmbeddedResource Include="ffmpeg\ffmpeg.exe" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<Content Include="ffmpeg\readme.txt" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup> <PropertyGroup>
<PostBuildEvent>xcopy "$(TargetPath)" "$(SolutionDir)\ProgramData\Plugins\$(ProjectName)\" /y</PostBuildEvent> <PostBuildEvent>xcopy "$(TargetPath)" "$(SolutionDir)\ProgramData\Plugins\$(ProjectName)\" /y</PostBuildEvent>

View File

@ -93,6 +93,10 @@ namespace MediaBrowser.Api
{ {
return new AudioHandler(); return new AudioHandler();
} }
else if (localPath.EndsWith("/api/video", StringComparison.OrdinalIgnoreCase))
{
return new VideoHandler();
}
return null; return null;
} }

View File

@ -0,0 +1,3 @@
This is the 32-bit static build of ffmpeg, located at:
http://ffmpeg.zeranoe.com/builds/

View File

@ -1,5 +1,6 @@
using System; using System;
using System.IO; using System.IO;
using System.Threading.Tasks;
namespace MediaBrowser.Common.Net.Handlers namespace MediaBrowser.Common.Net.Handlers
{ {
@ -48,9 +49,9 @@ namespace MediaBrowser.Common.Net.Handlers
} }
} }
protected override void WriteResponseToOutputStream(Stream stream) protected override Task WriteResponseToOutputStream(Stream stream)
{ {
GetEmbeddedResourceStream().CopyTo(stream); return GetEmbeddedResourceStream().CopyToAsync(stream);
} }
protected abstract Stream GetEmbeddedResourceStream(); protected abstract Stream GetEmbeddedResourceStream();

View File

@ -5,6 +5,7 @@ using System.IO;
using System.IO.Compression; using System.IO.Compression;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
using System.Threading.Tasks;
using MediaBrowser.Common.Logging; using MediaBrowser.Common.Logging;
namespace MediaBrowser.Common.Net.Handlers namespace MediaBrowser.Common.Net.Handlers
@ -36,18 +37,6 @@ namespace MediaBrowser.Common.Net.Handlers
} }
} }
/// <summary>
/// Returns true or false indicating if the handler writes to the stream asynchronously.
/// If so the subclass will be responsible for disposing the stream when complete.
/// </summary>
protected virtual bool IsAsyncHandler
{
get
{
return false;
}
}
protected virtual bool SupportsByteRangeRequests protected virtual bool SupportsByteRangeRequests
{ {
get get
@ -246,7 +235,7 @@ namespace MediaBrowser.Common.Net.Handlers
} }
} }
private void ProcessUncachedResponse(HttpListenerContext ctx, TimeSpan cacheDuration) private async void ProcessUncachedResponse(HttpListenerContext ctx, TimeSpan cacheDuration)
{ {
long? totalContentLength = TotalContentLength; long? totalContentLength = TotalContentLength;
@ -277,8 +266,6 @@ namespace MediaBrowser.Common.Net.Handlers
CacheResponse(ctx.Response, cacheDuration, LastDateModified); CacheResponse(ctx.Response, cacheDuration, LastDateModified);
} }
PrepareUncachedResponse(ctx, cacheDuration);
// Set the status code // Set the status code
ctx.Response.StatusCode = StatusCode; ctx.Response.StatusCode = StatusCode;
@ -301,9 +288,15 @@ namespace MediaBrowser.Common.Net.Handlers
outputStream = CompressedStream; outputStream = CompressedStream;
} }
WriteResponseToOutputStream(outputStream); try
{
if (!IsAsyncHandler) await WriteResponseToOutputStream(outputStream);
}
catch (Exception ex)
{
Logger.LogException(ex);
}
finally
{ {
DisposeResponseStream(); DisposeResponseStream();
} }
@ -315,10 +308,6 @@ namespace MediaBrowser.Common.Net.Handlers
} }
} }
protected virtual void PrepareUncachedResponse(HttpListenerContext ctx, TimeSpan cacheDuration)
{
}
private void CacheResponse(HttpListenerResponse response, TimeSpan duration, DateTime? dateModified) private void CacheResponse(HttpListenerResponse response, TimeSpan duration, DateTime? dateModified)
{ {
DateTime lastModified = dateModified ?? DateTime.Now; DateTime lastModified = dateModified ?? DateTime.Now;
@ -328,9 +317,9 @@ namespace MediaBrowser.Common.Net.Handlers
response.Headers[HttpResponseHeader.LastModified] = lastModified.ToString("r"); response.Headers[HttpResponseHeader.LastModified] = lastModified.ToString("r");
} }
protected abstract void WriteResponseToOutputStream(Stream stream); protected abstract Task WriteResponseToOutputStream(Stream stream);
protected void DisposeResponseStream() private void DisposeResponseStream()
{ {
if (CompressedStream != null) if (CompressedStream != null)
{ {

View File

@ -117,14 +117,6 @@ namespace MediaBrowser.Common.Net.Handlers
} }
} }
protected override bool IsAsyncHandler
{
get
{
return true;
}
}
public override string ContentType public override string ContentType
{ {
get get
@ -133,7 +125,7 @@ namespace MediaBrowser.Common.Net.Handlers
} }
} }
protected async override void WriteResponseToOutputStream(Stream stream) protected async override Task WriteResponseToOutputStream(Stream stream)
{ {
try try
{ {
@ -175,8 +167,6 @@ namespace MediaBrowser.Common.Net.Handlers
{ {
FileStream.Dispose(); FileStream.Dispose();
} }
DisposeResponseStream();
} }
} }