TranscodeManager.cs 27 KB

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