jellyfin/MediaBrowser.Api/ApiEntryPoint.cs

802 lines
26 KiB
C#
Raw Normal View History

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