2012-08-12 17:41:40 +02:00
|
|
|
|
using System;
|
2012-08-12 19:05:51 +02:00
|
|
|
|
using System.Collections.Generic;
|
2012-08-12 17:41:40 +02:00
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.IO;
|
2012-08-12 19:05:51 +02:00
|
|
|
|
using System.Linq;
|
2012-08-12 17:41:40 +02:00
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using MediaBrowser.Common.Logging;
|
|
|
|
|
using MediaBrowser.Common.Net;
|
|
|
|
|
using MediaBrowser.Common.Net.Handlers;
|
|
|
|
|
using MediaBrowser.Controller;
|
|
|
|
|
using MediaBrowser.Model.Entities;
|
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Api.HttpHandlers
|
|
|
|
|
{
|
|
|
|
|
public abstract class BaseMediaHandler<T> : BaseHandler
|
|
|
|
|
where T : BaseItem, new()
|
|
|
|
|
{
|
2012-08-12 19:05:51 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Supported values: mp3,flac,ogg,wav,asf,wma,aac
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected virtual IEnumerable<string> OutputFormats
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return QueryString["outputformats"].Split(',');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// These formats can be outputted directly but cannot be encoded to
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected virtual IEnumerable<string> UnsupportedOutputEncodingFormats
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return new string[] { };
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-12 17:41:40 +02:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-19 22:38:31 +02:00
|
|
|
|
public override Task<string> GetContentType()
|
2012-08-12 17:41:40 +02:00
|
|
|
|
{
|
2012-08-22 04:50:59 +02:00
|
|
|
|
return Task.FromResult<string>(MimeTypes.GetMimeType("." + GetConversionOutputFormat()));
|
2012-08-12 17:41:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
2012-08-19 22:38:31 +02:00
|
|
|
|
public override bool ShouldCompressResponse(string contentType)
|
2012-08-12 17:41:40 +02:00
|
|
|
|
{
|
2012-08-19 22:38:31 +02:00
|
|
|
|
return false;
|
2012-08-12 17:41:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
2012-08-22 04:50:59 +02:00
|
|
|
|
public override Task ProcessRequest(HttpListenerContext ctx)
|
2012-08-12 17:41:40 +02:00
|
|
|
|
{
|
|
|
|
|
HttpListenerContext = ctx;
|
|
|
|
|
|
|
|
|
|
if (!RequiresConversion())
|
|
|
|
|
{
|
2012-08-22 04:50:59 +02:00
|
|
|
|
return new StaticFileHandler() { Path = LibraryItem.Path }.ProcessRequest(ctx);
|
2012-08-15 15:20:29 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2012-08-22 04:50:59 +02:00
|
|
|
|
return base.ProcessRequest(ctx);
|
2012-08-12 17:41:40 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected abstract string GetCommandLineArguments();
|
2012-08-12 19:05:51 +02:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the format we'll be converting to
|
|
|
|
|
/// </summary>
|
|
|
|
|
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;
|
|
|
|
|
}
|
2012-08-12 17:41:40 +02:00
|
|
|
|
|
|
|
|
|
protected async override Task WriteResponseToOutputStream(Stream stream)
|
|
|
|
|
{
|
|
|
|
|
ProcessStartInfo startInfo = new ProcessStartInfo();
|
|
|
|
|
|
|
|
|
|
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 22:47:10 +02:00
|
|
|
|
startInfo.FileName = Kernel.Instance.ApplicationPaths.FFMpegPath;
|
|
|
|
|
startInfo.WorkingDirectory = Kernel.Instance.ApplicationPaths.FFMpegDirectory;
|
2012-08-12 17:41:40 +02:00
|
|
|
|
startInfo.Arguments = GetCommandLineArguments();
|
|
|
|
|
|
|
|
|
|
Logger.LogInfo(startInfo.FileName + " " + startInfo.Arguments);
|
|
|
|
|
|
|
|
|
|
Process process = new Process();
|
|
|
|
|
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-18 22:38:02 +02:00
|
|
|
|
FileStream logStream = new FileStream(Path.Combine(Kernel.Instance.ApplicationPaths.LogDirectoryPath, "ffmpeg-" + Guid.NewGuid().ToString() + ".txt"), FileMode.Create);
|
2012-08-12 17:41:40 +02:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
process.Start();
|
|
|
|
|
|
|
|
|
|
// MUST read both stdout and stderr asynchronously or a deadlock may occurr
|
|
|
|
|
// If we ever decide to disable the ffmpeg log then you must uncomment the below line.
|
|
|
|
|
//process.BeginErrorReadLine();
|
|
|
|
|
|
2012-08-22 04:50:59 +02:00
|
|
|
|
Task debugLogTask = process.StandardError.BaseStream.CopyToAsync(logStream);
|
2012-08-12 17:41:40 +02:00
|
|
|
|
|
2012-08-22 04:50:59 +02:00
|
|
|
|
await process.StandardOutput.BaseStream.CopyToAsync(stream).ConfigureAwait(false);
|
2012-08-12 17:41:40 +02:00
|
|
|
|
|
|
|
|
|
process.WaitForExit();
|
|
|
|
|
|
|
|
|
|
Logger.LogInfo("FFMpeg exited with code " + process.ExitCode);
|
|
|
|
|
|
2012-08-22 04:50:59 +02:00
|
|
|
|
await debugLogTask.ConfigureAwait(false);
|
2012-08-12 17:41:40 +02:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Logger.LogException(ex);
|
|
|
|
|
|
|
|
|
|
// Hate having to do this
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
process.Kill();
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
logStream.Dispose();
|
|
|
|
|
process.Dispose();
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-08-13 22:18:02 +02: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 17:41:40 +02:00
|
|
|
|
}
|
|
|
|
|
}
|