2012-08-10 13:18:30 +00:00
|
|
|
|
using System;
|
2012-08-10 15:17:52 +00:00
|
|
|
|
using System.Collections.Generic;
|
2012-08-11 03:13:56 +00:00
|
|
|
|
using System.Diagnostics;
|
2012-08-10 15:17:52 +00:00
|
|
|
|
using System.IO;
|
2012-08-11 03:13:56 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net;
|
|
|
|
|
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
|
|
|
|
|
{
|
2012-08-11 03:13:56 +00:00
|
|
|
|
public class AudioHandler : BaseHandler
|
2012-08-10 13:18:30 +00:00
|
|
|
|
{
|
2012-08-10 15:17:52 +00:00
|
|
|
|
private Audio _LibraryItem;
|
2012-08-10 13:18:30 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the library item that will be played, if any
|
|
|
|
|
/// </summary>
|
2012-08-10 15:17:52 +00:00
|
|
|
|
private Audio LibraryItem
|
2012-08-10 13:18:30 +00:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_LibraryItem == null)
|
|
|
|
|
{
|
|
|
|
|
string id = QueryString["id"];
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(id))
|
|
|
|
|
{
|
2012-08-10 15:17:52 +00:00
|
|
|
|
_LibraryItem = Kernel.Instance.GetItemById(Guid.Parse(id)) as Audio;
|
2012-08-10 13:18:30 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return _LibraryItem;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-11 03:13:56 +00:00
|
|
|
|
public override bool CompressResponse
|
2012-08-10 13:18:30 +00:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2012-08-11 03:13:56 +00:00
|
|
|
|
return false;
|
2012-08-10 15:17:52 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-11 03:13:56 +00:00
|
|
|
|
protected override bool IsAsyncHandler
|
2012-08-10 15:17:52 +00:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2012-08-11 03:13:56 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-08-10 15:17:52 +00:00
|
|
|
|
|
2012-08-11 03:13:56 +00:00
|
|
|
|
public override string ContentType
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return MimeTypes.GetMimeType("." + GetOutputFormat());
|
2012-08-10 15:17:52 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-11 03:13:56 +00:00
|
|
|
|
public IEnumerable<string> AudioFormats
|
2012-08-10 15:17:52 +00:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2012-08-11 03:13:56 +00:00
|
|
|
|
string val = QueryString["audioformat"];
|
2012-08-10 15:17:52 +00:00
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(val))
|
|
|
|
|
{
|
2012-08-11 03:13:56 +00:00
|
|
|
|
return new string[] { "mp3" };
|
2012-08-10 15:17:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-08-11 03:13:56 +00:00
|
|
|
|
return val.Split(',');
|
2012-08-10 15:17:52 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int? AudioBitRate
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
string val = QueryString["audiobitrate"];
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(val))
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return int.Parse(val);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-11 03:13:56 +00:00
|
|
|
|
public int? AudioChannels
|
2012-08-10 15:17:52 +00:00
|
|
|
|
{
|
|
|
|
|
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))
|
2012-08-10 13:18:30 +00:00
|
|
|
|
{
|
2012-08-10 15:17:52 +00:00
|
|
|
|
return 44100;
|
2012-08-10 13:18:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-08-10 15:17:52 +00:00
|
|
|
|
return int.Parse(val);
|
2012-08-10 13:18:30 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2012-08-10 15:17:52 +00:00
|
|
|
|
|
2012-08-11 03:13:56 +00:00
|
|
|
|
public override void ProcessRequest(HttpListenerContext ctx)
|
2012-08-10 15:17:52 +00:00
|
|
|
|
{
|
2012-08-11 03:13:56 +00:00
|
|
|
|
HttpListenerContext = ctx;
|
2012-08-10 15:17:52 +00:00
|
|
|
|
|
2012-08-11 03:13:56 +00:00
|
|
|
|
if (!RequiresTranscoding())
|
2012-08-10 15:17:52 +00:00
|
|
|
|
{
|
2012-08-11 03:13:56 +00:00
|
|
|
|
new StaticFileHandler() { Path = LibraryItem.Path }.ProcessRequest(ctx);
|
|
|
|
|
return;
|
2012-08-10 15:17:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-08-11 03:13:56 +00:00
|
|
|
|
base.ProcessRequest(ctx);
|
2012-08-10 15:17:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Determines whether or not the original file requires transcoding
|
|
|
|
|
/// </summary>
|
|
|
|
|
private bool RequiresTranscoding()
|
|
|
|
|
{
|
2012-08-11 03:13:56 +00:00
|
|
|
|
// If it's not in a format the consumer accepts, return true
|
|
|
|
|
if (!AudioFormats.Any(f => LibraryItem.Path.EndsWith(f, StringComparison.OrdinalIgnoreCase)))
|
2012-08-10 15:17:52 +00:00
|
|
|
|
{
|
|
|
|
|
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
|
2012-08-11 03:13:56 +00:00
|
|
|
|
if (AudioChannels.HasValue)
|
2012-08-10 15:17:52 +00:00
|
|
|
|
{
|
2012-08-11 03:13:56 +00:00
|
|
|
|
if (AudioChannels.Value < LibraryItem.Channels)
|
2012-08-10 15:17:52 +00:00
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-08-11 03:13:56 +00:00
|
|
|
|
|
2012-08-10 15:17:52 +00:00
|
|
|
|
// Yay
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-11 03:13:56 +00:00
|
|
|
|
private string GetOutputFormat()
|
2012-08-10 15:17:52 +00:00
|
|
|
|
{
|
2012-08-11 03:13:56 +00:00
|
|
|
|
string format = AudioFormats.FirstOrDefault(f => LibraryItem.Path.EndsWith(f, StringComparison.OrdinalIgnoreCase));
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(format))
|
2012-08-10 15:17:52 +00:00
|
|
|
|
{
|
2012-08-11 03:13:56 +00:00
|
|
|
|
return format;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return AudioFormats.First();
|
2012-08-10 15:17:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates arguments to pass to ffmpeg
|
|
|
|
|
/// </summary>
|
2012-08-11 03:13:56 +00:00
|
|
|
|
private string GetAudioArguments()
|
2012-08-10 15:17:52 +00:00
|
|
|
|
{
|
|
|
|
|
List<string> audioTranscodeParams = new List<string>();
|
|
|
|
|
|
|
|
|
|
if (AudioBitRate.HasValue)
|
|
|
|
|
{
|
|
|
|
|
audioTranscodeParams.Add("-ab " + AudioBitRate.Value);
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-11 03:13:56 +00:00
|
|
|
|
if (AudioChannels.HasValue)
|
2012-08-10 15:17:52 +00:00
|
|
|
|
{
|
2012-08-11 03:13:56 +00:00
|
|
|
|
audioTranscodeParams.Add("-ac " + AudioChannels.Value);
|
2012-08-10 15:17:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (AudioSampleRate.HasValue)
|
|
|
|
|
{
|
|
|
|
|
audioTranscodeParams.Add("-ar " + AudioSampleRate.Value);
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-11 03:13:56 +00:00
|
|
|
|
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;
|
2012-08-10 15:17:52 +00:00
|
|
|
|
|
2012-08-11 03:13:56 +00:00
|
|
|
|
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();
|
|
|
|
|
}
|
2012-08-10 15:17:52 +00:00
|
|
|
|
}
|
2012-08-10 13:18:30 +00:00
|
|
|
|
}
|
|
|
|
|
}
|