jellyfin/MediaBrowser.Api/ApiEntryPoint.cs

778 lines
25 KiB
C#
Raw Normal View History

using MediaBrowser.Api.Playback;
2014-12-21 20:40:37 +01:00
using MediaBrowser.Common.Configuration;
using MediaBrowser.Common.IO;
2014-12-21 20:40:37 +01:00
using MediaBrowser.Controller.Configuration;
2015-04-20 20:04:02 +02:00
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Plugins;
using MediaBrowser.Controller.Session;
2014-12-21 20:40:37 +01:00
using MediaBrowser.Model.Configuration;
using MediaBrowser.Model.Logging;
2014-06-20 06:50:30 +02:00
using MediaBrowser.Model.Session;
using System;
using System.Collections.Generic;
using System.Diagnostics;
2013-06-12 23:47:02 +02:00
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace MediaBrowser.Api
{
/// <summary>
/// Class ServerEntryPoint
/// </summary>
2013-03-08 20:14:09 +01:00
public class ApiEntryPoint : IServerEntryPoint
{
/// <summary>
/// The instance
/// </summary>
2013-03-08 20:14:09 +01:00
public static ApiEntryPoint Instance;
/// <summary>
/// Gets or sets the logger.
/// </summary>
/// <value>The logger.</value>
private ILogger Logger { get; set; }
2013-12-22 19:58:51 +01:00
/// <summary>
/// The application paths
/// </summary>
2014-12-21 20:40:37 +01:00
private readonly IServerConfigurationManager _config;
2013-12-22 19:58:51 +01:00
private readonly ISessionManager _sessionManager;
private readonly IFileSystem _fileSystem;
2015-04-20 20:04:02 +02:00
private readonly IMediaSourceManager _mediaSourceManager;
2014-07-08 03:41:03 +02:00
public readonly SemaphoreSlim TranscodingStartLock = new SemaphoreSlim(1, 1);
2014-07-02 20:34:08 +02:00
/// <summary>
2013-03-08 20:14:09 +01:00
/// Initializes a new instance of the <see cref="ApiEntryPoint" /> class.
/// </summary>
/// <param name="logger">The logger.</param>
2014-06-26 19:04:11 +02:00
/// <param name="sessionManager">The session manager.</param>
2014-12-21 20:40:37 +01:00
/// <param name="config">The configuration.</param>
2015-04-20 20:04:02 +02:00
/// <param name="fileSystem">The file system.</param>
/// <param name="mediaSourceManager">The media source manager.</param>
public ApiEntryPoint(ILogger logger, ISessionManager sessionManager, IServerConfigurationManager config, IFileSystem fileSystem, IMediaSourceManager mediaSourceManager)
{
Logger = logger;
_sessionManager = sessionManager;
2014-12-21 20:40:37 +01:00
_config = config;
_fileSystem = fileSystem;
2015-04-20 20:04:02 +02:00
_mediaSourceManager = mediaSourceManager;
Instance = this;
}
/// <summary>
/// Runs this instance.
/// </summary>
public void Run()
{
try
{
DeleteEncodedMediaCache();
}
2013-12-22 19:58:51 +01:00
catch (DirectoryNotFoundException)
{
// Don't clutter the log
}
catch (IOException ex)
{
Logger.ErrorException("Error deleting encoded media cache", ex);
}
}
2014-12-21 20:40:37 +01:00
public EncodingOptions GetEncodingOptions()
{
return _config.GetConfiguration<EncodingOptions>("encoding");
}
/// <summary>
/// Deletes the encoded media cache.
/// </summary>
private void DeleteEncodedMediaCache()
{
2015-01-17 05:29:53 +01:00
var path = _config.ApplicationPaths.TranscodingTempPath;
2015-01-02 06:36:27 +01:00
foreach (var file in Directory.EnumerateFiles(path, "*", SearchOption.AllDirectories)
.ToList())
{
_fileSystem.DeleteFile(file);
}
}
/// <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)
{
2013-04-10 17:32:09 +02:00
var jobCount = _activeTranscodingJobs.Count;
2015-04-20 20:35:33 +02:00
Parallel.ForEach(_activeTranscodingJobs.ToList(), j => KillTranscodingJob(j, false, path => true));
// 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>
2013-04-10 17:32:09 +02:00
private readonly List<TranscodingJob> _activeTranscodingJobs = new List<TranscodingJob>();
/// <summary>
/// Called when [transcode beginning].
/// </summary>
/// <param name="path">The path.</param>
2015-03-29 20:31:28 +02:00
/// <param name="playSessionId">The play session identifier.</param>
2015-04-20 20:04:02 +02:00
/// <param name="liveStreamId">The live stream identifier.</param>
2014-09-03 04:30:24 +02:00
/// <param name="transcodingJobId">The transcoding job identifier.</param>
/// <param name="type">The type.</param>
/// <param name="process">The process.</param>
2013-10-03 16:14:40 +02:00
/// <param name="deviceId">The device id.</param>
/// <param name="state">The state.</param>
2014-06-02 21:32:41 +02:00
/// <param name="cancellationTokenSource">The cancellation token source.</param>
2014-09-03 04:30:24 +02:00
/// <returns>TranscodingJob.</returns>
public TranscodingJob OnTranscodeBeginning(string path,
2015-03-29 20:31:28 +02:00
string playSessionId,
2015-04-20 20:04:02 +02:00
string liveStreamId,
2014-09-03 04:30:24 +02:00
string transcodingJobId,
TranscodingJobType type,
Process process,
string deviceId,
StreamState state,
CancellationTokenSource cancellationTokenSource)
{
2013-04-10 17:32:09 +02:00
lock (_activeTranscodingJobs)
{
2015-04-13 21:14:37 +02:00
var job = new TranscodingJob(Logger)
{
Type = type,
Path = path,
Process = process,
2013-06-12 04:59:38 +02:00
ActiveRequestCount = 1,
2014-06-02 21:32:41 +02:00
DeviceId = deviceId,
2014-09-03 04:30:24 +02:00
CancellationTokenSource = cancellationTokenSource,
2015-03-19 17:16:33 +01:00
Id = transcodingJobId,
2015-04-20 20:04:02 +02:00
PlaySessionId = playSessionId,
LiveStreamId = liveStreamId
2014-09-03 04:30:24 +02:00
};
_activeTranscodingJobs.Add(job);
ReportTranscodingProgress(job, state, null, null, null, null);
2014-09-03 04:30:24 +02:00
return job;
}
}
2014-09-03 04:30:24 +02:00
public void ReportTranscodingProgress(TranscodingJob job, StreamState state, TimeSpan? transcodingPosition, float? framerate, double? percentComplete, long? bytesTranscoded)
{
2014-09-03 04:30:24 +02:00
var ticks = transcodingPosition.HasValue ? transcodingPosition.Value.Ticks : (long?)null;
if (job != null)
{
job.Framerate = framerate;
job.CompletionPercentage = percentComplete;
job.TranscodingPositionTicks = ticks;
job.BytesTranscoded = bytesTranscoded;
}
var deviceId = state.Request.DeviceId;
if (!string.IsNullOrWhiteSpace(deviceId))
{
2015-03-29 20:16:40 +02:00
var audioCodec = state.ActualOutputAudioCodec;
2014-10-20 05:04:45 +02:00
var videoCodec = state.ActualOutputVideoCodec;
_sessionManager.ReportTranscodingInfo(deviceId, new TranscodingInfo
{
Bitrate = state.TotalOutputBitrate,
AudioCodec = audioCodec,
VideoCodec = videoCodec,
Container = state.OutputContainer,
Framerate = framerate,
2014-06-06 06:56:47 +02:00
CompletionPercentage = percentComplete,
Width = state.OutputWidth,
Height = state.OutputHeight,
2014-12-19 05:20:07 +01:00
AudioChannels = state.OutputAudioChannels,
IsAudioDirect = string.Equals(state.OutputAudioCodec, "copy", StringComparison.OrdinalIgnoreCase),
IsVideoDirect = string.Equals(state.OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase)
});
}
}
/// <summary>
/// <summary>
/// The progressive
/// </summary>
/// Called when [transcode failed to start].
/// </summary>
/// <param name="path">The path.</param>
/// <param name="type">The type.</param>
/// <param name="state">The state.</param>
public void OnTranscodeFailedToStart(string path, TranscodingJobType type, StreamState state)
{
2013-04-10 17:32:09 +02:00
lock (_activeTranscodingJobs)
{
2015-05-27 19:50:40 +02:00
var job = _activeTranscodingJobs.First(j => j.Type == type && string.Equals(j.Path, path, StringComparison.OrdinalIgnoreCase));
2013-04-10 17:32:09 +02:00
_activeTranscodingJobs.Remove(job);
}
if (!string.IsNullOrWhiteSpace(state.Request.DeviceId))
{
_sessionManager.ClearTranscodingInfo(state.Request.DeviceId);
}
}
/// <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)
2014-06-26 19:04:11 +02:00
{
return GetTranscodingJob(path, type) != null;
}
2015-05-19 21:15:40 +02:00
public TranscodingJob GetTranscodingJob(string path, TranscodingJobType type)
2014-09-03 04:30:24 +02:00
{
lock (_activeTranscodingJobs)
{
2015-05-27 19:50:40 +02:00
return _activeTranscodingJobs.FirstOrDefault(j => j.Type == type && string.Equals(j.Path, path, StringComparison.OrdinalIgnoreCase));
2014-09-03 04:30:24 +02:00
}
}
/// <summary>
/// Called when [transcode begin request].
/// </summary>
/// <param name="path">The path.</param>
/// <param name="type">The type.</param>
2014-09-03 04:30:24 +02:00
public TranscodingJob OnTranscodeBeginRequest(string path, TranscodingJobType type)
{
2013-04-10 17:32:09 +02:00
lock (_activeTranscodingJobs)
{
2015-05-27 19:50:40 +02:00
var job = _activeTranscodingJobs.FirstOrDefault(j => j.Type == type && string.Equals(j.Path, path, StringComparison.OrdinalIgnoreCase));
if (job == null)
{
2014-09-03 04:30:24 +02:00
return null;
}
2015-03-25 19:29:21 +01:00
OnTranscodeBeginRequest(job);
2014-09-03 04:30:24 +02:00
return job;
}
}
2015-03-25 19:29:21 +01:00
public void OnTranscodeBeginRequest(TranscodingJob job)
{
job.ActiveRequestCount++;
2015-04-11 00:16:41 +02:00
if (string.IsNullOrWhiteSpace(job.PlaySessionId) || job.Type == TranscodingJobType.Progressive)
{
2015-04-13 21:14:37 +02:00
job.StopKillTimer();
2015-04-11 00:16:41 +02:00
}
2015-03-25 19:29:21 +01:00
}
2015-04-02 03:34:14 +02:00
2014-09-03 04:30:24 +02:00
public void OnTranscodeEndRequest(TranscodingJob job)
{
2014-09-03 04:30:24 +02:00
job.ActiveRequestCount--;
2015-04-11 00:16:41 +02:00
Logger.Debug("OnTranscodeEndRequest job.ActiveRequestCount={0}", job.ActiveRequestCount);
if (job.ActiveRequestCount <= 0)
{
2015-04-11 00:16:41 +02:00
PingTimer(job, false);
2015-04-02 03:34:14 +02:00
}
}
2015-04-13 21:14:37 +02:00
internal void PingTranscodingJob(string playSessionId)
2015-04-02 03:34:14 +02:00
{
2015-04-13 21:14:37 +02:00
if (string.IsNullOrEmpty(playSessionId))
2015-04-02 03:34:14 +02:00
{
2015-04-13 21:14:37 +02:00
throw new ArgumentNullException("playSessionId");
2015-04-02 03:34:14 +02:00
}
2015-04-13 21:14:37 +02:00
Logger.Debug("PingTranscodingJob PlaySessionId={0}", playSessionId);
2015-04-02 03:34:14 +02:00
var jobs = new List<TranscodingJob>();
2014-09-18 06:50:21 +02:00
2015-04-02 03:34:14 +02:00
lock (_activeTranscodingJobs)
{
// This is really only needed for HLS.
// Progressive streams can stop on their own reliably
2015-04-13 21:14:37 +02:00
jobs = jobs.Where(j => string.Equals(playSessionId, j.PlaySessionId, StringComparison.OrdinalIgnoreCase)).ToList();
2015-04-02 03:34:14 +02:00
}
foreach (var job in jobs)
{
2015-04-11 00:16:41 +02:00
PingTimer(job, true);
2015-04-02 03:34:14 +02:00
}
}
2015-04-20 20:04:02 +02:00
private async void PingTimer(TranscodingJob job, bool isProgressCheckIn)
2015-04-02 03:34:14 +02:00
{
2015-04-13 21:14:37 +02:00
if (job.HasExited)
{
job.StopKillTimer();
return;
}
2015-05-22 17:59:17 +02:00
var timerDuration = 1000;
2015-04-02 03:34:14 +02:00
2015-05-22 17:59:17 +02:00
if (job.Type != TranscodingJobType.Progressive)
2015-04-09 19:30:18 +02:00
{
2015-05-22 17:59:17 +02:00
timerDuration = 1800000;
2015-05-27 19:50:40 +02:00
2015-05-22 17:59:17 +02:00
// We can really reduce the timeout for apps that are using the newer api
if (!string.IsNullOrWhiteSpace(job.PlaySessionId))
{
timerDuration = 60000;
}
2015-04-09 19:30:18 +02:00
}
2015-04-19 21:17:17 +02:00
job.PingTimeout = timerDuration;
job.LastPingDate = DateTime.UtcNow;
2015-04-13 21:14:37 +02:00
// Don't start the timer for playback checkins with progressive streaming
if (job.Type != TranscodingJobType.Progressive || !isProgressCheckIn)
2015-04-02 03:34:14 +02:00
{
2015-04-19 21:17:17 +02:00
job.StartKillTimer(OnTranscodeKillTimerStopped);
}
2015-04-02 03:34:14 +02:00
else
{
2015-04-19 21:17:17 +02:00
job.ChangeKillTimerIfStarted();
2015-04-02 03:34:14 +02:00
}
2015-04-20 20:04:02 +02:00
if (!string.IsNullOrWhiteSpace(job.LiveStreamId))
{
try
{
await _mediaSourceManager.PingLiveStream(job.LiveStreamId, CancellationToken.None).ConfigureAwait(false);
}
catch (Exception ex)
{
Logger.ErrorException("Error closing live stream", ex);
}
}
}
/// <summary>
/// Called when [transcode kill timer stopped].
/// </summary>
/// <param name="state">The state.</param>
private void OnTranscodeKillTimerStopped(object state)
{
var job = (TranscodingJob)state;
2015-04-19 21:17:17 +02:00
if (!job.HasExited && job.Type != TranscodingJobType.Progressive)
{
2015-04-20 20:04:02 +02:00
var timeSinceLastPing = (DateTime.UtcNow - job.LastPingDate).TotalMilliseconds;
if (timeSinceLastPing < job.PingTimeout)
2015-04-19 21:17:17 +02:00
{
2015-04-20 20:04:02 +02:00
job.StartKillTimer(OnTranscodeKillTimerStopped, job.PingTimeout);
2015-04-19 21:17:17 +02:00
return;
}
}
2015-04-13 21:14:37 +02:00
Logger.Debug("Transcoding kill timer stopped for JobId {0} PlaySessionId {1}. Killing transcoding", job.Id, job.PlaySessionId);
2015-04-20 20:35:33 +02:00
KillTranscodingJob(job, true, path => true);
}
/// <summary>
/// Kills the single transcoding job.
/// </summary>
2013-10-03 16:14:40 +02:00
/// <param name="deviceId">The device id.</param>
2015-03-29 20:31:28 +02:00
/// <param name="playSessionId">The play session identifier.</param>
2014-07-22 03:29:06 +02:00
/// <param name="deleteFiles">The delete files.</param>
2014-07-08 03:41:03 +02:00
/// <returns>Task.</returns>
2015-03-29 20:31:28 +02:00
internal void KillTranscodingJobs(string deviceId, string playSessionId, Func<string, bool> deleteFiles)
{
2015-03-19 17:16:33 +01:00
KillTranscodingJobs(j =>
{
2015-04-13 21:14:37 +02:00
if (!string.IsNullOrWhiteSpace(playSessionId))
2015-03-19 17:16:33 +01:00
{
2015-04-13 21:14:37 +02:00
return string.Equals(playSessionId, j.PlaySessionId, StringComparison.OrdinalIgnoreCase);
2015-03-19 17:16:33 +01:00
}
2015-04-13 21:14:37 +02:00
return string.Equals(deviceId, j.DeviceId, StringComparison.OrdinalIgnoreCase);
2015-03-19 17:16:33 +01:00
}, deleteFiles);
}
2014-06-28 21:35:30 +02:00
/// <summary>
/// Kills the transcoding jobs.
/// </summary>
2014-07-22 03:29:06 +02:00
/// <param name="killJob">The kill job.</param>
/// <param name="deleteFiles">The delete files.</param>
2014-07-08 03:41:03 +02:00
/// <returns>Task.</returns>
2015-03-19 17:16:33 +01:00
private void KillTranscodingJobs(Func<TranscodingJob, bool> killJob, Func<string, bool> deleteFiles)
2014-06-28 21:35:30 +02:00
{
var jobs = new List<TranscodingJob>();
lock (_activeTranscodingJobs)
{
// This is really only needed for HLS.
// Progressive streams can stop on their own reliably
2014-07-22 03:29:06 +02:00
jobs.AddRange(_activeTranscodingJobs.Where(killJob));
2014-06-28 21:35:30 +02:00
}
2014-07-02 20:34:08 +02:00
if (jobs.Count == 0)
2014-06-28 21:35:30 +02:00
{
2014-07-02 20:34:08 +02:00
return;
}
2014-10-07 01:58:46 +02:00
foreach (var job in jobs)
2014-07-02 20:34:08 +02:00
{
2015-04-20 20:35:33 +02:00
KillTranscodingJob(job, false, deleteFiles);
2014-06-28 21:35:30 +02:00
}
}
/// <summary>
/// Kills the transcoding job.
/// </summary>
/// <param name="job">The job.</param>
2015-04-20 20:35:33 +02:00
/// <param name="closeLiveStream">if set to <c>true</c> [close live stream].</param>
2014-07-08 03:41:03 +02:00
/// <param name="delete">The delete.</param>
2015-04-20 20:35:33 +02:00
private async void KillTranscodingJob(TranscodingJob job, bool closeLiveStream, Func<string, bool> delete)
{
2015-04-13 21:14:37 +02:00
job.DisposeKillTimer();
Logger.Debug("KillTranscodingJob - JobId {0} PlaySessionId {1}. Killing transcoding", job.Id, job.PlaySessionId);
2015-05-27 19:50:40 +02:00
2013-04-10 17:32:09 +02:00
lock (_activeTranscodingJobs)
{
2013-04-10 17:32:09 +02:00
_activeTranscodingJobs.Remove(job);
2014-06-02 21:32:41 +02:00
if (!job.CancellationTokenSource.IsCancellationRequested)
{
job.CancellationTokenSource.Cancel();
}
}
2014-06-20 06:50:30 +02:00
lock (job.ProcessLock)
{
2015-04-11 00:16:41 +02:00
if (job.TranscodingThrottler != null)
2013-06-10 06:00:44 +02:00
{
2015-04-11 00:16:41 +02:00
job.TranscodingThrottler.Stop();
2013-06-10 06:00:44 +02:00
}
2014-06-20 06:50:30 +02:00
2015-04-11 00:16:41 +02:00
var process = job.Process;
var hasExited = job.HasExited;
2014-06-20 06:50:30 +02:00
if (!hasExited)
2013-06-10 06:00:44 +02:00
{
2014-06-20 06:50:30 +02:00
try
{
Logger.Info("Killing ffmpeg process for {0}", job.Path);
//process.Kill();
process.StandardInput.WriteLine("q");
// Need to wait because killing is asynchronous
process.WaitForExit(5000);
}
catch (Exception ex)
{
Logger.ErrorException("Error killing transcoding job for {0}", ex, job.Path);
}
2013-06-10 06:00:44 +02:00
}
}
2014-07-08 03:41:03 +02:00
if (delete(job.Path))
2014-06-20 06:50:30 +02:00
{
DeletePartialStreamFiles(job.Path, job.Type, 0, 1500);
}
2015-04-20 20:35:33 +02:00
if (closeLiveStream && !string.IsNullOrWhiteSpace(job.LiveStreamId))
{
try
{
await _mediaSourceManager.CloseLiveStream(job.LiveStreamId, CancellationToken.None).ConfigureAwait(false);
}
catch (Exception ex)
{
Logger.ErrorException("Error closing live stream for {0}", ex, job.Path);
}
}
2014-01-03 05:58:22 +01:00
}
2013-06-10 06:00:44 +02:00
2014-01-03 05:58:22 +01:00
private async void DeletePartialStreamFiles(string path, TranscodingJobType jobType, int retryCount, int delayMs)
{
2014-06-29 04:30:20 +02:00
if (retryCount >= 10)
2014-01-03 05:58:22 +01:00
{
return;
}
2013-06-10 06:00:44 +02:00
2014-01-03 05:58:22 +01:00
Logger.Info("Deleting partial stream file(s) {0}", path);
await Task.Delay(delayMs).ConfigureAwait(false);
try
{
if (jobType == TranscodingJobType.Progressive)
2013-06-10 06:00:44 +02:00
{
2014-01-03 05:58:22 +01:00
DeleteProgressivePartialStreamFiles(path);
2013-06-10 06:00:44 +02:00
}
2014-01-03 05:58:22 +01:00
else
2013-06-10 06:00:44 +02:00
{
2014-01-03 05:58:22 +01:00
DeleteHlsPartialStreamFiles(path);
2013-06-10 06:00:44 +02:00
}
2014-09-09 03:15:31 +02:00
}
catch (DirectoryNotFoundException)
{
}
catch (FileNotFoundException)
{
}
2014-01-03 05:58:22 +01:00
catch (IOException ex)
{
Logger.ErrorException("Error deleting partial stream file(s) {0}", ex, path);
DeletePartialStreamFiles(path, jobType, retryCount + 1, 500);
}
catch (Exception ex)
{
Logger.ErrorException("Error deleting partial stream file(s) {0}", ex, path);
}
}
2013-06-10 06:00:44 +02:00
/// <summary>
/// Deletes the progressive partial stream files.
/// </summary>
/// <param name="outputFilePath">The output file path.</param>
private void DeleteProgressivePartialStreamFiles(string outputFilePath)
{
_fileSystem.DeleteFile(outputFilePath);
2013-06-10 06:00:44 +02:00
}
/// <summary>
/// Deletes the HLS partial stream files.
/// </summary>
/// <param name="outputFilePath">The output file path.</param>
private void DeleteHlsPartialStreamFiles(string outputFilePath)
{
var directory = Path.GetDirectoryName(outputFilePath);
var name = Path.GetFileNameWithoutExtension(outputFilePath);
var filesToDelete = Directory.EnumerateFiles(directory)
.Where(f => f.IndexOf(name, StringComparison.OrdinalIgnoreCase) != -1)
.ToList();
2014-06-28 21:35:30 +02:00
Exception e = null;
2015-03-25 19:29:21 +01:00
2013-06-10 06:00:44 +02:00
foreach (var file in filesToDelete)
{
try
{
Logger.Info("Deleting HLS file {0}", file);
_fileSystem.DeleteFile(file);
2014-09-09 03:15:31 +02:00
}
catch (DirectoryNotFoundException)
{
}
catch (FileNotFoundException)
{
2014-10-07 01:58:46 +02:00
2013-06-10 06:00:44 +02:00
}
catch (IOException ex)
{
2014-06-28 21:35:30 +02:00
e = ex;
2013-06-10 06:00:44 +02:00
Logger.ErrorException("Error deleting HLS file {0}", ex, file);
}
}
2014-06-28 21:35:30 +02:00
if (e != null)
{
throw e;
}
2013-06-10 06:00:44 +02:00
}
}
/// <summary>
/// Class TranscodingJob
/// </summary>
public class TranscodingJob
{
2015-03-19 17:16:33 +01:00
/// <summary>
2015-03-29 20:31:28 +02:00
/// Gets or sets the play session identifier.
2015-03-19 17:16:33 +01:00
/// </summary>
2015-03-29 20:31:28 +02:00
/// <value>The play session identifier.</value>
public string PlaySessionId { get; set; }
/// <summary>
2015-04-20 20:04:02 +02:00
/// Gets or sets the live stream identifier.
/// </summary>
/// <value>The live stream identifier.</value>
public string LiveStreamId { get; set; }
2015-05-22 17:59:17 +02:00
public bool IsLiveOutput { get; set; }
2015-04-20 20:04:02 +02:00
/// <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; }
2015-04-13 21:14:37 +02:00
public ILogger Logger { get; private 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>
2015-04-13 21:14:37 +02:00
private Timer KillTimer { get; set; }
2013-06-12 04:59:38 +02:00
2013-10-03 16:14:40 +02:00
public string DeviceId { get; set; }
2014-06-02 21:32:41 +02:00
public CancellationTokenSource CancellationTokenSource { get; set; }
2014-06-20 06:50:30 +02:00
public object ProcessLock = new object();
2014-06-26 19:04:11 +02:00
public bool HasExited { get; set; }
2014-09-03 04:30:24 +02:00
public string Id { get; set; }
public float? Framerate { get; set; }
public double? CompletionPercentage { get; set; }
public long? BytesDownloaded { get; set; }
public long? BytesTranscoded { get; set; }
2014-10-07 01:58:46 +02:00
2014-09-03 04:30:24 +02:00
public long? TranscodingPositionTicks { get; set; }
public long? DownloadPositionTicks { get; set; }
2014-09-18 06:50:21 +02:00
2015-03-25 02:34:34 +01:00
public TranscodingThrottler TranscodingThrottler { get; set; }
2015-04-13 21:14:37 +02:00
private readonly object _timerLock = new object();
2015-04-19 21:17:17 +02:00
public DateTime LastPingDate { get; set; }
public int PingTimeout { get; set; }
2015-04-13 21:14:37 +02:00
public TranscodingJob(ILogger logger)
{
Logger = logger;
}
public void StopKillTimer()
{
lock (_timerLock)
{
if (KillTimer != null)
{
KillTimer.Change(Timeout.Infinite, Timeout.Infinite);
}
}
}
2014-09-18 06:50:21 +02:00
public void DisposeKillTimer()
{
2015-04-13 21:14:37 +02:00
lock (_timerLock)
{
if (KillTimer != null)
{
KillTimer.Dispose();
KillTimer = null;
}
}
}
2015-04-19 21:17:17 +02:00
public void StartKillTimer(TimerCallback callback)
2015-04-20 20:04:02 +02:00
{
StartKillTimer(callback, PingTimeout);
}
public void StartKillTimer(TimerCallback callback, int intervalMs)
2015-04-13 21:14:37 +02:00
{
CheckHasExited();
lock (_timerLock)
{
if (KillTimer == null)
{
Logger.Debug("Starting kill timer at {0}ms. JobId {1} PlaySessionId {2}", intervalMs, Id, PlaySessionId);
KillTimer = new Timer(callback, this, intervalMs, Timeout.Infinite);
}
else
{
Logger.Debug("Changing kill timer to {0}ms. JobId {1} PlaySessionId {2}", intervalMs, Id, PlaySessionId);
KillTimer.Change(intervalMs, Timeout.Infinite);
}
}
}
2015-04-19 21:17:17 +02:00
public void ChangeKillTimerIfStarted()
2015-04-13 21:14:37 +02:00
{
CheckHasExited();
lock (_timerLock)
{
if (KillTimer != null)
{
2015-04-19 21:17:17 +02:00
var intervalMs = PingTimeout;
2015-04-13 21:14:37 +02:00
Logger.Debug("Changing kill timer to {0}ms. JobId {1} PlaySessionId {2}", intervalMs, Id, PlaySessionId);
KillTimer.Change(intervalMs, Timeout.Infinite);
}
}
}
private void CheckHasExited()
{
if (HasExited)
2014-09-18 06:50:21 +02:00
{
2015-04-13 21:14:37 +02:00
throw new ObjectDisposedException("Job");
2014-09-18 06:50:21 +02:00
}
}
}
/// <summary>
/// Enum TranscodingJobType
/// </summary>
public enum TranscodingJobType
{
/// <summary>
/// The progressive
/// </summary>
Progressive,
/// <summary>
/// The HLS
/// </summary>
2014-10-12 19:31:41 +02:00
Hls,
/// <summary>
/// The dash
/// </summary>
Dash
}
}