ApiEntryPoint.cs 25 KB

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