TranscodeManager.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Globalization;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Runtime.CompilerServices;
  8. using System.Text;
  9. using System.Text.Json;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. using AsyncKeyedLock;
  13. using Jellyfin.Data.Enums;
  14. using Jellyfin.Extensions;
  15. using MediaBrowser.Common;
  16. using MediaBrowser.Common.Configuration;
  17. using MediaBrowser.Common.Extensions;
  18. using MediaBrowser.Controller.Configuration;
  19. using MediaBrowser.Controller.Library;
  20. using MediaBrowser.Controller.MediaEncoding;
  21. using MediaBrowser.Controller.Session;
  22. using MediaBrowser.Controller.Streaming;
  23. using MediaBrowser.Model.Dlna;
  24. using MediaBrowser.Model.Entities;
  25. using MediaBrowser.Model.IO;
  26. using MediaBrowser.Model.MediaInfo;
  27. using MediaBrowser.Model.Session;
  28. using Microsoft.Extensions.Logging;
  29. namespace MediaBrowser.MediaEncoding.Transcoding;
  30. /// <inheritdoc cref="ITranscodeManager"/>
  31. public sealed class TranscodeManager : ITranscodeManager, IDisposable
  32. {
  33. private readonly ILoggerFactory _loggerFactory;
  34. private readonly ILogger<TranscodeManager> _logger;
  35. private readonly IFileSystem _fileSystem;
  36. private readonly IApplicationPaths _appPaths;
  37. private readonly IServerConfigurationManager _serverConfigurationManager;
  38. private readonly IUserManager _userManager;
  39. private readonly ISessionManager _sessionManager;
  40. private readonly EncodingHelper _encodingHelper;
  41. private readonly IMediaEncoder _mediaEncoder;
  42. private readonly IMediaSourceManager _mediaSourceManager;
  43. private readonly IAttachmentExtractor _attachmentExtractor;
  44. private readonly List<TranscodingJob> _activeTranscodingJobs = new();
  45. private readonly AsyncKeyedLocker<string> _transcodingLocks = new(o =>
  46. {
  47. o.PoolSize = 20;
  48. o.PoolInitialFill = 1;
  49. });
  50. private readonly Version _maxFFmpegCkeyPauseSupported = new Version(6, 1);
  51. /// <summary>
  52. /// Initializes a new instance of the <see cref="TranscodeManager"/> class.
  53. /// </summary>
  54. /// <param name="loggerFactory">The <see cref="ILoggerFactory"/>.</param>
  55. /// <param name="fileSystem">The <see cref="IFileSystem"/>.</param>
  56. /// <param name="appPaths">The <see cref="IApplicationPaths"/>.</param>
  57. /// <param name="serverConfigurationManager">The <see cref="IServerConfigurationManager"/>.</param>
  58. /// <param name="userManager">The <see cref="IUserManager"/>.</param>
  59. /// <param name="sessionManager">The <see cref="ISessionManager"/>.</param>
  60. /// <param name="encodingHelper">The <see cref="EncodingHelper"/>.</param>
  61. /// <param name="mediaEncoder">The <see cref="IMediaEncoder"/>.</param>
  62. /// <param name="mediaSourceManager">The <see cref="IMediaSourceManager"/>.</param>
  63. /// <param name="attachmentExtractor">The <see cref="IAttachmentExtractor"/>.</param>
  64. public TranscodeManager(
  65. ILoggerFactory loggerFactory,
  66. IFileSystem fileSystem,
  67. IApplicationPaths appPaths,
  68. IServerConfigurationManager serverConfigurationManager,
  69. IUserManager userManager,
  70. ISessionManager sessionManager,
  71. EncodingHelper encodingHelper,
  72. IMediaEncoder mediaEncoder,
  73. IMediaSourceManager mediaSourceManager,
  74. IAttachmentExtractor attachmentExtractor)
  75. {
  76. _loggerFactory = loggerFactory;
  77. _fileSystem = fileSystem;
  78. _appPaths = appPaths;
  79. _serverConfigurationManager = serverConfigurationManager;
  80. _userManager = userManager;
  81. _sessionManager = sessionManager;
  82. _encodingHelper = encodingHelper;
  83. _mediaEncoder = mediaEncoder;
  84. _mediaSourceManager = mediaSourceManager;
  85. _attachmentExtractor = attachmentExtractor;
  86. _logger = loggerFactory.CreateLogger<TranscodeManager>();
  87. DeleteEncodedMediaCache();
  88. _sessionManager.PlaybackProgress += OnPlaybackProgress;
  89. _sessionManager.PlaybackStart += OnPlaybackProgress;
  90. }
  91. /// <inheritdoc />
  92. public TranscodingJob? GetTranscodingJob(string playSessionId)
  93. {
  94. lock (_activeTranscodingJobs)
  95. {
  96. return _activeTranscodingJobs.FirstOrDefault(j => string.Equals(j.PlaySessionId, playSessionId, StringComparison.OrdinalIgnoreCase));
  97. }
  98. }
  99. /// <inheritdoc />
  100. public TranscodingJob? GetTranscodingJob(string path, TranscodingJobType type)
  101. {
  102. lock (_activeTranscodingJobs)
  103. {
  104. return _activeTranscodingJobs.FirstOrDefault(j => j.Type == type && string.Equals(j.Path, path, StringComparison.OrdinalIgnoreCase));
  105. }
  106. }
  107. /// <inheritdoc />
  108. public void PingTranscodingJob(string playSessionId, bool? isUserPaused)
  109. {
  110. ArgumentException.ThrowIfNullOrEmpty(playSessionId);
  111. _logger.LogDebug("PingTranscodingJob PlaySessionId={0} isUsedPaused: {1}", playSessionId, isUserPaused);
  112. List<TranscodingJob> jobs;
  113. lock (_activeTranscodingJobs)
  114. {
  115. // This is really only needed for HLS.
  116. // Progressive streams can stop on their own reliably.
  117. jobs = _activeTranscodingJobs.Where(j => string.Equals(playSessionId, j.PlaySessionId, StringComparison.OrdinalIgnoreCase)).ToList();
  118. }
  119. foreach (var job in jobs)
  120. {
  121. if (isUserPaused.HasValue)
  122. {
  123. _logger.LogDebug("Setting job.IsUserPaused to {0}. jobId: {1}", isUserPaused, job.Id);
  124. job.IsUserPaused = isUserPaused.Value;
  125. }
  126. PingTimer(job, true);
  127. }
  128. }
  129. private void PingTimer(TranscodingJob job, bool isProgressCheckIn)
  130. {
  131. if (job.HasExited)
  132. {
  133. job.StopKillTimer();
  134. return;
  135. }
  136. var timerDuration = 10000;
  137. if (job.Type != TranscodingJobType.Progressive)
  138. {
  139. timerDuration = 60000;
  140. }
  141. job.PingTimeout = timerDuration;
  142. job.LastPingDate = DateTime.UtcNow;
  143. // Don't start the timer for playback checkins with progressive streaming
  144. if (job.Type != TranscodingJobType.Progressive || !isProgressCheckIn)
  145. {
  146. job.StartKillTimer(OnTranscodeKillTimerStopped);
  147. }
  148. else
  149. {
  150. job.ChangeKillTimerIfStarted();
  151. }
  152. }
  153. private async void OnTranscodeKillTimerStopped(object? state)
  154. {
  155. var job = state as TranscodingJob ?? throw new ArgumentException($"{nameof(state)} is not of type {nameof(TranscodingJob)}", nameof(state));
  156. if (!job.HasExited && job.Type != TranscodingJobType.Progressive)
  157. {
  158. var timeSinceLastPing = (DateTime.UtcNow - job.LastPingDate).TotalMilliseconds;
  159. if (timeSinceLastPing < job.PingTimeout)
  160. {
  161. job.StartKillTimer(OnTranscodeKillTimerStopped, job.PingTimeout);
  162. return;
  163. }
  164. }
  165. _logger.LogInformation("Transcoding kill timer stopped for JobId {0} PlaySessionId {1}. Killing transcoding", job.Id, job.PlaySessionId);
  166. await KillTranscodingJob(job, true, path => true).ConfigureAwait(false);
  167. }
  168. /// <inheritdoc />
  169. public Task KillTranscodingJobs(string deviceId, string? playSessionId, Func<string, bool> deleteFiles)
  170. {
  171. var jobs = new List<TranscodingJob>();
  172. lock (_activeTranscodingJobs)
  173. {
  174. // This is really only needed for HLS.
  175. // Progressive streams can stop on their own reliably.
  176. jobs.AddRange(_activeTranscodingJobs.Where(j => string.IsNullOrWhiteSpace(playSessionId)
  177. ? string.Equals(deviceId, j.DeviceId, StringComparison.OrdinalIgnoreCase)
  178. : string.Equals(playSessionId, j.PlaySessionId, StringComparison.OrdinalIgnoreCase)));
  179. }
  180. return Task.WhenAll(GetKillJobs());
  181. IEnumerable<Task> GetKillJobs()
  182. {
  183. foreach (var job in jobs)
  184. {
  185. yield return KillTranscodingJob(job, false, deleteFiles);
  186. }
  187. }
  188. }
  189. private async Task KillTranscodingJob(TranscodingJob job, bool closeLiveStream, Func<string, bool> delete)
  190. {
  191. job.DisposeKillTimer();
  192. _logger.LogDebug("KillTranscodingJob - JobId {0} PlaySessionId {1}. Killing transcoding", job.Id, job.PlaySessionId);
  193. lock (_activeTranscodingJobs)
  194. {
  195. _activeTranscodingJobs.Remove(job);
  196. if (job.CancellationTokenSource?.IsCancellationRequested == false)
  197. {
  198. #pragma warning disable CA1849 // Can't await in lock block
  199. job.CancellationTokenSource.Cancel();
  200. #pragma warning restore CA1849
  201. }
  202. }
  203. job.Stop();
  204. if (delete(job.Path!))
  205. {
  206. await DeletePartialStreamFiles(job.Path!, job.Type, 0, 1500).ConfigureAwait(false);
  207. }
  208. if (closeLiveStream && !string.IsNullOrWhiteSpace(job.LiveStreamId))
  209. {
  210. try
  211. {
  212. await _mediaSourceManager.CloseLiveStream(job.LiveStreamId).ConfigureAwait(false);
  213. }
  214. catch (Exception ex)
  215. {
  216. _logger.LogError(ex, "Error closing live stream for {Path}", job.Path);
  217. }
  218. }
  219. }
  220. private async Task DeletePartialStreamFiles(string path, TranscodingJobType jobType, int retryCount, int delayMs)
  221. {
  222. if (retryCount >= 10)
  223. {
  224. return;
  225. }
  226. _logger.LogInformation("Deleting partial stream file(s) {Path}", path);
  227. await Task.Delay(delayMs).ConfigureAwait(false);
  228. try
  229. {
  230. if (jobType == TranscodingJobType.Progressive)
  231. {
  232. DeleteProgressivePartialStreamFiles(path);
  233. }
  234. else
  235. {
  236. DeleteHlsPartialStreamFiles(path);
  237. }
  238. }
  239. catch (IOException ex)
  240. {
  241. _logger.LogError(ex, "Error deleting partial stream file(s) {Path}", path);
  242. await DeletePartialStreamFiles(path, jobType, retryCount + 1, 500).ConfigureAwait(false);
  243. }
  244. catch (Exception ex)
  245. {
  246. _logger.LogError(ex, "Error deleting partial stream file(s) {Path}", path);
  247. }
  248. }
  249. private void DeleteProgressivePartialStreamFiles(string outputFilePath)
  250. {
  251. if (File.Exists(outputFilePath))
  252. {
  253. _fileSystem.DeleteFile(outputFilePath);
  254. }
  255. }
  256. private void DeleteHlsPartialStreamFiles(string outputFilePath)
  257. {
  258. var directory = Path.GetDirectoryName(outputFilePath)
  259. ?? throw new ArgumentException("Path can't be a root directory.", nameof(outputFilePath));
  260. var name = Path.GetFileNameWithoutExtension(outputFilePath);
  261. var filesToDelete = _fileSystem.GetFilePaths(directory)
  262. .Where(f => f.Contains(name, StringComparison.OrdinalIgnoreCase));
  263. List<Exception>? exs = null;
  264. foreach (var file in filesToDelete)
  265. {
  266. try
  267. {
  268. _logger.LogDebug("Deleting HLS file {0}", file);
  269. _fileSystem.DeleteFile(file);
  270. }
  271. catch (IOException ex)
  272. {
  273. (exs ??= new List<Exception>()).Add(ex);
  274. _logger.LogError(ex, "Error deleting HLS file {Path}", file);
  275. }
  276. }
  277. if (exs is not null)
  278. {
  279. throw new AggregateException("Error deleting HLS files", exs);
  280. }
  281. }
  282. /// <inheritdoc />
  283. public void ReportTranscodingProgress(
  284. TranscodingJob job,
  285. StreamState state,
  286. TimeSpan? transcodingPosition,
  287. float? framerate,
  288. double? percentComplete,
  289. long? bytesTranscoded,
  290. int? bitRate)
  291. {
  292. var ticks = transcodingPosition?.Ticks;
  293. if (job is not null)
  294. {
  295. job.Framerate = framerate;
  296. job.CompletionPercentage = percentComplete;
  297. job.TranscodingPositionTicks = ticks;
  298. job.BytesTranscoded = bytesTranscoded;
  299. job.BitRate = bitRate;
  300. }
  301. var deviceId = state.Request.DeviceId;
  302. if (!string.IsNullOrWhiteSpace(deviceId))
  303. {
  304. var audioCodec = state.ActualOutputAudioCodec;
  305. var videoCodec = state.ActualOutputVideoCodec;
  306. var hardwareAccelerationType = _serverConfigurationManager.GetEncodingOptions().HardwareAccelerationType;
  307. _sessionManager.ReportTranscodingInfo(deviceId, new TranscodingInfo
  308. {
  309. Bitrate = bitRate ?? state.TotalOutputBitrate,
  310. AudioCodec = audioCodec,
  311. VideoCodec = videoCodec,
  312. Container = state.OutputContainer,
  313. Framerate = framerate,
  314. CompletionPercentage = percentComplete,
  315. Width = state.OutputWidth,
  316. Height = state.OutputHeight,
  317. AudioChannels = state.OutputAudioChannels,
  318. IsAudioDirect = EncodingHelper.IsCopyCodec(state.OutputAudioCodec),
  319. IsVideoDirect = EncodingHelper.IsCopyCodec(state.OutputVideoCodec),
  320. HardwareAccelerationType = hardwareAccelerationType,
  321. TranscodeReasons = state.TranscodeReasons
  322. });
  323. }
  324. }
  325. /// <inheritdoc />
  326. public async Task<TranscodingJob> StartFfMpeg(
  327. StreamState state,
  328. string outputPath,
  329. string commandLineArguments,
  330. Guid userId,
  331. TranscodingJobType transcodingJobType,
  332. CancellationTokenSource cancellationTokenSource,
  333. string? workingDirectory = null)
  334. {
  335. var directory = Path.GetDirectoryName(outputPath) ?? throw new ArgumentException($"Provided path ({outputPath}) is not valid.", nameof(outputPath));
  336. Directory.CreateDirectory(directory);
  337. await AcquireResources(state, cancellationTokenSource).ConfigureAwait(false);
  338. if (state.VideoRequest is not null && !EncodingHelper.IsCopyCodec(state.OutputVideoCodec))
  339. {
  340. var user = userId.IsEmpty() ? null : _userManager.GetUserById(userId);
  341. if (user is not null && !user.HasPermission(PermissionKind.EnableVideoPlaybackTranscoding))
  342. {
  343. OnTranscodeFailedToStart(outputPath, transcodingJobType, state);
  344. throw new ArgumentException("User does not have access to video transcoding.");
  345. }
  346. }
  347. ArgumentException.ThrowIfNullOrEmpty(_mediaEncoder.EncoderPath);
  348. // If subtitles get burned in fonts may need to be extracted from the media file
  349. if (state.SubtitleStream is not null && state.SubtitleDeliveryMethod == SubtitleDeliveryMethod.Encode)
  350. {
  351. var attachmentPath = Path.Combine(_appPaths.CachePath, "attachments", state.MediaSource.Id);
  352. if (state.MediaSource.VideoType == VideoType.Dvd || state.MediaSource.VideoType == VideoType.BluRay)
  353. {
  354. var concatPath = Path.Join(_appPaths.CachePath, "concat", state.MediaSource.Id + ".concat");
  355. await _attachmentExtractor.ExtractAllAttachments(concatPath, state.MediaSource, attachmentPath, cancellationTokenSource.Token).ConfigureAwait(false);
  356. }
  357. else
  358. {
  359. await _attachmentExtractor.ExtractAllAttachments(state.MediaPath, state.MediaSource, attachmentPath, cancellationTokenSource.Token).ConfigureAwait(false);
  360. }
  361. if (state.SubtitleStream.IsExternal && Path.GetExtension(state.SubtitleStream.Path.AsSpan()).Equals(".mks", StringComparison.OrdinalIgnoreCase))
  362. {
  363. string subtitlePath = state.SubtitleStream.Path;
  364. string subtitlePathArgument = string.Format(CultureInfo.InvariantCulture, "file:\"{0}\"", subtitlePath.Replace("\"", "\\\"", StringComparison.Ordinal));
  365. string subtitleId = subtitlePath.GetMD5().ToString("N", CultureInfo.InvariantCulture);
  366. await _attachmentExtractor.ExtractAllAttachmentsExternal(subtitlePathArgument, subtitleId, attachmentPath, cancellationTokenSource.Token).ConfigureAwait(false);
  367. }
  368. }
  369. var process = new Process
  370. {
  371. StartInfo = new ProcessStartInfo
  372. {
  373. WindowStyle = ProcessWindowStyle.Hidden,
  374. CreateNoWindow = true,
  375. UseShellExecute = false,
  376. // Must consume both stdout and stderr or deadlocks may occur
  377. // RedirectStandardOutput = true,
  378. RedirectStandardError = true,
  379. RedirectStandardInput = true,
  380. FileName = _mediaEncoder.EncoderPath,
  381. Arguments = commandLineArguments,
  382. WorkingDirectory = string.IsNullOrWhiteSpace(workingDirectory) ? string.Empty : workingDirectory,
  383. ErrorDialog = false
  384. },
  385. EnableRaisingEvents = true
  386. };
  387. var transcodingJob = OnTranscodeBeginning(
  388. outputPath,
  389. state.Request.PlaySessionId,
  390. state.MediaSource.LiveStreamId,
  391. Guid.NewGuid().ToString("N", CultureInfo.InvariantCulture),
  392. transcodingJobType,
  393. process,
  394. state.Request.DeviceId,
  395. state,
  396. cancellationTokenSource);
  397. _logger.LogInformation("{Filename} {Arguments}", process.StartInfo.FileName, process.StartInfo.Arguments);
  398. var logFilePrefix = "FFmpeg.Transcode-";
  399. if (state.VideoRequest is not null
  400. && EncodingHelper.IsCopyCodec(state.OutputVideoCodec))
  401. {
  402. logFilePrefix = EncodingHelper.IsCopyCodec(state.OutputAudioCodec)
  403. ? "FFmpeg.Remux-"
  404. : "FFmpeg.DirectStream-";
  405. }
  406. if (state.VideoRequest is null && EncodingHelper.IsCopyCodec(state.OutputAudioCodec))
  407. {
  408. logFilePrefix = "FFmpeg.Remux-";
  409. }
  410. var logFilePath = Path.Combine(
  411. _serverConfigurationManager.ApplicationPaths.LogDirectoryPath,
  412. $"{logFilePrefix}{DateTime.Now:yyyy-MM-dd_HH-mm-ss}_{state.Request.MediaSourceId}_{Guid.NewGuid().ToString()[..8]}.log");
  413. // FFmpeg writes debug/error info to stderr. This is useful when debugging so let's put it in the log directory.
  414. Stream logStream = new FileStream(
  415. logFilePath,
  416. FileMode.Create,
  417. FileAccess.Write,
  418. FileShare.Read,
  419. IODefaults.FileStreamBufferSize,
  420. FileOptions.Asynchronous);
  421. await JsonSerializer.SerializeAsync(logStream, state.MediaSource, cancellationToken: cancellationTokenSource.Token).ConfigureAwait(false);
  422. var commandLineLogMessageBytes = Encoding.UTF8.GetBytes(
  423. Environment.NewLine
  424. + Environment.NewLine
  425. + process.StartInfo.FileName + " " + process.StartInfo.Arguments
  426. + Environment.NewLine
  427. + Environment.NewLine);
  428. await logStream.WriteAsync(commandLineLogMessageBytes, cancellationTokenSource.Token).ConfigureAwait(false);
  429. process.Exited += (_, _) => OnFfMpegProcessExited(process, transcodingJob, state);
  430. try
  431. {
  432. process.Start();
  433. }
  434. catch (Exception ex)
  435. {
  436. _logger.LogError(ex, "Error starting FFmpeg");
  437. OnTranscodeFailedToStart(outputPath, transcodingJobType, state);
  438. throw;
  439. }
  440. _logger.LogDebug("Launched FFmpeg process");
  441. state.TranscodingJob = transcodingJob;
  442. // Important - don't await the log task or we won't be able to kill FFmpeg when the user stops playback
  443. _ = new JobLogger(_logger).StartStreamingLog(state, process.StandardError, logStream);
  444. // Wait for the file to exist before proceeding
  445. var ffmpegTargetFile = state.WaitForPath ?? outputPath;
  446. _logger.LogDebug("Waiting for the creation of {0}", ffmpegTargetFile);
  447. while (!File.Exists(ffmpegTargetFile) && !transcodingJob.HasExited)
  448. {
  449. await Task.Delay(100, cancellationTokenSource.Token).ConfigureAwait(false);
  450. }
  451. _logger.LogDebug("File {0} created or transcoding has finished", ffmpegTargetFile);
  452. if (state.IsInputVideo && transcodingJob.Type == TranscodingJobType.Progressive && !transcodingJob.HasExited)
  453. {
  454. await Task.Delay(1000, cancellationTokenSource.Token).ConfigureAwait(false);
  455. if (state.ReadInputAtNativeFramerate && !transcodingJob.HasExited)
  456. {
  457. await Task.Delay(1500, cancellationTokenSource.Token).ConfigureAwait(false);
  458. }
  459. }
  460. if (!transcodingJob.HasExited)
  461. {
  462. StartThrottler(state, transcodingJob);
  463. StartSegmentCleaner(state, transcodingJob);
  464. }
  465. else if (transcodingJob.ExitCode != 0)
  466. {
  467. throw new FfmpegException(string.Format(CultureInfo.InvariantCulture, "FFmpeg exited with code {0}", transcodingJob.ExitCode));
  468. }
  469. _logger.LogDebug("StartFfMpeg() finished successfully");
  470. return transcodingJob;
  471. }
  472. private void StartThrottler(StreamState state, TranscodingJob transcodingJob)
  473. {
  474. if (EnableThrottling(state)
  475. && (_mediaEncoder.IsPkeyPauseSupported
  476. || _mediaEncoder.EncoderVersion <= _maxFFmpegCkeyPauseSupported))
  477. {
  478. transcodingJob.TranscodingThrottler = new TranscodingThrottler(transcodingJob, _loggerFactory.CreateLogger<TranscodingThrottler>(), _serverConfigurationManager, _fileSystem, _mediaEncoder);
  479. transcodingJob.TranscodingThrottler.Start();
  480. }
  481. }
  482. private static bool EnableThrottling(StreamState state)
  483. => state.InputProtocol == MediaProtocol.File
  484. && state.RunTimeTicks.HasValue
  485. && state.RunTimeTicks.Value >= TimeSpan.FromMinutes(5).Ticks
  486. && state.IsInputVideo
  487. && state.VideoType == VideoType.VideoFile;
  488. private void StartSegmentCleaner(StreamState state, TranscodingJob transcodingJob)
  489. {
  490. if (EnableSegmentCleaning(state))
  491. {
  492. transcodingJob.TranscodingSegmentCleaner = new TranscodingSegmentCleaner(transcodingJob, _loggerFactory.CreateLogger<TranscodingSegmentCleaner>(), _serverConfigurationManager, _fileSystem, _mediaEncoder, state.SegmentLength);
  493. transcodingJob.TranscodingSegmentCleaner.Start();
  494. }
  495. }
  496. private static bool EnableSegmentCleaning(StreamState state)
  497. => state.InputProtocol is MediaProtocol.File or MediaProtocol.Http
  498. && state.IsInputVideo
  499. && state.TranscodingType == TranscodingJobType.Hls
  500. && state.RunTimeTicks.HasValue
  501. && state.RunTimeTicks.Value >= TimeSpan.FromMinutes(5).Ticks;
  502. private TranscodingJob OnTranscodeBeginning(
  503. string path,
  504. string? playSessionId,
  505. string? liveStreamId,
  506. string transcodingJobId,
  507. TranscodingJobType type,
  508. Process process,
  509. string? deviceId,
  510. StreamState state,
  511. CancellationTokenSource cancellationTokenSource)
  512. {
  513. lock (_activeTranscodingJobs)
  514. {
  515. var job = new TranscodingJob(_loggerFactory.CreateLogger<TranscodingJob>())
  516. {
  517. Type = type,
  518. Path = path,
  519. Process = process,
  520. ActiveRequestCount = 1,
  521. DeviceId = deviceId,
  522. CancellationTokenSource = cancellationTokenSource,
  523. Id = transcodingJobId,
  524. PlaySessionId = playSessionId,
  525. LiveStreamId = liveStreamId,
  526. MediaSource = state.MediaSource
  527. };
  528. _activeTranscodingJobs.Add(job);
  529. ReportTranscodingProgress(job, state, null, null, null, null, null);
  530. return job;
  531. }
  532. }
  533. /// <inheritdoc />
  534. public void OnTranscodeEndRequest(TranscodingJob job)
  535. {
  536. job.ActiveRequestCount--;
  537. _logger.LogDebug("OnTranscodeEndRequest job.ActiveRequestCount={ActiveRequestCount}", job.ActiveRequestCount);
  538. if (job.ActiveRequestCount <= 0)
  539. {
  540. PingTimer(job, false);
  541. }
  542. }
  543. private void OnTranscodeFailedToStart(string path, TranscodingJobType type, StreamState state)
  544. {
  545. lock (_activeTranscodingJobs)
  546. {
  547. var job = _activeTranscodingJobs.FirstOrDefault(j => j.Type == type && string.Equals(j.Path, path, StringComparison.OrdinalIgnoreCase));
  548. if (job is not null)
  549. {
  550. _activeTranscodingJobs.Remove(job);
  551. }
  552. }
  553. if (!string.IsNullOrWhiteSpace(state.Request.DeviceId))
  554. {
  555. _sessionManager.ClearTranscodingInfo(state.Request.DeviceId);
  556. }
  557. }
  558. private void OnFfMpegProcessExited(Process process, TranscodingJob job, StreamState state)
  559. {
  560. job.HasExited = true;
  561. job.ExitCode = process.ExitCode;
  562. ReportTranscodingProgress(job, state, null, null, null, null, null);
  563. _logger.LogDebug("Disposing stream resources");
  564. state.Dispose();
  565. if (process.ExitCode == 0)
  566. {
  567. _logger.LogInformation("FFmpeg exited with code 0");
  568. }
  569. else
  570. {
  571. _logger.LogError("FFmpeg exited with code {0}", process.ExitCode);
  572. }
  573. job.Dispose();
  574. }
  575. private async Task AcquireResources(StreamState state, CancellationTokenSource cancellationTokenSource)
  576. {
  577. if (state.MediaSource.RequiresOpening && string.IsNullOrWhiteSpace(state.Request.LiveStreamId))
  578. {
  579. var liveStreamResponse = await _mediaSourceManager.OpenLiveStream(
  580. new LiveStreamRequest { OpenToken = state.MediaSource.OpenToken },
  581. cancellationTokenSource.Token)
  582. .ConfigureAwait(false);
  583. var encodingOptions = _serverConfigurationManager.GetEncodingOptions();
  584. _encodingHelper.AttachMediaSourceInfo(state, encodingOptions, liveStreamResponse.MediaSource, state.RequestedUrl);
  585. if (state.VideoRequest is not null)
  586. {
  587. _encodingHelper.TryStreamCopy(state);
  588. }
  589. }
  590. if (state.MediaSource.BufferMs.HasValue)
  591. {
  592. await Task.Delay(state.MediaSource.BufferMs.Value, cancellationTokenSource.Token).ConfigureAwait(false);
  593. }
  594. }
  595. /// <inheritdoc />
  596. public TranscodingJob? OnTranscodeBeginRequest(string path, TranscodingJobType type)
  597. {
  598. lock (_activeTranscodingJobs)
  599. {
  600. var job = _activeTranscodingJobs
  601. .FirstOrDefault(j => j.Type == type && string.Equals(j.Path, path, StringComparison.OrdinalIgnoreCase));
  602. if (job is null)
  603. {
  604. return null;
  605. }
  606. job.ActiveRequestCount++;
  607. if (string.IsNullOrWhiteSpace(job.PlaySessionId) || job.Type == TranscodingJobType.Progressive)
  608. {
  609. job.StopKillTimer();
  610. }
  611. return job;
  612. }
  613. }
  614. private void OnPlaybackProgress(object? sender, PlaybackProgressEventArgs e)
  615. {
  616. if (!string.IsNullOrWhiteSpace(e.PlaySessionId))
  617. {
  618. PingTranscodingJob(e.PlaySessionId, e.IsPaused);
  619. }
  620. }
  621. private void DeleteEncodedMediaCache()
  622. {
  623. var path = _serverConfigurationManager.GetTranscodePath();
  624. if (!Directory.Exists(path))
  625. {
  626. return;
  627. }
  628. foreach (var file in _fileSystem.GetFilePaths(path, true))
  629. {
  630. try
  631. {
  632. _fileSystem.DeleteFile(file);
  633. }
  634. catch (Exception ex)
  635. {
  636. _logger.LogError(ex, "Error deleting encoded media cache file {Path}", path);
  637. }
  638. }
  639. }
  640. /// <summary>
  641. /// Transcoding lock.
  642. /// </summary>
  643. /// <param name="outputPath">The output path of the transcoded file.</param>
  644. /// <param name="cancellationToken">The cancellation token.</param>
  645. /// <returns>An <see cref="IDisposable"/>.</returns>
  646. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  647. public ValueTask<IDisposable> LockAsync(string outputPath, CancellationToken cancellationToken)
  648. {
  649. return _transcodingLocks.LockAsync(outputPath, cancellationToken);
  650. }
  651. /// <inheritdoc />
  652. public void Dispose()
  653. {
  654. _sessionManager.PlaybackProgress -= OnPlaybackProgress;
  655. _sessionManager.PlaybackStart -= OnPlaybackProgress;
  656. _transcodingLocks.Dispose();
  657. }
  658. }