ApiEntryPoint.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  1. using MediaBrowser.Api.Playback;
  2. using MediaBrowser.Common.Configuration;
  3. using MediaBrowser.Common.IO;
  4. using MediaBrowser.Controller.Configuration;
  5. using MediaBrowser.Controller.Library;
  6. using MediaBrowser.Controller.Plugins;
  7. using MediaBrowser.Controller.Session;
  8. using MediaBrowser.Model.Configuration;
  9. using MediaBrowser.Model.Logging;
  10. using MediaBrowser.Model.Session;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Diagnostics;
  14. using System.IO;
  15. using System.Linq;
  16. using System.Threading;
  17. using System.Threading.Tasks;
  18. namespace MediaBrowser.Api
  19. {
  20. /// <summary>
  21. /// Class ServerEntryPoint
  22. /// </summary>
  23. public class ApiEntryPoint : IServerEntryPoint
  24. {
  25. /// <summary>
  26. /// The instance
  27. /// </summary>
  28. public static ApiEntryPoint Instance;
  29. /// <summary>
  30. /// Gets or sets the logger.
  31. /// </summary>
  32. /// <value>The logger.</value>
  33. private ILogger Logger { get; set; }
  34. /// <summary>
  35. /// The application paths
  36. /// </summary>
  37. private readonly IServerConfigurationManager _config;
  38. private readonly ISessionManager _sessionManager;
  39. private readonly IFileSystem _fileSystem;
  40. private readonly IMediaSourceManager _mediaSourceManager;
  41. public readonly SemaphoreSlim TranscodingStartLock = new SemaphoreSlim(1, 1);
  42. /// <summary>
  43. /// Initializes a new instance of the <see cref="ApiEntryPoint" /> class.
  44. /// </summary>
  45. /// <param name="logger">The logger.</param>
  46. /// <param name="sessionManager">The session manager.</param>
  47. /// <param name="config">The configuration.</param>
  48. /// <param name="fileSystem">The file system.</param>
  49. /// <param name="mediaSourceManager">The media source manager.</param>
  50. public ApiEntryPoint(ILogger logger, ISessionManager sessionManager, IServerConfigurationManager config, IFileSystem fileSystem, IMediaSourceManager mediaSourceManager)
  51. {
  52. Logger = logger;
  53. _sessionManager = sessionManager;
  54. _config = config;
  55. _fileSystem = fileSystem;
  56. _mediaSourceManager = mediaSourceManager;
  57. Instance = this;
  58. }
  59. /// <summary>
  60. /// Runs this instance.
  61. /// </summary>
  62. public void Run()
  63. {
  64. try
  65. {
  66. DeleteEncodedMediaCache();
  67. }
  68. catch (DirectoryNotFoundException)
  69. {
  70. // Don't clutter the log
  71. }
  72. catch (IOException ex)
  73. {
  74. Logger.ErrorException("Error deleting encoded media cache", ex);
  75. }
  76. }
  77. public EncodingOptions GetEncodingOptions()
  78. {
  79. return _config.GetConfiguration<EncodingOptions>("encoding");
  80. }
  81. /// <summary>
  82. /// Deletes the encoded media cache.
  83. /// </summary>
  84. private void DeleteEncodedMediaCache()
  85. {
  86. var path = _config.ApplicationPaths.TranscodingTempPath;
  87. foreach (var file in Directory.EnumerateFiles(path, "*", SearchOption.AllDirectories)
  88. .ToList())
  89. {
  90. _fileSystem.DeleteFile(file);
  91. }
  92. }
  93. /// <summary>
  94. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  95. /// </summary>
  96. public void Dispose()
  97. {
  98. Dispose(true);
  99. GC.SuppressFinalize(this);
  100. }
  101. /// <summary>
  102. /// Releases unmanaged and - optionally - managed resources.
  103. /// </summary>
  104. /// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  105. protected virtual void Dispose(bool dispose)
  106. {
  107. var jobCount = _activeTranscodingJobs.Count;
  108. Parallel.ForEach(_activeTranscodingJobs.ToList(), j => KillTranscodingJob(j, false, path => true));
  109. // Try to allow for some time to kill the ffmpeg processes and delete the partial stream files
  110. if (jobCount > 0)
  111. {
  112. Thread.Sleep(1000);
  113. }
  114. }
  115. /// <summary>
  116. /// The active transcoding jobs
  117. /// </summary>
  118. private readonly List<TranscodingJob> _activeTranscodingJobs = new List<TranscodingJob>();
  119. /// <summary>
  120. /// Called when [transcode beginning].
  121. /// </summary>
  122. /// <param name="path">The path.</param>
  123. /// <param name="playSessionId">The play session identifier.</param>
  124. /// <param name="liveStreamId">The live stream identifier.</param>
  125. /// <param name="transcodingJobId">The transcoding job identifier.</param>
  126. /// <param name="type">The type.</param>
  127. /// <param name="process">The process.</param>
  128. /// <param name="deviceId">The device id.</param>
  129. /// <param name="state">The state.</param>
  130. /// <param name="cancellationTokenSource">The cancellation token source.</param>
  131. /// <returns>TranscodingJob.</returns>
  132. public TranscodingJob OnTranscodeBeginning(string path,
  133. string playSessionId,
  134. string liveStreamId,
  135. string transcodingJobId,
  136. TranscodingJobType type,
  137. Process process,
  138. string deviceId,
  139. StreamState state,
  140. CancellationTokenSource cancellationTokenSource)
  141. {
  142. lock (_activeTranscodingJobs)
  143. {
  144. var job = new TranscodingJob(Logger)
  145. {
  146. Type = type,
  147. Path = path,
  148. Process = process,
  149. ActiveRequestCount = 1,
  150. DeviceId = deviceId,
  151. CancellationTokenSource = cancellationTokenSource,
  152. Id = transcodingJobId,
  153. PlaySessionId = playSessionId,
  154. LiveStreamId = liveStreamId
  155. };
  156. _activeTranscodingJobs.Add(job);
  157. ReportTranscodingProgress(job, state, null, null, null, null);
  158. return job;
  159. }
  160. }
  161. public void ReportTranscodingProgress(TranscodingJob job, StreamState state, TimeSpan? transcodingPosition, float? framerate, double? percentComplete, long? bytesTranscoded)
  162. {
  163. var ticks = transcodingPosition.HasValue ? transcodingPosition.Value.Ticks : (long?)null;
  164. if (job != null)
  165. {
  166. job.Framerate = framerate;
  167. job.CompletionPercentage = percentComplete;
  168. job.TranscodingPositionTicks = ticks;
  169. job.BytesTranscoded = bytesTranscoded;
  170. }
  171. var deviceId = state.Request.DeviceId;
  172. if (!string.IsNullOrWhiteSpace(deviceId))
  173. {
  174. var audioCodec = state.ActualOutputAudioCodec;
  175. var videoCodec = state.ActualOutputVideoCodec;
  176. _sessionManager.ReportTranscodingInfo(deviceId, new TranscodingInfo
  177. {
  178. Bitrate = state.TotalOutputBitrate,
  179. AudioCodec = audioCodec,
  180. VideoCodec = videoCodec,
  181. Container = state.OutputContainer,
  182. Framerate = framerate,
  183. CompletionPercentage = percentComplete,
  184. Width = state.OutputWidth,
  185. Height = state.OutputHeight,
  186. AudioChannels = state.OutputAudioChannels,
  187. IsAudioDirect = string.Equals(state.OutputAudioCodec, "copy", StringComparison.OrdinalIgnoreCase),
  188. IsVideoDirect = string.Equals(state.OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase)
  189. });
  190. }
  191. }
  192. /// <summary>
  193. /// <summary>
  194. /// The progressive
  195. /// </summary>
  196. /// Called when [transcode failed to start].
  197. /// </summary>
  198. /// <param name="path">The path.</param>
  199. /// <param name="type">The type.</param>
  200. /// <param name="state">The state.</param>
  201. public void OnTranscodeFailedToStart(string path, TranscodingJobType type, StreamState state)
  202. {
  203. lock (_activeTranscodingJobs)
  204. {
  205. var job = _activeTranscodingJobs.First(j => j.Type == type && string.Equals(j.Path, path, StringComparison.OrdinalIgnoreCase));
  206. _activeTranscodingJobs.Remove(job);
  207. }
  208. if (!string.IsNullOrWhiteSpace(state.Request.DeviceId))
  209. {
  210. _sessionManager.ClearTranscodingInfo(state.Request.DeviceId);
  211. }
  212. }
  213. /// <summary>
  214. /// Determines whether [has active transcoding job] [the specified path].
  215. /// </summary>
  216. /// <param name="path">The path.</param>
  217. /// <param name="type">The type.</param>
  218. /// <returns><c>true</c> if [has active transcoding job] [the specified path]; otherwise, <c>false</c>.</returns>
  219. public bool HasActiveTranscodingJob(string path, TranscodingJobType type)
  220. {
  221. return GetTranscodingJob(path, type) != null;
  222. }
  223. public TranscodingJob GetTranscodingJob(string path, TranscodingJobType type)
  224. {
  225. lock (_activeTranscodingJobs)
  226. {
  227. return _activeTranscodingJobs.FirstOrDefault(j => j.Type == type && string.Equals(j.Path, path, StringComparison.OrdinalIgnoreCase));
  228. }
  229. }
  230. /// <summary>
  231. /// Called when [transcode begin request].
  232. /// </summary>
  233. /// <param name="path">The path.</param>
  234. /// <param name="type">The type.</param>
  235. public TranscodingJob OnTranscodeBeginRequest(string path, TranscodingJobType type)
  236. {
  237. lock (_activeTranscodingJobs)
  238. {
  239. var job = _activeTranscodingJobs.FirstOrDefault(j => j.Type == type && string.Equals(j.Path, path, StringComparison.OrdinalIgnoreCase));
  240. if (job == null)
  241. {
  242. return null;
  243. }
  244. OnTranscodeBeginRequest(job);
  245. return job;
  246. }
  247. }
  248. public void OnTranscodeBeginRequest(TranscodingJob job)
  249. {
  250. job.ActiveRequestCount++;
  251. if (string.IsNullOrWhiteSpace(job.PlaySessionId) || job.Type == TranscodingJobType.Progressive)
  252. {
  253. job.StopKillTimer();
  254. }
  255. }
  256. public void OnTranscodeEndRequest(TranscodingJob job)
  257. {
  258. job.ActiveRequestCount--;
  259. Logger.Debug("OnTranscodeEndRequest job.ActiveRequestCount={0}", job.ActiveRequestCount);
  260. if (job.ActiveRequestCount <= 0)
  261. {
  262. PingTimer(job, false);
  263. }
  264. }
  265. internal void PingTranscodingJob(string playSessionId)
  266. {
  267. if (string.IsNullOrEmpty(playSessionId))
  268. {
  269. throw new ArgumentNullException("playSessionId");
  270. }
  271. Logger.Debug("PingTranscodingJob PlaySessionId={0}", playSessionId);
  272. var jobs = new List<TranscodingJob>();
  273. lock (_activeTranscodingJobs)
  274. {
  275. // This is really only needed for HLS.
  276. // Progressive streams can stop on their own reliably
  277. jobs = jobs.Where(j => string.Equals(playSessionId, j.PlaySessionId, StringComparison.OrdinalIgnoreCase)).ToList();
  278. }
  279. foreach (var job in jobs)
  280. {
  281. PingTimer(job, true);
  282. }
  283. }
  284. private async void PingTimer(TranscodingJob job, bool isProgressCheckIn)
  285. {
  286. if (job.HasExited)
  287. {
  288. job.StopKillTimer();
  289. return;
  290. }
  291. var timerDuration = 1000;
  292. if (job.Type != TranscodingJobType.Progressive)
  293. {
  294. timerDuration = 1800000;
  295. // We can really reduce the timeout for apps that are using the newer api
  296. if (!string.IsNullOrWhiteSpace(job.PlaySessionId))
  297. {
  298. timerDuration = 300000;
  299. }
  300. }
  301. job.PingTimeout = timerDuration;
  302. job.LastPingDate = DateTime.UtcNow;
  303. // Don't start the timer for playback checkins with progressive streaming
  304. if (job.Type != TranscodingJobType.Progressive || !isProgressCheckIn)
  305. {
  306. job.StartKillTimer(OnTranscodeKillTimerStopped);
  307. }
  308. else
  309. {
  310. job.ChangeKillTimerIfStarted();
  311. }
  312. if (!string.IsNullOrWhiteSpace(job.LiveStreamId))
  313. {
  314. try
  315. {
  316. await _mediaSourceManager.PingLiveStream(job.LiveStreamId, CancellationToken.None).ConfigureAwait(false);
  317. }
  318. catch (Exception ex)
  319. {
  320. Logger.ErrorException("Error closing live stream", ex);
  321. }
  322. }
  323. }
  324. /// <summary>
  325. /// Called when [transcode kill timer stopped].
  326. /// </summary>
  327. /// <param name="state">The state.</param>
  328. private void OnTranscodeKillTimerStopped(object state)
  329. {
  330. var job = (TranscodingJob)state;
  331. if (!job.HasExited && job.Type != TranscodingJobType.Progressive)
  332. {
  333. var timeSinceLastPing = (DateTime.UtcNow - job.LastPingDate).TotalMilliseconds;
  334. if (timeSinceLastPing < job.PingTimeout)
  335. {
  336. job.StartKillTimer(OnTranscodeKillTimerStopped, job.PingTimeout);
  337. return;
  338. }
  339. }
  340. Logger.Debug("Transcoding kill timer stopped for JobId {0} PlaySessionId {1}. Killing transcoding", job.Id, job.PlaySessionId);
  341. KillTranscodingJob(job, true, path => true);
  342. }
  343. /// <summary>
  344. /// Kills the single transcoding job.
  345. /// </summary>
  346. /// <param name="deviceId">The device id.</param>
  347. /// <param name="playSessionId">The play session identifier.</param>
  348. /// <param name="deleteFiles">The delete files.</param>
  349. /// <returns>Task.</returns>
  350. internal void KillTranscodingJobs(string deviceId, string playSessionId, Func<string, bool> deleteFiles)
  351. {
  352. KillTranscodingJobs(j =>
  353. {
  354. if (!string.IsNullOrWhiteSpace(playSessionId))
  355. {
  356. return string.Equals(playSessionId, j.PlaySessionId, StringComparison.OrdinalIgnoreCase);
  357. }
  358. return string.Equals(deviceId, j.DeviceId, StringComparison.OrdinalIgnoreCase);
  359. }, deleteFiles);
  360. }
  361. /// <summary>
  362. /// Kills the transcoding jobs.
  363. /// </summary>
  364. /// <param name="killJob">The kill job.</param>
  365. /// <param name="deleteFiles">The delete files.</param>
  366. /// <returns>Task.</returns>
  367. private void KillTranscodingJobs(Func<TranscodingJob, bool> killJob, Func<string, bool> deleteFiles)
  368. {
  369. var jobs = new List<TranscodingJob>();
  370. lock (_activeTranscodingJobs)
  371. {
  372. // This is really only needed for HLS.
  373. // Progressive streams can stop on their own reliably
  374. jobs.AddRange(_activeTranscodingJobs.Where(killJob));
  375. }
  376. if (jobs.Count == 0)
  377. {
  378. return;
  379. }
  380. foreach (var job in jobs)
  381. {
  382. KillTranscodingJob(job, false, deleteFiles);
  383. }
  384. }
  385. /// <summary>
  386. /// Kills the transcoding job.
  387. /// </summary>
  388. /// <param name="job">The job.</param>
  389. /// <param name="closeLiveStream">if set to <c>true</c> [close live stream].</param>
  390. /// <param name="delete">The delete.</param>
  391. private async void KillTranscodingJob(TranscodingJob job, bool closeLiveStream, Func<string, bool> delete)
  392. {
  393. job.DisposeKillTimer();
  394. Logger.Debug("KillTranscodingJob - JobId {0} PlaySessionId {1}. Killing transcoding", job.Id, job.PlaySessionId);
  395. lock (_activeTranscodingJobs)
  396. {
  397. _activeTranscodingJobs.Remove(job);
  398. if (!job.CancellationTokenSource.IsCancellationRequested)
  399. {
  400. job.CancellationTokenSource.Cancel();
  401. }
  402. }
  403. lock (job.ProcessLock)
  404. {
  405. if (job.TranscodingThrottler != null)
  406. {
  407. job.TranscodingThrottler.Stop();
  408. }
  409. var process = job.Process;
  410. var hasExited = job.HasExited;
  411. if (!hasExited)
  412. {
  413. try
  414. {
  415. Logger.Info("Killing ffmpeg process for {0}", job.Path);
  416. //process.Kill();
  417. process.StandardInput.WriteLine("q");
  418. // Need to wait because killing is asynchronous
  419. process.WaitForExit(5000);
  420. }
  421. catch (Exception ex)
  422. {
  423. Logger.ErrorException("Error killing transcoding job for {0}", ex, job.Path);
  424. }
  425. }
  426. }
  427. if (delete(job.Path))
  428. {
  429. DeletePartialStreamFiles(job.Path, job.Type, 0, 1500);
  430. }
  431. if (closeLiveStream && !string.IsNullOrWhiteSpace(job.LiveStreamId))
  432. {
  433. try
  434. {
  435. await _mediaSourceManager.CloseLiveStream(job.LiveStreamId, CancellationToken.None).ConfigureAwait(false);
  436. }
  437. catch (Exception ex)
  438. {
  439. Logger.ErrorException("Error closing live stream for {0}", ex, job.Path);
  440. }
  441. }
  442. }
  443. private async void DeletePartialStreamFiles(string path, TranscodingJobType jobType, int retryCount, int delayMs)
  444. {
  445. if (retryCount >= 10)
  446. {
  447. return;
  448. }
  449. Logger.Info("Deleting partial stream file(s) {0}", path);
  450. await Task.Delay(delayMs).ConfigureAwait(false);
  451. try
  452. {
  453. if (jobType == TranscodingJobType.Progressive)
  454. {
  455. DeleteProgressivePartialStreamFiles(path);
  456. }
  457. else
  458. {
  459. DeleteHlsPartialStreamFiles(path);
  460. }
  461. }
  462. catch (DirectoryNotFoundException)
  463. {
  464. }
  465. catch (FileNotFoundException)
  466. {
  467. }
  468. catch (IOException ex)
  469. {
  470. Logger.ErrorException("Error deleting partial stream file(s) {0}", ex, path);
  471. DeletePartialStreamFiles(path, jobType, retryCount + 1, 500);
  472. }
  473. catch (Exception ex)
  474. {
  475. Logger.ErrorException("Error deleting partial stream file(s) {0}", ex, path);
  476. }
  477. }
  478. /// <summary>
  479. /// Deletes the progressive partial stream files.
  480. /// </summary>
  481. /// <param name="outputFilePath">The output file path.</param>
  482. private void DeleteProgressivePartialStreamFiles(string outputFilePath)
  483. {
  484. _fileSystem.DeleteFile(outputFilePath);
  485. }
  486. /// <summary>
  487. /// Deletes the HLS partial stream files.
  488. /// </summary>
  489. /// <param name="outputFilePath">The output file path.</param>
  490. private void DeleteHlsPartialStreamFiles(string outputFilePath)
  491. {
  492. var directory = Path.GetDirectoryName(outputFilePath);
  493. var name = Path.GetFileNameWithoutExtension(outputFilePath);
  494. var filesToDelete = Directory.EnumerateFiles(directory)
  495. .Where(f => f.IndexOf(name, StringComparison.OrdinalIgnoreCase) != -1)
  496. .ToList();
  497. Exception e = null;
  498. foreach (var file in filesToDelete)
  499. {
  500. try
  501. {
  502. Logger.Info("Deleting HLS file {0}", file);
  503. _fileSystem.DeleteFile(file);
  504. }
  505. catch (DirectoryNotFoundException)
  506. {
  507. }
  508. catch (FileNotFoundException)
  509. {
  510. }
  511. catch (IOException ex)
  512. {
  513. e = ex;
  514. Logger.ErrorException("Error deleting HLS file {0}", ex, file);
  515. }
  516. }
  517. if (e != null)
  518. {
  519. throw e;
  520. }
  521. }
  522. }
  523. /// <summary>
  524. /// Class TranscodingJob
  525. /// </summary>
  526. public class TranscodingJob
  527. {
  528. /// <summary>
  529. /// Gets or sets the play session identifier.
  530. /// </summary>
  531. /// <value>The play session identifier.</value>
  532. public string PlaySessionId { get; set; }
  533. /// <summary>
  534. /// Gets or sets the live stream identifier.
  535. /// </summary>
  536. /// <value>The live stream identifier.</value>
  537. public string LiveStreamId { get; set; }
  538. public bool IsLiveOutput { get; set; }
  539. /// <summary>
  540. /// Gets or sets the path.
  541. /// </summary>
  542. /// <value>The path.</value>
  543. public string Path { get; set; }
  544. /// <summary>
  545. /// Gets or sets the type.
  546. /// </summary>
  547. /// <value>The type.</value>
  548. public TranscodingJobType Type { get; set; }
  549. /// <summary>
  550. /// Gets or sets the process.
  551. /// </summary>
  552. /// <value>The process.</value>
  553. public Process Process { get; set; }
  554. public ILogger Logger { get; private set; }
  555. /// <summary>
  556. /// Gets or sets the active request count.
  557. /// </summary>
  558. /// <value>The active request count.</value>
  559. public int ActiveRequestCount { get; set; }
  560. /// <summary>
  561. /// Gets or sets the kill timer.
  562. /// </summary>
  563. /// <value>The kill timer.</value>
  564. private Timer KillTimer { get; set; }
  565. public string DeviceId { get; set; }
  566. public CancellationTokenSource CancellationTokenSource { get; set; }
  567. public object ProcessLock = new object();
  568. public bool HasExited { get; set; }
  569. public string Id { get; set; }
  570. public float? Framerate { get; set; }
  571. public double? CompletionPercentage { get; set; }
  572. public long? BytesDownloaded { get; set; }
  573. public long? BytesTranscoded { get; set; }
  574. public long? TranscodingPositionTicks { get; set; }
  575. public long? DownloadPositionTicks { get; set; }
  576. public TranscodingThrottler TranscodingThrottler { get; set; }
  577. private readonly object _timerLock = new object();
  578. public DateTime LastPingDate { get; set; }
  579. public int PingTimeout { get; set; }
  580. public TranscodingJob(ILogger logger)
  581. {
  582. Logger = logger;
  583. }
  584. public void StopKillTimer()
  585. {
  586. lock (_timerLock)
  587. {
  588. if (KillTimer != null)
  589. {
  590. KillTimer.Change(Timeout.Infinite, Timeout.Infinite);
  591. }
  592. }
  593. }
  594. public void DisposeKillTimer()
  595. {
  596. lock (_timerLock)
  597. {
  598. if (KillTimer != null)
  599. {
  600. KillTimer.Dispose();
  601. KillTimer = null;
  602. }
  603. }
  604. }
  605. public void StartKillTimer(TimerCallback callback)
  606. {
  607. StartKillTimer(callback, PingTimeout);
  608. }
  609. public void StartKillTimer(TimerCallback callback, int intervalMs)
  610. {
  611. CheckHasExited();
  612. lock (_timerLock)
  613. {
  614. if (KillTimer == null)
  615. {
  616. Logger.Debug("Starting kill timer at {0}ms. JobId {1} PlaySessionId {2}", intervalMs, Id, PlaySessionId);
  617. KillTimer = new Timer(callback, this, intervalMs, Timeout.Infinite);
  618. }
  619. else
  620. {
  621. Logger.Debug("Changing kill timer to {0}ms. JobId {1} PlaySessionId {2}", intervalMs, Id, PlaySessionId);
  622. KillTimer.Change(intervalMs, Timeout.Infinite);
  623. }
  624. }
  625. }
  626. public void ChangeKillTimerIfStarted()
  627. {
  628. CheckHasExited();
  629. lock (_timerLock)
  630. {
  631. if (KillTimer != null)
  632. {
  633. var intervalMs = PingTimeout;
  634. Logger.Debug("Changing kill timer to {0}ms. JobId {1} PlaySessionId {2}", intervalMs, Id, PlaySessionId);
  635. KillTimer.Change(intervalMs, Timeout.Infinite);
  636. }
  637. }
  638. }
  639. private void CheckHasExited()
  640. {
  641. if (HasExited)
  642. {
  643. throw new ObjectDisposedException("Job");
  644. }
  645. }
  646. }
  647. /// <summary>
  648. /// Enum TranscodingJobType
  649. /// </summary>
  650. public enum TranscodingJobType
  651. {
  652. /// <summary>
  653. /// The progressive
  654. /// </summary>
  655. Progressive,
  656. /// <summary>
  657. /// The HLS
  658. /// </summary>
  659. Hls,
  660. /// <summary>
  661. /// The dash
  662. /// </summary>
  663. Dash
  664. }
  665. }