From 6fe82962661d30baadb19d241f30fb6126f53406 Mon Sep 17 00:00:00 2001 From: LukePulverenti Luke Pulverenti luke pulverenti Date: Sun, 12 Aug 2012 13:05:51 -0400 Subject: Moved more common code from audio/video handler down to the base class --- MediaBrowser.Api/HttpHandlers/BaseMediaHandler.cs | 57 +++++++++++++++++++++-- 1 file changed, 54 insertions(+), 3 deletions(-) (limited to 'MediaBrowser.Api/HttpHandlers/BaseMediaHandler.cs') diff --git a/MediaBrowser.Api/HttpHandlers/BaseMediaHandler.cs b/MediaBrowser.Api/HttpHandlers/BaseMediaHandler.cs index f3b217c0d..890e73950 100644 --- a/MediaBrowser.Api/HttpHandlers/BaseMediaHandler.cs +++ b/MediaBrowser.Api/HttpHandlers/BaseMediaHandler.cs @@ -1,6 +1,8 @@ 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.Configuration; @@ -15,6 +17,28 @@ namespace MediaBrowser.Api.HttpHandlers public abstract class BaseMediaHandler : BaseHandler where T : BaseItem, new() { + /// + /// Supported values: mp3,flac,ogg,wav,asf,wma,aac + /// + protected virtual IEnumerable OutputFormats + { + get + { + return QueryString["outputformats"].Split(','); + } + } + + /// + /// These formats can be outputted directly but cannot be encoded to + /// + protected virtual IEnumerable UnsupportedOutputEncodingFormats + { + get + { + return new string[] { }; + } + } + private T _LibraryItem; /// /// Gets the library item that will be played, if any @@ -71,7 +95,7 @@ namespace MediaBrowser.Api.HttpHandlers { get { - return MimeTypes.GetMimeType("." + GetOutputFormat()); + return MimeTypes.GetMimeType("." + GetConversionOutputFormat()); } } @@ -97,8 +121,35 @@ namespace MediaBrowser.Api.HttpHandlers } protected abstract string GetCommandLineArguments(); - protected abstract string GetOutputFormat(); - protected abstract bool RequiresConversion(); + + /// + /// Gets the format we'll be converting to + /// + protected virtual string GetConversionOutputFormat() + { + return OutputFormats.First(f => !UnsupportedOutputEncodingFormats.Any(s => s.Equals(f, StringComparison.OrdinalIgnoreCase))); + } + + protected virtual bool RequiresConversion() + { + string currentFormat = Path.GetExtension(LibraryItem.Path).Replace(".", string.Empty); + + if (OutputFormats.Any(f => currentFormat.EndsWith(f, StringComparison.OrdinalIgnoreCase))) + { + // We can output these files directly, but we can't encode them + if (UnsupportedOutputEncodingFormats.Any(f => currentFormat.EndsWith(f, StringComparison.OrdinalIgnoreCase))) + { + return false; + } + } + else + { + // If it's not in a format the consumer accepts, return true + return true; + } + + return false; + } protected async override Task WriteResponseToOutputStream(Stream stream) { -- cgit v1.2.3