aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Api
diff options
context:
space:
mode:
Diffstat (limited to 'MediaBrowser.Api')
-rw-r--r--MediaBrowser.Api/MediaBrowser.Api.csproj1
-rw-r--r--MediaBrowser.Api/Playback/BaseStreamingService.cs6
-rw-r--r--MediaBrowser.Api/Playback/Hls/BaseHlsService.cs4
-rw-r--r--MediaBrowser.Api/Playback/Progressive/BaseProgressiveStreamingService.cs4
-rw-r--r--MediaBrowser.Api/Playback/Progressive/ProgressiveStreamWriter.cs2
-rw-r--r--MediaBrowser.Api/Plugin.cs303
-rw-r--r--MediaBrowser.Api/PluginService.cs22
-rw-r--r--MediaBrowser.Api/ServerEntryPoint.cs333
8 files changed, 370 insertions, 305 deletions
diff --git a/MediaBrowser.Api/MediaBrowser.Api.csproj b/MediaBrowser.Api/MediaBrowser.Api.csproj
index cf2a55695..97c399fbe 100644
--- a/MediaBrowser.Api/MediaBrowser.Api.csproj
+++ b/MediaBrowser.Api/MediaBrowser.Api.csproj
@@ -97,6 +97,7 @@
<Compile Include="PluginService.cs" />
<Compile Include="ScheduledTasks\ScheduledTaskService.cs" />
<Compile Include="ScheduledTasks\ScheduledTasksWebSocketListener.cs" />
+ <Compile Include="ServerEntryPoint.cs" />
<Compile Include="SystemService.cs" />
<Compile Include="UserLibrary\BaseItemsByNameService.cs" />
<Compile Include="UserLibrary\GenresService.cs" />
diff --git a/MediaBrowser.Api/Playback/BaseStreamingService.cs b/MediaBrowser.Api/Playback/BaseStreamingService.cs
index ce9fe0e0b..980856410 100644
--- a/MediaBrowser.Api/Playback/BaseStreamingService.cs
+++ b/MediaBrowser.Api/Playback/BaseStreamingService.cs
@@ -518,7 +518,7 @@ namespace MediaBrowser.Api.Playback
EnableRaisingEvents = true
};
- Plugin.Instance.OnTranscodeBeginning(outputPath, TranscodingJobType, process);
+ ServerEntryPoint.Instance.OnTranscodeBeginning(outputPath, TranscodingJobType, process);
//Logger.Info(process.StartInfo.FileName + " " + process.StartInfo.Arguments);
@@ -537,7 +537,7 @@ namespace MediaBrowser.Api.Playback
{
Logger.ErrorException("Error starting ffmpeg", ex);
- Plugin.Instance.OnTranscodeFailedToStart(outputPath, TranscodingJobType);
+ ServerEntryPoint.Instance.OnTranscodeFailedToStart(outputPath, TranscodingJobType);
state.LogFileStream.Dispose();
@@ -588,7 +588,7 @@ namespace MediaBrowser.Api.Playback
process.Dispose();
- Plugin.Instance.OnTranscodingFinished(outputFilePath, TranscodingJobType);
+ ServerEntryPoint.Instance.OnTranscodingFinished(outputFilePath, TranscodingJobType);
if (!exitCode.HasValue || exitCode.Value != 0)
{
diff --git a/MediaBrowser.Api/Playback/Hls/BaseHlsService.cs b/MediaBrowser.Api/Playback/Hls/BaseHlsService.cs
index 8c7edbe91..d616565ca 100644
--- a/MediaBrowser.Api/Playback/Hls/BaseHlsService.cs
+++ b/MediaBrowser.Api/Playback/Hls/BaseHlsService.cs
@@ -77,7 +77,7 @@ namespace MediaBrowser.Api.Playback.Hls
}
else
{
- Plugin.Instance.OnTranscodeBeginRequest(playlist, TranscodingJobType.Hls);
+ ServerEntryPoint.Instance.OnTranscodeBeginRequest(playlist, TranscodingJobType.Hls);
}
// Get the current playlist text and convert to bytes
@@ -94,7 +94,7 @@ namespace MediaBrowser.Api.Playback.Hls
}
finally
{
- Plugin.Instance.OnTranscodeEndRequest(playlist, TranscodingJobType.Hls);
+ ServerEntryPoint.Instance.OnTranscodeEndRequest(playlist, TranscodingJobType.Hls);
}
}
diff --git a/MediaBrowser.Api/Playback/Progressive/BaseProgressiveStreamingService.cs b/MediaBrowser.Api/Playback/Progressive/BaseProgressiveStreamingService.cs
index 08e66c387..bbfe65504 100644
--- a/MediaBrowser.Api/Playback/Progressive/BaseProgressiveStreamingService.cs
+++ b/MediaBrowser.Api/Playback/Progressive/BaseProgressiveStreamingService.cs
@@ -107,7 +107,7 @@ namespace MediaBrowser.Api.Playback.Progressive
var outputPath = GetOutputFilePath(state);
- if (File.Exists(outputPath) && !Plugin.Instance.HasActiveTranscodingJob(outputPath, TranscodingJobType.Progressive))
+ if (File.Exists(outputPath) && !ServerEntryPoint.Instance.HasActiveTranscodingJob(outputPath, TranscodingJobType.Progressive))
{
return ToStaticFileResult(outputPath);
}
@@ -133,7 +133,7 @@ namespace MediaBrowser.Api.Playback.Progressive
}
else
{
- Plugin.Instance.OnTranscodeBeginRequest(outputPath, TranscodingJobType.Progressive);
+ ServerEntryPoint.Instance.OnTranscodeBeginRequest(outputPath, TranscodingJobType.Progressive);
}
return new ProgressiveStreamWriter
diff --git a/MediaBrowser.Api/Playback/Progressive/ProgressiveStreamWriter.cs b/MediaBrowser.Api/Playback/Progressive/ProgressiveStreamWriter.cs
index 9ac93694b..b6b73c78d 100644
--- a/MediaBrowser.Api/Playback/Progressive/ProgressiveStreamWriter.cs
+++ b/MediaBrowser.Api/Playback/Progressive/ProgressiveStreamWriter.cs
@@ -41,7 +41,7 @@ namespace MediaBrowser.Api.Playback.Progressive
}
finally
{
- Plugin.Instance.OnTranscodeEndRequest(Path, TranscodingJobType.Progressive);
+ ServerEntryPoint.Instance.OnTranscodeEndRequest(Path, TranscodingJobType.Progressive);
}
}
diff --git a/MediaBrowser.Api/Plugin.cs b/MediaBrowser.Api/Plugin.cs
index 58c0ffcfa..b8c81bbe1 100644
--- a/MediaBrowser.Api/Plugin.cs
+++ b/MediaBrowser.Api/Plugin.cs
@@ -1,12 +1,7 @@
-using MediaBrowser.Common.Plugins;
+using MediaBrowser.Common.Kernel;
+using MediaBrowser.Common.Plugins;
using MediaBrowser.Model.Plugins;
-using System;
-using System.Collections.Generic;
-using System.ComponentModel;
-using System.Diagnostics;
-using System.Linq;
-using System.Threading;
-using System.Threading.Tasks;
+using MediaBrowser.Model.Serialization;
namespace MediaBrowser.Api
{
@@ -16,6 +11,16 @@ namespace MediaBrowser.Api
public class Plugin : BasePlugin<BasePluginConfiguration>
{
/// <summary>
+ /// Initializes a new instance of the <see cref="Plugin" /> class.
+ /// </summary>
+ /// <param name="kernel">The kernel.</param>
+ /// <param name="xmlSerializer">The XML serializer.</param>
+ public Plugin(IKernel kernel, IXmlSerializer xmlSerializer) : base(kernel, xmlSerializer)
+ {
+ Instance = this;
+ }
+
+ /// <summary>
/// Gets the name of the plugin
/// </summary>
/// <value>The name.</value>
@@ -41,287 +46,5 @@ namespace MediaBrowser.Api
/// </summary>
/// <value>The instance.</value>
public static Plugin Instance { get; private set; }
-
- /// <summary>
- /// Initializes a new instance of the <see cref="Plugin" /> class.
- /// </summary>
- public Plugin()
- {
- Instance = this;
- }
-
- /// <summary>
- /// Releases unmanaged and - optionally - managed resources.
- /// </summary>
- /// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
- protected override void DisposeOnServer(bool dispose)
- {
- if (dispose)
- {
- var jobCount = ActiveTranscodingJobs.Count;
-
- Parallel.ForEach(ActiveTranscodingJobs, OnTranscodeKillTimerStopped);
-
- // Try to allow for some time to kill the ffmpeg processes and delete the partial stream files
- if (jobCount > 0)
- {
- Thread.Sleep(1000);
- }
- }
-
- base.DisposeOnServer(dispose);
- }
-
- /// <summary>
- /// The active transcoding jobs
- /// </summary>
- private readonly List<TranscodingJob> ActiveTranscodingJobs = new List<TranscodingJob>();
-
- /// <summary>
- /// Called when [transcode beginning].
- /// </summary>
- /// <param name="path">The path.</param>
- /// <param name="type">The type.</param>
- /// <param name="process">The process.</param>
- public void OnTranscodeBeginning(string path, TranscodingJobType type, Process process)
- {
- lock (ActiveTranscodingJobs)
- {
- ActiveTranscodingJobs.Add(new TranscodingJob
- {
- Type = type,
- Path = path,
- Process = process,
- ActiveRequestCount = 1
- });
- }
- }
-
- /// <summary>
- /// Called when [transcode failed to start].
- /// </summary>
- /// <param name="path">The path.</param>
- /// <param name="type">The type.</param>
- public void OnTranscodeFailedToStart(string path, TranscodingJobType type)
- {
- lock (ActiveTranscodingJobs)
- {
- var job = ActiveTranscodingJobs.First(j => j.Type == type && j.Path.Equals(path, StringComparison.OrdinalIgnoreCase));
-
- ActiveTranscodingJobs.Remove(job);
- }
- }
-
- /// <summary>
- /// Determines whether [has active transcoding job] [the specified path].
- /// </summary>
- /// <param name="path">The path.</param>
- /// <param name="type">The type.</param>
- /// <returns><c>true</c> if [has active transcoding job] [the specified path]; otherwise, <c>false</c>.</returns>
- public bool HasActiveTranscodingJob(string path, TranscodingJobType type)
- {
- lock (ActiveTranscodingJobs)
- {
- return ActiveTranscodingJobs.Any(j => j.Type == type && j.Path.Equals(path, StringComparison.OrdinalIgnoreCase));
- }
- }
-
- /// <summary>
- /// Called when [transcode begin request].
- /// </summary>
- /// <param name="path">The path.</param>
- /// <param name="type">The type.</param>
- public void OnTranscodeBeginRequest(string path, TranscodingJobType type)
- {
- lock (ActiveTranscodingJobs)
- {
- var job = ActiveTranscodingJobs.FirstOrDefault(j => j.Type == type && j.Path.Equals(path, StringComparison.OrdinalIgnoreCase));
-
- if (job == null)
- {
- return;
- }
-
- job.ActiveRequestCount++;
-
- if (job.KillTimer != null)
- {
- job.KillTimer.Dispose();
- job.KillTimer = null;
- }
- }
- }
-
- /// <summary>
- /// Called when [transcode end request].
- /// </summary>
- /// <param name="path">The path.</param>
- /// <param name="type">The type.</param>
- public void OnTranscodeEndRequest(string path, TranscodingJobType type)
- {
- lock (ActiveTranscodingJobs)
- {
- var job = ActiveTranscodingJobs.FirstOrDefault(j => j.Type == type && j.Path.Equals(path, StringComparison.OrdinalIgnoreCase));
-
- if (job == null)
- {
- return;
- }
-
- job.ActiveRequestCount--;
-
- if (job.ActiveRequestCount == 0)
- {
- var timerDuration = type == TranscodingJobType.Progressive ? 1000 : 30000;
-
- if (job.KillTimer == null)
- {
- job.KillTimer = new Timer(OnTranscodeKillTimerStopped, job, timerDuration, Timeout.Infinite);
- }
- else
- {
- job.KillTimer.Change(timerDuration, Timeout.Infinite);
- }
- }
- }
- }
-
- /// <summary>
- /// Called when [transcoding finished].
- /// </summary>
- /// <param name="path">The path.</param>
- /// <param name="type">The type.</param>
- public void OnTranscodingFinished(string path, TranscodingJobType type)
- {
- lock (ActiveTranscodingJobs)
- {
- var job = ActiveTranscodingJobs.FirstOrDefault(j => j.Type == type && j.Path.Equals(path, StringComparison.OrdinalIgnoreCase));
-
- if (job == null)
- {
- return;
- }
-
- ActiveTranscodingJobs.Remove(job);
-
- if (job.KillTimer != null)
- {
- job.KillTimer.Dispose();
- job.KillTimer = null;
- }
- }
- }
-
- /// <summary>
- /// Called when [transcode kill timer stopped].
- /// </summary>
- /// <param name="state">The state.</param>
- private void OnTranscodeKillTimerStopped(object state)
- {
- var job = (TranscodingJob)state;
-
- lock (ActiveTranscodingJobs)
- {
- ActiveTranscodingJobs.Remove(job);
-
- if (job.KillTimer != null)
- {
- job.KillTimer.Dispose();
- job.KillTimer = null;
- }
- }
-
- var process = job.Process;
-
- var hasExited = true;
-
- try
- {
- hasExited = process.HasExited;
- }
- catch (Win32Exception ex)
- {
- Logger.ErrorException("Error determining if ffmpeg process has exited for {0}", ex, job.Path);
- }
- catch (InvalidOperationException ex)
- {
- Logger.ErrorException("Error determining if ffmpeg process has exited for {0}", ex, job.Path);
- }
- catch (NotSupportedException ex)
- {
- Logger.ErrorException("Error determining if ffmpeg process has exited for {0}", ex, job.Path);
- }
-
- if (hasExited)
- {
- return;
- }
-
- try
- {
- Logger.Info("Killing ffmpeg process for {0}", job.Path);
-
- process.Kill();
- }
- catch (Win32Exception ex)
- {
- Logger.ErrorException("Error killing transcoding job for {0}", ex, job.Path);
- }
- catch (InvalidOperationException ex)
- {
- Logger.ErrorException("Error killing transcoding job for {0}", ex, job.Path);
- }
- catch (NotSupportedException ex)
- {
- Logger.ErrorException("Error killing transcoding job for {0}", ex, job.Path);
- }
- }
- }
-
- /// <summary>
- /// Class TranscodingJob
- /// </summary>
- public class TranscodingJob
- {
- /// <summary>
- /// Gets or sets the path.
- /// </summary>
- /// <value>The path.</value>
- public string Path { get; set; }
- /// <summary>
- /// Gets or sets the type.
- /// </summary>
- /// <value>The type.</value>
- public TranscodingJobType Type { get; set; }
- /// <summary>
- /// Gets or sets the process.
- /// </summary>
- /// <value>The process.</value>
- public Process Process { get; set; }
- /// <summary>
- /// Gets or sets the active request count.
- /// </summary>
- /// <value>The active request count.</value>
- public int ActiveRequestCount { get; set; }
- /// <summary>
- /// Gets or sets the kill timer.
- /// </summary>
- /// <value>The kill timer.</value>
- public Timer KillTimer { get; set; }
- }
-
- /// <summary>
- /// Enum TranscodingJobType
- /// </summary>
- public enum TranscodingJobType
- {
- /// <summary>
- /// The progressive
- /// </summary>
- Progressive,
- /// <summary>
- /// The HLS
- /// </summary>
- Hls
}
}
diff --git a/MediaBrowser.Api/PluginService.cs b/MediaBrowser.Api/PluginService.cs
index 06db7a276..7e907c2dd 100644
--- a/MediaBrowser.Api/PluginService.cs
+++ b/MediaBrowser.Api/PluginService.cs
@@ -1,5 +1,6 @@
using MediaBrowser.Common.Extensions;
using MediaBrowser.Common.Implementations.HttpServer;
+using MediaBrowser.Common.Kernel;
using MediaBrowser.Controller;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Plugins;
@@ -124,11 +125,17 @@ namespace MediaBrowser.Api
private readonly IJsonSerializer _jsonSerializer;
/// <summary>
+ /// The _app host
+ /// </summary>
+ private readonly IApplicationHost _appHost;
+
+ /// <summary>
/// Initializes a new instance of the <see cref="PluginService" /> class.
/// </summary>
/// <param name="jsonSerializer">The json serializer.</param>
+ /// <param name="appHost">The app host.</param>
/// <exception cref="System.ArgumentNullException">jsonSerializer</exception>
- public PluginService(IJsonSerializer jsonSerializer)
+ public PluginService(IJsonSerializer jsonSerializer, IApplicationHost appHost)
: base()
{
if (jsonSerializer == null)
@@ -136,6 +143,7 @@ namespace MediaBrowser.Api
throw new ArgumentNullException("jsonSerializer");
}
+ _appHost = appHost;
_jsonSerializer = jsonSerializer;
}
@@ -146,7 +154,7 @@ namespace MediaBrowser.Api
/// <returns>System.Object.</returns>
public object Get(GetPlugins request)
{
- var result = Kernel.Plugins.OrderBy(p => p.Name).Select(p => p.GetPluginInfo()).ToList();
+ var result = _appHost.Plugins.OrderBy(p => p.Name).Select(p => p.GetPluginInfo()).ToList();
return ToOptimizedResult(result);
}
@@ -158,7 +166,7 @@ namespace MediaBrowser.Api
/// <returns>System.Object.</returns>
public object Get(GetPluginAssembly request)
{
- var plugin = Kernel.Plugins.First(p => p.Id == request.Id);
+ var plugin = _appHost.Plugins.First(p => p.Id == request.Id);
return ToStaticFileResult(plugin.AssemblyFilePath);
}
@@ -170,7 +178,7 @@ namespace MediaBrowser.Api
/// <returns>System.Object.</returns>
public object Get(GetPluginConfiguration request)
{
- var plugin = Kernel.Plugins.First(p => p.Id == request.Id);
+ var plugin = _appHost.Plugins.First(p => p.Id == request.Id);
var dateModified = plugin.ConfigurationDateLastModified;
@@ -186,7 +194,7 @@ namespace MediaBrowser.Api
/// <returns>System.Object.</returns>
public object Get(GetPluginConfigurationFile request)
{
- var plugin = Kernel.Plugins.First(p => p.Id == request.Id);
+ var plugin = _appHost.Plugins.First(p => p.Id == request.Id);
return ToStaticFileResult(plugin.ConfigurationFilePath);
}
@@ -235,7 +243,7 @@ namespace MediaBrowser.Api
var pathInfo = PathInfo.Parse(Request.PathInfo);
var id = new Guid(pathInfo.GetArgumentValue<string>(1));
- var plugin = Kernel.Plugins.First(p => p.Id == id);
+ var plugin = _appHost.Plugins.First(p => p.Id == id);
var configuration = _jsonSerializer.DeserializeFromStream(request.RequestStream, plugin.ConfigurationType) as BasePluginConfiguration;
@@ -250,7 +258,7 @@ namespace MediaBrowser.Api
{
var kernel = (Kernel)Kernel;
- var plugin = kernel.Plugins.First(p => p.Id == request.Id);
+ var plugin = _appHost.Plugins.First(p => p.Id == request.Id);
kernel.InstallationManager.UninstallPlugin(plugin);
}
diff --git a/MediaBrowser.Api/ServerEntryPoint.cs b/MediaBrowser.Api/ServerEntryPoint.cs
new file mode 100644
index 000000000..663d1aeca
--- /dev/null
+++ b/MediaBrowser.Api/ServerEntryPoint.cs
@@ -0,0 +1,333 @@
+using MediaBrowser.Controller.Plugins;
+using MediaBrowser.Model.Logging;
+using System;
+using System.Collections.Generic;
+using System.ComponentModel;
+using System.Diagnostics;
+using System.Linq;
+using System.Threading;
+using System.Threading.Tasks;
+
+namespace MediaBrowser.Api
+{
+ /// <summary>
+ /// Class ServerEntryPoint
+ /// </summary>
+ public class ServerEntryPoint : IServerEntryPoint
+ {
+ /// <summary>
+ /// The instance
+ /// </summary>
+ public static ServerEntryPoint Instance;
+
+ /// <summary>
+ /// Gets or sets the logger.
+ /// </summary>
+ /// <value>The logger.</value>
+ private ILogger Logger { get; set; }
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="ServerEntryPoint" /> class.
+ /// </summary>
+ /// <param name="logger">The logger.</param>
+ public ServerEntryPoint(ILogger logger)
+ {
+ Logger = logger;
+
+ Instance = this;
+ }
+
+ /// <summary>
+ /// Runs this instance.
+ /// </summary>
+ public void Run()
+ {
+ }
+
+ /// <summary>
+ /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
+ /// </summary>
+ public void Dispose()
+ {
+ Dispose(true);
+ GC.SuppressFinalize(this);
+ }
+
+ /// <summary>
+ /// Releases unmanaged and - optionally - managed resources.
+ /// </summary>
+ /// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
+ protected virtual void Dispose(bool dispose)
+ {
+ var jobCount = ActiveTranscodingJobs.Count;
+
+ Parallel.ForEach(ActiveTranscodingJobs, OnTranscodeKillTimerStopped);
+
+ // Try to allow for some time to kill the ffmpeg processes and delete the partial stream files
+ if (jobCount > 0)
+ {
+ Thread.Sleep(1000);
+ }
+ }
+
+ /// <summary>
+ /// The active transcoding jobs
+ /// </summary>
+ private readonly List<TranscodingJob> ActiveTranscodingJobs = new List<TranscodingJob>();
+
+ /// <summary>
+ /// Called when [transcode beginning].
+ /// </summary>
+ /// <param name="path">The path.</param>
+ /// <param name="type">The type.</param>
+ /// <param name="process">The process.</param>
+ public void OnTranscodeBeginning(string path, TranscodingJobType type, Process process)
+ {
+ lock (ActiveTranscodingJobs)
+ {
+ ActiveTranscodingJobs.Add(new TranscodingJob
+ {
+ Type = type,
+ Path = path,
+ Process = process,
+ ActiveRequestCount = 1
+ });
+ }
+ }
+
+ /// <summary>
+ /// <summary>
+ /// The progressive
+ /// </summary>
+ /// Called when [transcode failed to start].
+ /// </summary>
+ /// <param name="path">The path.</param>
+ /// <param name="type">The type.</param>
+ public void OnTranscodeFailedToStart(string path, TranscodingJobType type)
+ {
+ lock (ActiveTranscodingJobs)
+ {
+ var job = ActiveTranscodingJobs.First(j => j.Type == type && j.Path.Equals(path, StringComparison.OrdinalIgnoreCase));
+
+ ActiveTranscodingJobs.Remove(job);
+ }
+ }
+
+ /// <summary>
+ /// Determines whether [has active transcoding job] [the specified path].
+ /// </summary>
+ /// <param name="path">The path.</param>
+ /// <param name="type">The type.</param>
+ /// <returns><c>true</c> if [has active transcoding job] [the specified path]; otherwise, <c>false</c>.</returns>
+ public bool HasActiveTranscodingJob(string path, TranscodingJobType type)
+ {
+ lock (ActiveTranscodingJobs)
+ {
+ return ActiveTranscodingJobs.Any(j => j.Type == type && j.Path.Equals(path, StringComparison.OrdinalIgnoreCase));
+ }
+ }
+
+ /// <summary>
+ /// Called when [transcode begin request].
+ /// </summary>
+ /// <param name="path">The path.</param>
+ /// <param name="type">The type.</param>
+ public void OnTranscodeBeginRequest(string path, TranscodingJobType type)
+ {
+ lock (ActiveTranscodingJobs)
+ {
+ var job = ActiveTranscodingJobs.FirstOrDefault(j => j.Type == type && j.Path.Equals(path, StringComparison.OrdinalIgnoreCase));
+
+ if (job == null)
+ {
+ return;
+ }
+
+ job.ActiveRequestCount++;
+
+ if (job.KillTimer != null)
+ {
+ job.KillTimer.Dispose();
+ job.KillTimer = null;
+ }
+ }
+ }
+
+ /// <summary>
+ /// Called when [transcode end request].
+ /// </summary>
+ /// <param name="path">The path.</param>
+ /// <param name="type">The type.</param>
+ public void OnTranscodeEndRequest(string path, TranscodingJobType type)
+ {
+ lock (ActiveTranscodingJobs)
+ {
+ var job = ActiveTranscodingJobs.FirstOrDefault(j => j.Type == type && j.Path.Equals(path, StringComparison.OrdinalIgnoreCase));
+
+ if (job == null)
+ {
+ return;
+ }
+
+ job.ActiveRequestCount--;
+
+ if (job.ActiveRequestCount == 0)
+ {
+ var timerDuration = type == TranscodingJobType.Progressive ? 1000 : 30000;
+
+ if (job.KillTimer == null)
+ {
+ job.KillTimer = new Timer(OnTranscodeKillTimerStopped, job, timerDuration, Timeout.Infinite);
+ }
+ else
+ {
+ job.KillTimer.Change(timerDuration, Timeout.Infinite);
+ }
+ }
+ }
+ }
+
+ /// <summary>
+ /// Called when [transcoding finished].
+ /// </summary>
+ /// <param name="path">The path.</param>
+ /// <param name="type">The type.</param>
+ public void OnTranscodingFinished(string path, TranscodingJobType type)
+ {
+ lock (ActiveTranscodingJobs)
+ {
+ var job = ActiveTranscodingJobs.FirstOrDefault(j => j.Type == type && j.Path.Equals(path, StringComparison.OrdinalIgnoreCase));
+
+ if (job == null)
+ {
+ return;
+ }
+
+ ActiveTranscodingJobs.Remove(job);
+
+ if (job.KillTimer != null)
+ {
+ job.KillTimer.Dispose();
+ job.KillTimer = null;
+ }
+ }
+ }
+
+ /// <summary>
+ /// Called when [transcode kill timer stopped].
+ /// </summary>
+ /// <param name="state">The state.</param>
+ private void OnTranscodeKillTimerStopped(object state)
+ {
+ var job = (TranscodingJob)state;
+
+ lock (ActiveTranscodingJobs)
+ {
+ ActiveTranscodingJobs.Remove(job);
+
+ if (job.KillTimer != null)
+ {
+ job.KillTimer.Dispose();
+ job.KillTimer = null;
+ }
+ }
+
+ var process = job.Process;
+
+ var hasExited = true;
+
+ try
+ {
+ hasExited = process.HasExited;
+ }
+ catch (Win32Exception ex)
+ {
+ Logger.ErrorException("Error determining if ffmpeg process has exited for {0}", ex, job.Path);
+ }
+ catch (InvalidOperationException ex)
+ {
+ Logger.ErrorException("Error determining if ffmpeg process has exited for {0}", ex, job.Path);
+ }
+ catch (NotSupportedException ex)
+ {
+ Logger.ErrorException("Error determining if ffmpeg process has exited for {0}", ex, job.Path);
+ }
+
+ if (hasExited)
+ {
+ return;
+ }
+
+ try
+ {
+ Logger.Info("Killing ffmpeg process for {0}", job.Path);
+
+ process.Kill();
+ }
+ catch (Win32Exception ex)
+ {
+ Logger.ErrorException("Error killing transcoding job for {0}", ex, job.Path);
+ }
+ catch (InvalidOperationException ex)
+ {
+ Logger.ErrorException("Error killing transcoding job for {0}", ex, job.Path);
+ }
+ catch (NotSupportedException ex)
+ {
+ Logger.ErrorException("Error killing transcoding job for {0}", ex, job.Path);
+ }
+ }
+
+ }
+
+ /// <summary>
+ /// Class TranscodingJob
+ /// </summary>
+ public class TranscodingJob
+ {
+ /// <summary>
+ /// Gets or sets the path.
+ /// </summary>
+ /// <value>The path.</value>
+ public string Path { get; set; }
+ /// <summary>
+ /// Gets or sets the type.
+ /// </summary>
+ /// <value>The type.</value>
+ public TranscodingJobType Type { get; set; }
+ /// <summary>
+ /// Gets or sets the process.
+ /// </summary>
+ /// <value>The process.</value>
+ public Process Process { get; set; }
+ /// <summary>
+ /// Gets or sets the active request count.
+ /// </summary>
+ /// <value>The active request count.</value>
+ public int ActiveRequestCount { get; set; }
+ /// <summary>
+ /// <summary>
+ /// Enum TranscodingJobType
+ /// </summary>
+ /// <summary>
+ /// Gets or sets the kill timer.
+ /// </summary>
+ /// <value>The kill timer.</value>
+ public Timer KillTimer { get; set; }
+ }
+
+ /// <summary>
+ /// Enum TranscodingJobType
+ /// </summary>
+ public enum TranscodingJobType
+ {
+ /// <summary>
+ /// The progressive
+ /// </summary>
+ Progressive,
+ /// <summary>
+ /// The HLS
+ /// </summary>
+ Hls
+ }
+}