jellyfin-server/MediaBrowser.Api/HttpHandlers/AudioHandler.cs

259 lines
7.1 KiB
C#
Raw Normal View History

2012-08-10 13:18:30 +00:00
using System;
2012-08-10 15:17:52 +00:00
using System.Collections.Generic;
using System.Diagnostics;
2012-08-10 15:17:52 +00:00
using System.IO;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using MediaBrowser.Common.Logging;
using MediaBrowser.Common.Net;
2012-08-10 13:18:30 +00:00
using MediaBrowser.Common.Net.Handlers;
using MediaBrowser.Controller;
using MediaBrowser.Model.Entities;
namespace MediaBrowser.Api.HttpHandlers
{
public class AudioHandler : BaseMediaHandler<Audio>
2012-08-10 13:18:30 +00:00
{
public IEnumerable<string> AudioFormats
2012-08-10 15:17:52 +00:00
{
get
{
2012-08-11 03:19:11 +00:00
string val = QueryString["audioformats"];
2012-08-10 15:17:52 +00:00
if (string.IsNullOrEmpty(val))
{
return new string[] { "mp3" };
2012-08-10 15:17:52 +00:00
}
return val.Split(',');
2012-08-10 15:17:52 +00:00
}
}
public IEnumerable<int> AudioBitRates
2012-08-10 15:17:52 +00:00
{
get
{
string val = QueryString["audioformats"];
2012-08-10 15:17:52 +00:00
if (string.IsNullOrEmpty(val))
2012-08-10 13:18:30 +00:00
{
return new int[] { };
2012-08-10 13:18:30 +00:00
}
return val.Split(',').Select(v => int.Parse(v));
2012-08-10 13:18:30 +00:00
}
}
2012-08-10 15:17:52 +00:00
private int? GetMaxAcceptedBitRate(string audioFormat)
2012-08-10 15:17:52 +00:00
{
int index = AudioFormats.ToList().IndexOf(audioFormat);
2012-08-10 15:17:52 +00:00
2012-08-11 18:12:34 +00:00
if (!AudioBitRates.Any())
{
return null;
}
return AudioBitRates.ElementAt(index);
2012-08-10 15:17:52 +00:00
}
/// <summary>
/// Determines whether or not the original file requires transcoding
/// </summary>
protected override bool RequiresConversion()
2012-08-10 15:17:52 +00:00
{
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)))
2012-08-10 15:17:52 +00:00
{
return true;
}
int? bitrate = GetMaxAcceptedBitRate(currentFormat);
2012-08-10 15:17:52 +00:00
// If the bitrate is greater than our desired bitrate, we need to transcode
if (bitrate.HasValue && bitrate.Value < LibraryItem.BitRate)
2012-08-10 15:17:52 +00:00
{
return true;
2012-08-10 15:17:52 +00:00
}
// If the number of channels is greater than our desired channels, we need to transcode
if (AudioChannels.HasValue && AudioChannels.Value < LibraryItem.Channels)
2012-08-10 15:17:52 +00:00
{
return true;
2012-08-10 15:17:52 +00:00
}
// If the sample rate is greater than our desired sample rate, we need to transcode
if (AudioSampleRate.HasValue && AudioSampleRate.Value < LibraryItem.SampleRate)
2012-08-10 15:17:52 +00:00
{
return true;
2012-08-10 15:17:52 +00:00
}
2012-08-10 15:17:52 +00:00
// Yay
return false;
}
/// <summary>
/// Gets the format we'll be converting to
/// </summary>
protected override string GetOutputFormat()
2012-08-10 15:17:52 +00:00
{
return AudioFormats.First();
2012-08-10 15:17:52 +00:00
}
/// <summary>
/// Creates arguments to pass to ffmpeg
/// </summary>
private string GetAudioArguments()
2012-08-10 15:17:52 +00:00
{
List<string> audioTranscodeParams = new List<string>();
string outputFormat = GetOutputFormat();
int? bitrate = GetMaxAcceptedBitRate(outputFormat);
if (bitrate.HasValue)
2012-08-10 15:17:52 +00:00
{
audioTranscodeParams.Add("-ab " + bitrate.Value);
2012-08-10 15:17:52 +00:00
}
if (AudioChannels.HasValue)
2012-08-10 15:17:52 +00:00
{
audioTranscodeParams.Add("-ac " + AudioChannels.Value);
2012-08-10 15:17:52 +00:00
}
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;
2012-08-10 15:17:52 +00:00
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();
}
2012-08-10 15:17:52 +00:00
}
2012-08-10 13:18:30 +00:00
}
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();
}
2012-08-10 13:18:30 +00:00
}