2012-09-08 14:52:13 +00:00
|
|
|
|
using MediaBrowser.Common.Logging;
|
|
|
|
|
using MediaBrowser.Common.Net;
|
|
|
|
|
using MediaBrowser.Common.Net.Handlers;
|
|
|
|
|
using MediaBrowser.Controller;
|
2012-09-11 01:34:02 +00:00
|
|
|
|
using MediaBrowser.Controller.Entities;
|
2012-09-08 14:52:13 +00:00
|
|
|
|
using System;
|
2012-08-12 17:05:51 +00:00
|
|
|
|
using System.Collections.Generic;
|
2012-08-12 15:41:40 +00:00
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.IO;
|
2012-08-12 17:05:51 +00:00
|
|
|
|
using System.Linq;
|
2012-08-12 15:41:40 +00:00
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Api.HttpHandlers
|
|
|
|
|
{
|
2012-09-09 13:56:04 +00:00
|
|
|
|
public abstract class BaseMediaHandler<TBaseItemType, TOutputType> : BaseHandler
|
|
|
|
|
where TBaseItemType : BaseItem, new()
|
2012-08-12 15:41:40 +00:00
|
|
|
|
{
|
2012-08-12 17:05:51 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Supported values: mp3,flac,ogg,wav,asf,wma,aac
|
|
|
|
|
/// </summary>
|
2012-09-09 13:56:04 +00:00
|
|
|
|
protected virtual IEnumerable<TOutputType> OutputFormats
|
2012-08-12 17:05:51 +00:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2012-09-09 13:56:04 +00:00
|
|
|
|
return QueryString["outputformats"].Split(',').Select(o => (TOutputType)Enum.Parse(typeof(TOutputType), o, true));
|
2012-08-12 17:05:51 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// These formats can be outputted directly but cannot be encoded to
|
|
|
|
|
/// </summary>
|
2012-09-09 13:56:04 +00:00
|
|
|
|
protected virtual IEnumerable<TOutputType> UnsupportedOutputEncodingFormats
|
2012-08-12 17:05:51 +00:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2012-09-09 13:56:04 +00:00
|
|
|
|
return new TOutputType[] { };
|
2012-08-12 17:05:51 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-11 18:20:12 +00:00
|
|
|
|
private TBaseItemType _libraryItem;
|
2012-08-12 15:41:40 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the library item that will be played, if any
|
|
|
|
|
/// </summary>
|
2012-09-09 13:56:04 +00:00
|
|
|
|
protected TBaseItemType LibraryItem
|
2012-08-12 15:41:40 +00:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2012-09-11 18:20:12 +00:00
|
|
|
|
if (_libraryItem == null)
|
2012-08-12 15:41:40 +00:00
|
|
|
|
{
|
|
|
|
|
string id = QueryString["id"];
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(id))
|
|
|
|
|
{
|
2012-09-11 18:20:12 +00:00
|
|
|
|
_libraryItem = Kernel.Instance.GetItemById(Guid.Parse(id)) as TBaseItemType;
|
2012-08-12 15:41:40 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-11 18:20:12 +00:00
|
|
|
|
return _libraryItem;
|
2012-08-12 15:41:40 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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))
|
|
|
|
|
{
|
2012-09-09 15:15:22 +00:00
|
|
|
|
return 44100;
|
2012-08-12 15:41:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return int.Parse(val);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-19 16:51:37 +00:00
|
|
|
|
protected override Task<ResponseInfo> GetResponseInfo()
|
2012-08-12 15:41:40 +00:00
|
|
|
|
{
|
2012-09-19 16:51:37 +00:00
|
|
|
|
ResponseInfo info = new ResponseInfo
|
|
|
|
|
{
|
|
|
|
|
ContentType = MimeTypes.GetMimeType("." + GetConversionOutputFormat()),
|
|
|
|
|
CompressResponse = false
|
|
|
|
|
};
|
2012-08-12 15:41:40 +00:00
|
|
|
|
|
2012-09-19 16:51:37 +00:00
|
|
|
|
return Task.FromResult<ResponseInfo>(info);
|
2012-08-12 15:41:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-08-22 02:50:59 +00:00
|
|
|
|
public override Task ProcessRequest(HttpListenerContext ctx)
|
2012-08-12 15:41:40 +00:00
|
|
|
|
{
|
|
|
|
|
HttpListenerContext = ctx;
|
|
|
|
|
|
|
|
|
|
if (!RequiresConversion())
|
|
|
|
|
{
|
2012-09-11 18:20:12 +00:00
|
|
|
|
return new StaticFileHandler { Path = LibraryItem.Path }.ProcessRequest(ctx);
|
2012-08-12 15:41:40 +00:00
|
|
|
|
}
|
2012-09-11 18:20:12 +00:00
|
|
|
|
|
|
|
|
|
return base.ProcessRequest(ctx);
|
2012-08-12 15:41:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected abstract string GetCommandLineArguments();
|
2012-08-12 17:05:51 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the format we'll be converting to
|
|
|
|
|
/// </summary>
|
2012-09-09 13:56:04 +00:00
|
|
|
|
protected virtual TOutputType GetConversionOutputFormat()
|
2012-08-12 17:05:51 +00:00
|
|
|
|
{
|
2012-09-09 13:56:04 +00:00
|
|
|
|
return OutputFormats.First(f => !UnsupportedOutputEncodingFormats.Any(s => s.ToString().Equals(f.ToString(), StringComparison.OrdinalIgnoreCase)));
|
2012-08-12 17:05:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected virtual bool RequiresConversion()
|
|
|
|
|
{
|
|
|
|
|
string currentFormat = Path.GetExtension(LibraryItem.Path).Replace(".", string.Empty);
|
|
|
|
|
|
2012-09-09 13:56:04 +00:00
|
|
|
|
if (OutputFormats.Any(f => currentFormat.EndsWith(f.ToString(), StringComparison.OrdinalIgnoreCase)))
|
2012-08-12 17:05:51 +00:00
|
|
|
|
{
|
|
|
|
|
// We can output these files directly, but we can't encode them
|
2012-09-09 13:56:04 +00:00
|
|
|
|
if (UnsupportedOutputEncodingFormats.Any(f => currentFormat.EndsWith(f.ToString(), StringComparison.OrdinalIgnoreCase)))
|
2012-08-12 17:05:51 +00:00
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// If it's not in a format the consumer accepts, return true
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2012-08-12 15:41:40 +00:00
|
|
|
|
|
2012-08-24 02:29:27 +00:00
|
|
|
|
private FileStream LogFileStream { get; set; }
|
|
|
|
|
|
2012-08-12 15:41:40 +00:00
|
|
|
|
protected async override Task WriteResponseToOutputStream(Stream stream)
|
|
|
|
|
{
|
2012-09-11 19:37:14 +00:00
|
|
|
|
var startInfo = new ProcessStartInfo{};
|
2012-08-12 15:41:40 +00:00
|
|
|
|
|
|
|
|
|
startInfo.CreateNoWindow = true;
|
|
|
|
|
|
|
|
|
|
startInfo.UseShellExecute = false;
|
|
|
|
|
|
|
|
|
|
// Must consume both or ffmpeg may hang due to deadlocks. See comments below.
|
|
|
|
|
startInfo.RedirectStandardOutput = true;
|
|
|
|
|
startInfo.RedirectStandardError = true;
|
|
|
|
|
|
2012-08-18 20:47:10 +00:00
|
|
|
|
startInfo.FileName = Kernel.Instance.ApplicationPaths.FFMpegPath;
|
|
|
|
|
startInfo.WorkingDirectory = Kernel.Instance.ApplicationPaths.FFMpegDirectory;
|
2012-08-12 15:41:40 +00:00
|
|
|
|
startInfo.Arguments = GetCommandLineArguments();
|
|
|
|
|
|
|
|
|
|
Logger.LogInfo(startInfo.FileName + " " + startInfo.Arguments);
|
|
|
|
|
|
2012-09-11 19:37:14 +00:00
|
|
|
|
var process = new Process{};
|
2012-08-12 15:41:40 +00:00
|
|
|
|
process.StartInfo = startInfo;
|
|
|
|
|
|
|
|
|
|
// FFMpeg writes debug/error info to stderr. This is useful when debugging so let's put it in the log directory.
|
2012-08-24 02:29:27 +00:00
|
|
|
|
LogFileStream = new FileStream(Path.Combine(Kernel.Instance.ApplicationPaths.LogDirectoryPath, "ffmpeg-" + Guid.NewGuid().ToString() + ".txt"), FileMode.Create);
|
|
|
|
|
|
|
|
|
|
process.EnableRaisingEvents = true;
|
|
|
|
|
|
2012-09-11 18:20:12 +00:00
|
|
|
|
process.Exited += ProcessExited;
|
2012-08-12 15:41:40 +00:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
process.Start();
|
|
|
|
|
|
|
|
|
|
// MUST read both stdout and stderr asynchronously or a deadlock may occurr
|
|
|
|
|
|
2012-08-24 02:29:27 +00:00
|
|
|
|
// Kick off two tasks
|
|
|
|
|
Task mediaTask = process.StandardOutput.BaseStream.CopyToAsync(stream);
|
|
|
|
|
Task debugLogTask = process.StandardError.BaseStream.CopyToAsync(LogFileStream);
|
2012-08-12 15:41:40 +00:00
|
|
|
|
|
2012-08-24 02:29:27 +00:00
|
|
|
|
await mediaTask.ConfigureAwait(false);
|
|
|
|
|
//await debugLogTask.ConfigureAwait(false);
|
2012-08-12 15:41:40 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Logger.LogException(ex);
|
|
|
|
|
|
|
|
|
|
// Hate having to do this
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
process.Kill();
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-08-24 02:29:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-09-11 18:20:12 +00:00
|
|
|
|
void ProcessExited(object sender, EventArgs e)
|
2012-08-24 02:29:27 +00:00
|
|
|
|
{
|
|
|
|
|
if (LogFileStream != null)
|
2012-08-12 15:41:40 +00:00
|
|
|
|
{
|
2012-08-24 02:29:27 +00:00
|
|
|
|
LogFileStream.Dispose();
|
2012-08-12 15:41:40 +00:00
|
|
|
|
}
|
2012-08-24 02:29:27 +00:00
|
|
|
|
|
2012-09-11 19:37:14 +00:00
|
|
|
|
var process = sender as Process;
|
2012-08-24 02:29:27 +00:00
|
|
|
|
|
|
|
|
|
Logger.LogInfo("FFMpeg exited with code " + process.ExitCode);
|
|
|
|
|
|
|
|
|
|
process.Dispose();
|
2012-08-12 15:41:40 +00:00
|
|
|
|
}
|
2012-08-13 20:18:02 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the number of audio channels to specify on the command line
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected int? GetNumAudioChannelsParam(int libraryItemChannels)
|
|
|
|
|
{
|
|
|
|
|
// If the user requested a max number of channels
|
|
|
|
|
if (AudioChannels.HasValue)
|
|
|
|
|
{
|
|
|
|
|
// Only specify the param if we're going to downmix
|
|
|
|
|
if (AudioChannels.Value < libraryItemChannels)
|
|
|
|
|
{
|
|
|
|
|
return AudioChannels.Value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the number of audio channels to specify on the command line
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected int? GetSampleRateParam(int libraryItemSampleRate)
|
|
|
|
|
{
|
|
|
|
|
// If the user requested a max value
|
|
|
|
|
if (AudioSampleRate.HasValue)
|
|
|
|
|
{
|
|
|
|
|
// Only specify the param if we're going to downmix
|
|
|
|
|
if (AudioSampleRate.Value < libraryItemSampleRate)
|
|
|
|
|
{
|
|
|
|
|
return AudioSampleRate.Value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2012-08-12 15:41:40 +00:00
|
|
|
|
}
|
|
|
|
|
}
|