ApiEntryPoint.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  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, 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 && j.Path.Equals(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 && j.Path.Equals(path, StringComparison.OrdinalIgnoreCase));
  228. }
  229. }
  230. public TranscodingJob GetTranscodingJob(string id)
  231. {
  232. lock (_activeTranscodingJobs)
  233. {
  234. return _activeTranscodingJobs.FirstOrDefault(j => j.Id.Equals(id, StringComparison.OrdinalIgnoreCase));
  235. }
  236. }
  237. /// <summary>
  238. /// Called when [transcode begin request].
  239. /// </summary>
  240. /// <param name="path">The path.</param>
  241. /// <param name="type">The type.</param>
  242. public TranscodingJob OnTranscodeBeginRequest(string path, TranscodingJobType type)
  243. {
  244. lock (_activeTranscodingJobs)
  245. {
  246. var job = _activeTranscodingJobs.FirstOrDefault(j => j.Type == type && j.Path.Equals(path, StringComparison.OrdinalIgnoreCase));
  247. if (job == null)
  248. {
  249. return null;
  250. }
  251. OnTranscodeBeginRequest(job);
  252. return job;
  253. }
  254. }
  255. public void OnTranscodeBeginRequest(TranscodingJob job)
  256. {
  257. job.ActiveRequestCount++;
  258. if (string.IsNullOrWhiteSpace(job.PlaySessionId) || job.Type == TranscodingJobType.Progressive)
  259. {
  260. job.StopKillTimer();
  261. }
  262. }
  263. public void OnTranscodeEndRequest(TranscodingJob job)
  264. {
  265. job.ActiveRequestCount--;
  266. Logger.Debug("OnTranscodeEndRequest job.ActiveRequestCount={0}", job.ActiveRequestCount);
  267. if (job.ActiveRequestCount <= 0)
  268. {
  269. PingTimer(job, false);
  270. }
  271. }
  272. internal void PingTranscodingJob(string playSessionId)
  273. {
  274. if (string.IsNullOrEmpty(playSessionId))
  275. {
  276. throw new ArgumentNullException("playSessionId");
  277. }
  278. Logger.Debug("PingTranscodingJob PlaySessionId={0}", playSessionId);
  279. var jobs = new List<TranscodingJob>();
  280. lock (_activeTranscodingJobs)
  281. {
  282. // This is really only needed for HLS.
  283. // Progressive streams can stop on their own reliably
  284. jobs = jobs.Where(j => string.Equals(playSessionId, j.PlaySessionId, StringComparison.OrdinalIgnoreCase)).ToList();
  285. }
  286. foreach (var job in jobs)
  287. {
  288. PingTimer(job, true);
  289. }
  290. }
  291. private async void PingTimer(TranscodingJob job, bool isProgressCheckIn)
  292. {
  293. if (job.HasExited)
  294. {
  295. job.StopKillTimer();
  296. return;
  297. }
  298. var timerDuration = job.Type == TranscodingJobType.Progressive ?
  299. 1000 :
  300. 1800000;
  301. // We can really reduce the timeout for apps that are using the newer api
  302. if (!string.IsNullOrWhiteSpace(job.PlaySessionId) && job.Type != TranscodingJobType.Progressive)
  303. {
  304. timerDuration = 60000;
  305. }
  306. job.PingTimeout = timerDuration;
  307. job.LastPingDate = DateTime.UtcNow;
  308. // Don't start the timer for playback checkins with progressive streaming
  309. if (job.Type != TranscodingJobType.Progressive || !isProgressCheckIn)
  310. {
  311. job.StartKillTimer(OnTranscodeKillTimerStopped);
  312. }
  313. else
  314. {
  315. job.ChangeKillTimerIfStarted();
  316. }
  317. if (!string.IsNullOrWhiteSpace(job.LiveStreamId))
  318. {
  319. try
  320. {
  321. await _mediaSourceManager.PingLiveStream(job.LiveStreamId, CancellationToken.None).ConfigureAwait(false);
  322. }
  323. catch (Exception ex)
  324. {
  325. Logger.ErrorException("Error closing live stream", ex);
  326. }
  327. }
  328. }
  329. /// <summary>
  330. /// Called when [transcode kill timer stopped].
  331. /// </summary>
  332. /// <param name="state">The state.</param>
  333. private void OnTranscodeKillTimerStopped(object state)
  334. {
  335. var job = (TranscodingJob)state;
  336. if (!job.HasExited && job.Type != TranscodingJobType.Progressive)
  337. {
  338. var timeSinceLastPing = (DateTime.UtcNow - job.LastPingDate).TotalMilliseconds;
  339. if (timeSinceLastPing < job.PingTimeout)
  340. {
  341. job.StartKillTimer(OnTranscodeKillTimerStopped, job.PingTimeout);
  342. return;
  343. }
  344. }
  345. Logger.Debug("Transcoding kill timer stopped for JobId {0} PlaySessionId {1}. Killing transcoding", job.Id, job.PlaySessionId);
  346. KillTranscodingJob(job, path => true);
  347. }
  348. /// <summary>
  349. /// Kills the single transcoding job.
  350. /// </summary>
  351. /// <param name="deviceId">The device id.</param>
  352. /// <param name="playSessionId">The play session identifier.</param>
  353. /// <param name="deleteFiles">The delete files.</param>
  354. /// <returns>Task.</returns>
  355. internal void KillTranscodingJobs(string deviceId, string playSessionId, Func<string, bool> deleteFiles)
  356. {
  357. KillTranscodingJobs(j =>
  358. {
  359. if (!string.IsNullOrWhiteSpace(playSessionId))
  360. {
  361. return string.Equals(playSessionId, j.PlaySessionId, StringComparison.OrdinalIgnoreCase);
  362. }
  363. return string.Equals(deviceId, j.DeviceId, StringComparison.OrdinalIgnoreCase);
  364. }, deleteFiles);
  365. }
  366. /// <summary>
  367. /// Kills the transcoding jobs.
  368. /// </summary>
  369. /// <param name="killJob">The kill job.</param>
  370. /// <param name="deleteFiles">The delete files.</param>
  371. /// <returns>Task.</returns>
  372. private void KillTranscodingJobs(Func<TranscodingJob, bool> killJob, Func<string, bool> deleteFiles)
  373. {
  374. var jobs = new List<TranscodingJob>();
  375. lock (_activeTranscodingJobs)
  376. {
  377. // This is really only needed for HLS.
  378. // Progressive streams can stop on their own reliably
  379. jobs.AddRange(_activeTranscodingJobs.Where(killJob));
  380. }
  381. if (jobs.Count == 0)
  382. {
  383. return;
  384. }
  385. foreach (var job in jobs)
  386. {
  387. KillTranscodingJob(job, deleteFiles);
  388. }
  389. }
  390. /// <summary>
  391. /// Kills the transcoding job.
  392. /// </summary>
  393. /// <param name="job">The job.</param>
  394. /// <param name="delete">The delete.</param>
  395. private void KillTranscodingJob(TranscodingJob job, Func<string, bool> delete)
  396. {
  397. job.DisposeKillTimer();
  398. Logger.Debug("KillTranscodingJob - JobId {0} PlaySessionId {1}. Killing transcoding", job.Id, job.PlaySessionId);
  399. lock (_activeTranscodingJobs)
  400. {
  401. _activeTranscodingJobs.Remove(job);
  402. if (!job.CancellationTokenSource.IsCancellationRequested)
  403. {
  404. job.CancellationTokenSource.Cancel();
  405. }
  406. }
  407. lock (job.ProcessLock)
  408. {
  409. if (job.TranscodingThrottler != null)
  410. {
  411. job.TranscodingThrottler.Stop();
  412. }
  413. var process = job.Process;
  414. var hasExited = job.HasExited;
  415. if (!hasExited)
  416. {
  417. try
  418. {
  419. Logger.Info("Killing ffmpeg process for {0}", job.Path);
  420. //process.Kill();
  421. process.StandardInput.WriteLine("q");
  422. // Need to wait because killing is asynchronous
  423. process.WaitForExit(5000);
  424. }
  425. catch (Exception ex)
  426. {
  427. Logger.ErrorException("Error killing transcoding job for {0}", ex, job.Path);
  428. }
  429. }
  430. }
  431. if (delete(job.Path))
  432. {
  433. DeletePartialStreamFiles(job.Path, job.Type, 0, 1500);
  434. }
  435. }
  436. private async void DeletePartialStreamFiles(string path, TranscodingJobType jobType, int retryCount, int delayMs)
  437. {
  438. if (retryCount >= 10)
  439. {
  440. return;
  441. }
  442. Logger.Info("Deleting partial stream file(s) {0}", path);
  443. await Task.Delay(delayMs).ConfigureAwait(false);
  444. try
  445. {
  446. if (jobType == TranscodingJobType.Progressive)
  447. {
  448. DeleteProgressivePartialStreamFiles(path);
  449. }
  450. else
  451. {
  452. DeleteHlsPartialStreamFiles(path);
  453. }
  454. }
  455. catch (DirectoryNotFoundException)
  456. {
  457. }
  458. catch (FileNotFoundException)
  459. {
  460. }
  461. catch (IOException ex)
  462. {
  463. Logger.ErrorException("Error deleting partial stream file(s) {0}", ex, path);
  464. DeletePartialStreamFiles(path, jobType, retryCount + 1, 500);
  465. }
  466. catch (Exception ex)
  467. {
  468. Logger.ErrorException("Error deleting partial stream file(s) {0}", ex, path);
  469. }
  470. }
  471. /// <summary>
  472. /// Deletes the progressive partial stream files.
  473. /// </summary>
  474. /// <param name="outputFilePath">The output file path.</param>
  475. private void DeleteProgressivePartialStreamFiles(string outputFilePath)
  476. {
  477. _fileSystem.DeleteFile(outputFilePath);
  478. }
  479. /// <summary>
  480. /// Deletes the HLS partial stream files.
  481. /// </summary>
  482. /// <param name="outputFilePath">The output file path.</param>
  483. private void DeleteHlsPartialStreamFiles(string outputFilePath)
  484. {
  485. var directory = Path.GetDirectoryName(outputFilePath);
  486. var name = Path.GetFileNameWithoutExtension(outputFilePath);
  487. var filesToDelete = Directory.EnumerateFiles(directory)
  488. .Where(f => f.IndexOf(name, StringComparison.OrdinalIgnoreCase) != -1)
  489. .ToList();
  490. Exception e = null;
  491. foreach (var file in filesToDelete)
  492. {
  493. try
  494. {
  495. Logger.Info("Deleting HLS file {0}", file);
  496. _fileSystem.DeleteFile(file);
  497. }
  498. catch (DirectoryNotFoundException)
  499. {
  500. }
  501. catch (FileNotFoundException)
  502. {
  503. }
  504. catch (IOException ex)
  505. {
  506. e = ex;
  507. Logger.ErrorException("Error deleting HLS file {0}", ex, file);
  508. }
  509. }
  510. if (e != null)
  511. {
  512. throw e;
  513. }
  514. }
  515. }
  516. /// <summary>
  517. /// Class TranscodingJob
  518. /// </summary>
  519. public class TranscodingJob
  520. {
  521. /// <summary>
  522. /// Gets or sets the play session identifier.
  523. /// </summary>
  524. /// <value>The play session identifier.</value>
  525. public string PlaySessionId { get; set; }
  526. /// <summary>
  527. /// Gets or sets the live stream identifier.
  528. /// </summary>
  529. /// <value>The live stream identifier.</value>
  530. public string LiveStreamId { get; set; }
  531. /// <summary>
  532. /// Gets or sets the path.
  533. /// </summary>
  534. /// <value>The path.</value>
  535. public string Path { get; set; }
  536. /// <summary>
  537. /// Gets or sets the type.
  538. /// </summary>
  539. /// <value>The type.</value>
  540. public TranscodingJobType Type { get; set; }
  541. /// <summary>
  542. /// Gets or sets the process.
  543. /// </summary>
  544. /// <value>The process.</value>
  545. public Process Process { get; set; }
  546. public ILogger Logger { get; private set; }
  547. /// <summary>
  548. /// Gets or sets the active request count.
  549. /// </summary>
  550. /// <value>The active request count.</value>
  551. public int ActiveRequestCount { get; set; }
  552. /// <summary>
  553. /// Gets or sets the kill timer.
  554. /// </summary>
  555. /// <value>The kill timer.</value>
  556. private Timer KillTimer { get; set; }
  557. public string DeviceId { get; set; }
  558. public CancellationTokenSource CancellationTokenSource { get; set; }
  559. public object ProcessLock = new object();
  560. public bool HasExited { get; set; }
  561. public string Id { get; set; }
  562. public float? Framerate { get; set; }
  563. public double? CompletionPercentage { get; set; }
  564. public long? BytesDownloaded { get; set; }
  565. public long? BytesTranscoded { get; set; }
  566. public long? TranscodingPositionTicks { get; set; }
  567. public long? DownloadPositionTicks { get; set; }
  568. public TranscodingThrottler TranscodingThrottler { get; set; }
  569. private readonly object _timerLock = new object();
  570. public DateTime LastPingDate { get; set; }
  571. public int PingTimeout { get; set; }
  572. public TranscodingJob(ILogger logger)
  573. {
  574. Logger = logger;
  575. }
  576. public void StopKillTimer()
  577. {
  578. lock (_timerLock)
  579. {
  580. if (KillTimer != null)
  581. {
  582. KillTimer.Change(Timeout.Infinite, Timeout.Infinite);
  583. }
  584. }
  585. }
  586. public void DisposeKillTimer()
  587. {
  588. lock (_timerLock)
  589. {
  590. if (KillTimer != null)
  591. {
  592. KillTimer.Dispose();
  593. KillTimer = null;
  594. }
  595. }
  596. }
  597. public void StartKillTimer(TimerCallback callback)
  598. {
  599. StartKillTimer(callback, PingTimeout);
  600. }
  601. public void StartKillTimer(TimerCallback callback, int intervalMs)
  602. {
  603. CheckHasExited();
  604. lock (_timerLock)
  605. {
  606. if (KillTimer == null)
  607. {
  608. Logger.Debug("Starting kill timer at {0}ms. JobId {1} PlaySessionId {2}", intervalMs, Id, PlaySessionId);
  609. KillTimer = new Timer(callback, this, intervalMs, Timeout.Infinite);
  610. }
  611. else
  612. {
  613. Logger.Debug("Changing kill timer to {0}ms. JobId {1} PlaySessionId {2}", intervalMs, Id, PlaySessionId);
  614. KillTimer.Change(intervalMs, Timeout.Infinite);
  615. }
  616. }
  617. }
  618. public void ChangeKillTimerIfStarted()
  619. {
  620. CheckHasExited();
  621. lock (_timerLock)
  622. {
  623. if (KillTimer != null)
  624. {
  625. var intervalMs = PingTimeout;
  626. Logger.Debug("Changing kill timer to {0}ms. JobId {1} PlaySessionId {2}", intervalMs, Id, PlaySessionId);
  627. KillTimer.Change(intervalMs, Timeout.Infinite);
  628. }
  629. }
  630. }
  631. private void CheckHasExited()
  632. {
  633. if (HasExited)
  634. {
  635. throw new ObjectDisposedException("Job");
  636. }
  637. }
  638. }
  639. /// <summary>
  640. /// Enum TranscodingJobType
  641. /// </summary>
  642. public enum TranscodingJobType
  643. {
  644. /// <summary>
  645. /// The progressive
  646. /// </summary>
  647. Progressive,
  648. /// <summary>
  649. /// The HLS
  650. /// </summary>
  651. Hls,
  652. /// <summary>
  653. /// The dash
  654. /// </summary>
  655. Dash
  656. }
  657. }