aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Api/HttpHandlers/AudioHandler.cs
diff options
context:
space:
mode:
authorLukePulverenti Luke Pulverenti luke pulverenti <LukePulverenti Luke Pulverenti luke.pulverenti@gmail.com>2012-08-11 14:07:07 -0400
committerLukePulverenti Luke Pulverenti luke pulverenti <LukePulverenti Luke Pulverenti luke.pulverenti@gmail.com>2012-08-11 14:07:07 -0400
commit24d2c441b3d265026ee77297ea4b7a3ffb47918b (patch)
treebdea0879028a40c597c3d1836e7326d00f9d1b6d /MediaBrowser.Api/HttpHandlers/AudioHandler.cs
parent51227bef6fb1f7ebd4dbb599e9c1692f3fb2e981 (diff)
Re-worked async actions in BaseHandler, and changed AudioBitRate to AudioBitRates.
Diffstat (limited to 'MediaBrowser.Api/HttpHandlers/AudioHandler.cs')
-rw-r--r--MediaBrowser.Api/HttpHandlers/AudioHandler.cs244
1 files changed, 122 insertions, 122 deletions
diff --git a/MediaBrowser.Api/HttpHandlers/AudioHandler.cs b/MediaBrowser.Api/HttpHandlers/AudioHandler.cs
index a4afe1a1d..61f6e7fbc 100644
--- a/MediaBrowser.Api/HttpHandlers/AudioHandler.cs
+++ b/MediaBrowser.Api/HttpHandlers/AudioHandler.cs
@@ -4,6 +4,7 @@ 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;
@@ -12,54 +13,8 @@ using MediaBrowser.Model.Entities;
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
{
get
@@ -75,115 +30,70 @@ namespace MediaBrowser.Api.HttpHandlers
}
}
- public int? AudioBitRate
+ public IEnumerable<int> AudioBitRates
{
get
{
- string val = QueryString["audiobitrate"];
-
- if (string.IsNullOrEmpty(val))
- {
- return null;
- }
-
- return int.Parse(val);
- }
- }
-
- public int? AudioChannels
- {
- get
- {
- string val = QueryString["audiochannels"];
-
- if (string.IsNullOrEmpty(val))
- {
- return null;
- }
-
- return int.Parse(val);
- }
- }
-
- public int? AudioSampleRate
- {
- get
- {
- string val = QueryString["audiosamplerate"];
+ string val = QueryString["audioformats"];
if (string.IsNullOrEmpty(val))
{
- return 44100;
+ return new int[] { };
}
- return int.Parse(val);
+ return val.Split(',').Select(v => int.Parse(v));
}
}
- public override void ProcessRequest(HttpListenerContext ctx)
+ private int? GetMaxAcceptedBitRate(string audioFormat)
{
- HttpListenerContext = ctx;
-
- if (!RequiresTranscoding())
- {
- new StaticFileHandler() { Path = LibraryItem.Path }.ProcessRequest(ctx);
- return;
- }
+ int index = AudioFormats.ToList().IndexOf(audioFormat);
- base.ProcessRequest(ctx);
+ return AudioBitRates.ElementAtOrDefault(index);
}
/// <summary>
/// Determines whether or not the original file requires transcoding
/// </summary>
- private bool RequiresTranscoding()
+ 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 => LibraryItem.Path.EndsWith(f, StringComparison.OrdinalIgnoreCase)))
+ 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 (AudioBitRate.HasValue)
+ if (bitrate.HasValue && bitrate.Value < LibraryItem.BitRate)
{
- if (AudioBitRate.Value < LibraryItem.BitRate)
- {
- return true;
- }
+ return true;
}
// If the number of channels is greater than our desired channels, we need to transcode
- if (AudioChannels.HasValue)
+ if (AudioChannels.HasValue && AudioChannels.Value < LibraryItem.Channels)
{
- if (AudioChannels.Value < LibraryItem.Channels)
- {
- return true;
- }
+ return true;
}
// If the sample rate is greater than our desired sample rate, we need to transcode
- if (AudioSampleRate.HasValue)
+ if (AudioSampleRate.HasValue && AudioSampleRate.Value < LibraryItem.SampleRate)
{
- if (AudioSampleRate.Value < LibraryItem.SampleRate)
- {
- return true;
- }
+ return true;
}
// Yay
return false;
}
- private string GetOutputFormat()
+ /// <summary>
+ /// Gets the format we'll be converting to
+ /// </summary>
+ protected override string GetOutputFormat()
{
- string format = AudioFormats.FirstOrDefault(f => LibraryItem.Path.EndsWith(f, StringComparison.OrdinalIgnoreCase));
-
- if (!string.IsNullOrWhiteSpace(format))
- {
- return format;
- }
-
return AudioFormats.First();
}
@@ -194,9 +104,13 @@ namespace MediaBrowser.Api.HttpHandlers
{
List<string> audioTranscodeParams = new List<string>();
- if (AudioBitRate.HasValue)
+ string outputFormat = GetOutputFormat();
+
+ int? bitrate = GetMaxAcceptedBitRate(outputFormat);
+
+ if (bitrate.HasValue)
{
- audioTranscodeParams.Add("-ab " + AudioBitRate.Value);
+ audioTranscodeParams.Add("-ab " + bitrate.Value);
}
if (AudioChannels.HasValue)
@@ -209,12 +123,12 @@ namespace MediaBrowser.Api.HttpHandlers
audioTranscodeParams.Add("-ar " + AudioSampleRate.Value);
}
- audioTranscodeParams.Add("-f " + GetOutputFormat());
+ audioTranscodeParams.Add("-f " + outputFormat);
return "-i \"" + LibraryItem.Path + "\" -vn " + string.Join(" ", audioTranscodeParams.ToArray()) + " -";
}
- protected async override void WriteResponseToOutputStream(Stream stream)
+ protected async override Task WriteResponseToOutputStream(Stream stream)
{
ProcessStartInfo startInfo = new ProcessStartInfo();
@@ -227,7 +141,7 @@ namespace MediaBrowser.Api.HttpHandlers
startInfo.WorkingDirectory = ApiService.FFMpegDirectory;
startInfo.Arguments = GetAudioArguments();
- Logger.LogInfo("Audio Handler Transcode: " + ApiService.FFMpegPath + " " + startInfo.Arguments);
+ Logger.LogInfo(startInfo.FileName + " " + startInfo.Arguments);
Process process = new Process();
process.StartInfo = startInfo;
@@ -244,10 +158,96 @@ namespace MediaBrowser.Api.HttpHandlers
}
finally
{
- DisposeResponseStream();
-
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;
+ }
+ }
+
+ public int? AudioChannels
+ {
+ get
+ {
+ string val = QueryString["audiochannels"];
+
+ if (string.IsNullOrEmpty(val))
+ {
+ return null;
+ }
+
+ return int.Parse(val);
+ }
+ }
+
+ public int? AudioSampleRate
+ {
+ get
+ {
+ string val = QueryString["audiosamplerate"];
+
+ if (string.IsNullOrEmpty(val))
+ {
+ return 44100;
+ }
+
+ return int.Parse(val);
+ }
+ }
+
+ public override string ContentType
+ {
+ get
+ {
+ return MimeTypes.GetMimeType("." + GetOutputFormat());
+ }
+ }
+
+ public override bool CompressResponse
+ {
+ get
+ {
+ return false;
+ }
+ }
+
+ public override void ProcessRequest(HttpListenerContext ctx)
+ {
+ HttpListenerContext = ctx;
+
+ if (!RequiresConversion())
+ {
+ new StaticFileHandler() { Path = LibraryItem.Path }.ProcessRequest(ctx);
+ return;
+ }
+
+ base.ProcessRequest(ctx);
+ }
+
+ protected abstract string GetOutputFormat();
+ protected abstract bool RequiresConversion();
+ }
}