EncodingJobInfo.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. #nullable disable
  2. #pragma warning disable CS1591, SA1401
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Globalization;
  6. using System.Linq;
  7. using Jellyfin.Data.Entities;
  8. using MediaBrowser.Model.Dlna;
  9. using MediaBrowser.Model.Drawing;
  10. using MediaBrowser.Model.Dto;
  11. using MediaBrowser.Model.Entities;
  12. using MediaBrowser.Model.MediaInfo;
  13. using MediaBrowser.Model.Net;
  14. using MediaBrowser.Model.Session;
  15. namespace MediaBrowser.Controller.MediaEncoding
  16. {
  17. // For now, a common base class until the API and MediaEncoding classes are unified
  18. public class EncodingJobInfo
  19. {
  20. public int? OutputAudioBitrate;
  21. public int? OutputAudioChannels;
  22. private TranscodeReason[] _transcodeReasons = null;
  23. public EncodingJobInfo(TranscodingJobType jobType)
  24. {
  25. TranscodingType = jobType;
  26. RemoteHttpHeaders = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
  27. SupportedAudioCodecs = Array.Empty<string>();
  28. SupportedVideoCodecs = Array.Empty<string>();
  29. SupportedSubtitleCodecs = Array.Empty<string>();
  30. }
  31. public TranscodeReason[] TranscodeReasons
  32. {
  33. get
  34. {
  35. if (_transcodeReasons == null)
  36. {
  37. if (BaseRequest.TranscodeReasons == null)
  38. {
  39. return Array.Empty<TranscodeReason>();
  40. }
  41. _transcodeReasons = BaseRequest.TranscodeReasons
  42. .Split(',')
  43. .Where(i => !string.IsNullOrEmpty(i))
  44. .Select(v => (TranscodeReason)Enum.Parse(typeof(TranscodeReason), v, true))
  45. .ToArray();
  46. }
  47. return _transcodeReasons;
  48. }
  49. }
  50. public IProgress<double> Progress { get; set; }
  51. public MediaStream VideoStream { get; set; }
  52. public VideoType VideoType { get; set; }
  53. public Dictionary<string, string> RemoteHttpHeaders { get; set; }
  54. public string OutputVideoCodec { get; set; }
  55. public MediaProtocol InputProtocol { get; set; }
  56. public string MediaPath { get; set; }
  57. public bool IsInputVideo { get; set; }
  58. public string OutputAudioCodec { get; set; }
  59. public int? OutputVideoBitrate { get; set; }
  60. public MediaStream SubtitleStream { get; set; }
  61. public SubtitleDeliveryMethod SubtitleDeliveryMethod { get; set; }
  62. public string[] SupportedSubtitleCodecs { get; set; }
  63. public int InternalSubtitleStreamOffset { get; set; }
  64. public MediaSourceInfo MediaSource { get; set; }
  65. public User User { get; set; }
  66. public long? RunTimeTicks { get; set; }
  67. public bool ReadInputAtNativeFramerate { get; set; }
  68. public string OutputFilePath { get; set; }
  69. public string MimeType { get; set; }
  70. public bool IgnoreInputDts => MediaSource.IgnoreDts;
  71. public bool IgnoreInputIndex => MediaSource.IgnoreIndex;
  72. public bool GenPtsInput => MediaSource.GenPtsInput;
  73. public bool DiscardCorruptFramesInput => false;
  74. public bool EnableFastSeekInput => false;
  75. public bool GenPtsOutput => false;
  76. public string OutputContainer { get; set; }
  77. public string OutputVideoSync
  78. {
  79. get
  80. {
  81. // For live tv + in progress recordings
  82. if (string.Equals(InputContainer, "mpegts", StringComparison.OrdinalIgnoreCase)
  83. || string.Equals(InputContainer, "ts", StringComparison.OrdinalIgnoreCase))
  84. {
  85. if (!MediaSource.RunTimeTicks.HasValue)
  86. {
  87. return "cfr";
  88. }
  89. }
  90. return "-1";
  91. }
  92. }
  93. public string AlbumCoverPath { get; set; }
  94. public string InputAudioSync { get; set; }
  95. public string InputVideoSync { get; set; }
  96. public TransportStreamTimestamp InputTimestamp { get; set; }
  97. public MediaStream AudioStream { get; set; }
  98. public string[] SupportedAudioCodecs { get; set; }
  99. public string[] SupportedVideoCodecs { get; set; }
  100. public string InputContainer { get; set; }
  101. public IsoType? IsoType { get; set; }
  102. public BaseEncodingJobOptions BaseRequest { get; set; }
  103. public bool IsVideoRequest { get; set; }
  104. public TranscodingJobType TranscodingType { get; set; }
  105. public long? StartTimeTicks => BaseRequest.StartTimeTicks;
  106. public bool CopyTimestamps => BaseRequest.CopyTimestamps;
  107. public bool IsSegmentedLiveStream
  108. => TranscodingType != TranscodingJobType.Progressive && !RunTimeTicks.HasValue;
  109. public int? TotalOutputBitrate => (OutputAudioBitrate ?? 0) + (OutputVideoBitrate ?? 0);
  110. public int? OutputWidth
  111. {
  112. get
  113. {
  114. if (VideoStream != null && VideoStream.Width.HasValue && VideoStream.Height.HasValue)
  115. {
  116. var size = new ImageDimensions(VideoStream.Width.Value, VideoStream.Height.Value);
  117. var newSize = DrawingUtils.Resize(
  118. size,
  119. BaseRequest.Width ?? 0,
  120. BaseRequest.Height ?? 0,
  121. BaseRequest.MaxWidth ?? 0,
  122. BaseRequest.MaxHeight ?? 0);
  123. return newSize.Width;
  124. }
  125. if (!IsVideoRequest)
  126. {
  127. return null;
  128. }
  129. return BaseRequest.MaxWidth ?? BaseRequest.Width;
  130. }
  131. }
  132. public int? OutputHeight
  133. {
  134. get
  135. {
  136. if (VideoStream != null && VideoStream.Width.HasValue && VideoStream.Height.HasValue)
  137. {
  138. var size = new ImageDimensions(VideoStream.Width.Value, VideoStream.Height.Value);
  139. var newSize = DrawingUtils.Resize(
  140. size,
  141. BaseRequest.Width ?? 0,
  142. BaseRequest.Height ?? 0,
  143. BaseRequest.MaxWidth ?? 0,
  144. BaseRequest.MaxHeight ?? 0);
  145. return newSize.Height;
  146. }
  147. if (!IsVideoRequest)
  148. {
  149. return null;
  150. }
  151. return BaseRequest.MaxHeight ?? BaseRequest.Height;
  152. }
  153. }
  154. public int? OutputAudioSampleRate
  155. {
  156. get
  157. {
  158. if (BaseRequest.Static
  159. || EncodingHelper.IsCopyCodec(OutputAudioCodec))
  160. {
  161. if (AudioStream != null)
  162. {
  163. return AudioStream.SampleRate;
  164. }
  165. }
  166. else if (BaseRequest.AudioSampleRate.HasValue)
  167. {
  168. // Don't exceed what the encoder supports
  169. // Seeing issues of attempting to encode to 88200
  170. return BaseRequest.AudioSampleRate.Value;
  171. }
  172. return null;
  173. }
  174. }
  175. public int? OutputAudioBitDepth
  176. {
  177. get
  178. {
  179. if (BaseRequest.Static
  180. || EncodingHelper.IsCopyCodec(OutputAudioCodec))
  181. {
  182. if (AudioStream != null)
  183. {
  184. return AudioStream.BitDepth;
  185. }
  186. }
  187. return null;
  188. }
  189. }
  190. /// <summary>
  191. /// Gets the target video level.
  192. /// </summary>
  193. public double? TargetVideoLevel
  194. {
  195. get
  196. {
  197. if (BaseRequest.Static || EncodingHelper.IsCopyCodec(OutputVideoCodec))
  198. {
  199. return VideoStream?.Level;
  200. }
  201. var level = GetRequestedLevel(ActualOutputVideoCodec);
  202. if (!string.IsNullOrEmpty(level)
  203. && double.TryParse(level, NumberStyles.Any, CultureInfo.InvariantCulture, out var result))
  204. {
  205. return result;
  206. }
  207. return null;
  208. }
  209. }
  210. /// <summary>
  211. /// Gets the target video bit depth.
  212. /// </summary>
  213. public int? TargetVideoBitDepth
  214. {
  215. get
  216. {
  217. if (BaseRequest.Static
  218. || EncodingHelper.IsCopyCodec(OutputVideoCodec))
  219. {
  220. return VideoStream?.BitDepth;
  221. }
  222. return null;
  223. }
  224. }
  225. /// <summary>
  226. /// Gets the target reference frames.
  227. /// </summary>
  228. /// <value>The target reference frames.</value>
  229. public int? TargetRefFrames
  230. {
  231. get
  232. {
  233. if (BaseRequest.Static
  234. || EncodingHelper.IsCopyCodec(OutputVideoCodec))
  235. {
  236. return VideoStream?.RefFrames;
  237. }
  238. return null;
  239. }
  240. }
  241. /// <summary>
  242. /// Gets the target framerate.
  243. /// </summary>
  244. public float? TargetFramerate
  245. {
  246. get
  247. {
  248. if (BaseRequest.Static
  249. || EncodingHelper.IsCopyCodec(OutputVideoCodec))
  250. {
  251. return VideoStream == null ? null : (VideoStream.AverageFrameRate ?? VideoStream.RealFrameRate);
  252. }
  253. return BaseRequest.MaxFramerate ?? BaseRequest.Framerate;
  254. }
  255. }
  256. public TransportStreamTimestamp TargetTimestamp
  257. {
  258. get
  259. {
  260. if (BaseRequest.Static)
  261. {
  262. return InputTimestamp;
  263. }
  264. return string.Equals(OutputContainer, "m2ts", StringComparison.OrdinalIgnoreCase) ?
  265. TransportStreamTimestamp.Valid :
  266. TransportStreamTimestamp.None;
  267. }
  268. }
  269. /// <summary>
  270. /// Gets the target packet length.
  271. /// </summary>
  272. public int? TargetPacketLength
  273. {
  274. get
  275. {
  276. if (BaseRequest.Static || EncodingHelper.IsCopyCodec(OutputVideoCodec))
  277. {
  278. return VideoStream?.PacketLength;
  279. }
  280. return null;
  281. }
  282. }
  283. /// <summary>
  284. /// Gets the target video profile.
  285. /// </summary>
  286. public string TargetVideoProfile
  287. {
  288. get
  289. {
  290. if (BaseRequest.Static || EncodingHelper.IsCopyCodec(OutputVideoCodec))
  291. {
  292. return VideoStream?.Profile;
  293. }
  294. var requestedProfile = GetRequestedProfiles(ActualOutputVideoCodec).FirstOrDefault();
  295. if (!string.IsNullOrEmpty(requestedProfile))
  296. {
  297. return requestedProfile;
  298. }
  299. return null;
  300. }
  301. }
  302. public string TargetVideoCodecTag
  303. {
  304. get
  305. {
  306. if (BaseRequest.Static
  307. || EncodingHelper.IsCopyCodec(OutputVideoCodec))
  308. {
  309. return VideoStream?.CodecTag;
  310. }
  311. return null;
  312. }
  313. }
  314. public bool? IsTargetAnamorphic
  315. {
  316. get
  317. {
  318. if (BaseRequest.Static
  319. || EncodingHelper.IsCopyCodec(OutputVideoCodec))
  320. {
  321. return VideoStream?.IsAnamorphic;
  322. }
  323. return false;
  324. }
  325. }
  326. public string ActualOutputVideoCodec
  327. {
  328. get
  329. {
  330. if (VideoStream == null)
  331. {
  332. return null;
  333. }
  334. if (EncodingHelper.IsCopyCodec(OutputVideoCodec))
  335. {
  336. return VideoStream?.Codec;
  337. }
  338. return OutputVideoCodec;
  339. }
  340. }
  341. public string ActualOutputAudioCodec
  342. {
  343. get
  344. {
  345. if (AudioStream == null)
  346. {
  347. return null;
  348. }
  349. if (EncodingHelper.IsCopyCodec(OutputAudioCodec))
  350. {
  351. return AudioStream?.Codec;
  352. }
  353. return OutputAudioCodec;
  354. }
  355. }
  356. public bool? IsTargetInterlaced
  357. {
  358. get
  359. {
  360. if (BaseRequest.Static
  361. || EncodingHelper.IsCopyCodec(OutputVideoCodec))
  362. {
  363. return VideoStream?.IsInterlaced;
  364. }
  365. if (DeInterlace(ActualOutputVideoCodec, true))
  366. {
  367. return false;
  368. }
  369. return VideoStream?.IsInterlaced;
  370. }
  371. }
  372. public bool? IsTargetAVC
  373. {
  374. get
  375. {
  376. if (BaseRequest.Static || EncodingHelper.IsCopyCodec(OutputVideoCodec))
  377. {
  378. return VideoStream?.IsAVC;
  379. }
  380. return false;
  381. }
  382. }
  383. public int? TargetVideoStreamCount
  384. {
  385. get
  386. {
  387. if (BaseRequest.Static)
  388. {
  389. return GetMediaStreamCount(MediaStreamType.Video, int.MaxValue);
  390. }
  391. return GetMediaStreamCount(MediaStreamType.Video, 1);
  392. }
  393. }
  394. public int? TargetAudioStreamCount
  395. {
  396. get
  397. {
  398. if (BaseRequest.Static)
  399. {
  400. return GetMediaStreamCount(MediaStreamType.Audio, int.MaxValue);
  401. }
  402. return GetMediaStreamCount(MediaStreamType.Audio, 1);
  403. }
  404. }
  405. public int HlsListSize => 0;
  406. public bool EnableBreakOnNonKeyFrames(string videoCodec)
  407. {
  408. if (TranscodingType != TranscodingJobType.Progressive)
  409. {
  410. if (IsSegmentedLiveStream)
  411. {
  412. return false;
  413. }
  414. return BaseRequest.BreakOnNonKeyFrames && EncodingHelper.IsCopyCodec(videoCodec);
  415. }
  416. return false;
  417. }
  418. private int? GetMediaStreamCount(MediaStreamType type, int limit)
  419. {
  420. var count = MediaSource.GetStreamCount(type);
  421. if (count.HasValue)
  422. {
  423. count = Math.Min(count.Value, limit);
  424. }
  425. return count;
  426. }
  427. public string GetMimeType(string outputPath, bool enableStreamDefault = true)
  428. {
  429. if (!string.IsNullOrEmpty(MimeType))
  430. {
  431. return MimeType;
  432. }
  433. return MimeTypes.GetMimeType(outputPath, enableStreamDefault);
  434. }
  435. public bool DeInterlace(string videoCodec, bool forceDeinterlaceIfSourceIsInterlaced)
  436. {
  437. var videoStream = VideoStream;
  438. var isInputInterlaced = videoStream != null && videoStream.IsInterlaced;
  439. if (!isInputInterlaced)
  440. {
  441. return false;
  442. }
  443. // Support general param
  444. if (BaseRequest.DeInterlace)
  445. {
  446. return true;
  447. }
  448. if (!string.IsNullOrEmpty(videoCodec))
  449. {
  450. if (string.Equals(BaseRequest.GetOption(videoCodec, "deinterlace"), "true", StringComparison.OrdinalIgnoreCase))
  451. {
  452. return true;
  453. }
  454. }
  455. return forceDeinterlaceIfSourceIsInterlaced && isInputInterlaced;
  456. }
  457. public string[] GetRequestedProfiles(string codec)
  458. {
  459. if (!string.IsNullOrEmpty(BaseRequest.Profile))
  460. {
  461. return BaseRequest.Profile.Split(new[] { '|', ',' }, StringSplitOptions.RemoveEmptyEntries);
  462. }
  463. if (!string.IsNullOrEmpty(codec))
  464. {
  465. var profile = BaseRequest.GetOption(codec, "profile");
  466. if (!string.IsNullOrEmpty(profile))
  467. {
  468. return profile.Split(new[] { '|', ',' }, StringSplitOptions.RemoveEmptyEntries);
  469. }
  470. }
  471. return Array.Empty<string>();
  472. }
  473. public string GetRequestedLevel(string codec)
  474. {
  475. if (!string.IsNullOrEmpty(BaseRequest.Level))
  476. {
  477. return BaseRequest.Level;
  478. }
  479. if (!string.IsNullOrEmpty(codec))
  480. {
  481. return BaseRequest.GetOption(codec, "level");
  482. }
  483. return null;
  484. }
  485. public int? GetRequestedMaxRefFrames(string codec)
  486. {
  487. if (BaseRequest.MaxRefFrames.HasValue)
  488. {
  489. return BaseRequest.MaxRefFrames;
  490. }
  491. if (!string.IsNullOrEmpty(codec))
  492. {
  493. var value = BaseRequest.GetOption(codec, "maxrefframes");
  494. if (!string.IsNullOrEmpty(value)
  495. && int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out var result))
  496. {
  497. return result;
  498. }
  499. }
  500. return null;
  501. }
  502. public int? GetRequestedVideoBitDepth(string codec)
  503. {
  504. if (BaseRequest.MaxVideoBitDepth.HasValue)
  505. {
  506. return BaseRequest.MaxVideoBitDepth;
  507. }
  508. if (!string.IsNullOrEmpty(codec))
  509. {
  510. var value = BaseRequest.GetOption(codec, "videobitdepth");
  511. if (!string.IsNullOrEmpty(value)
  512. && int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out var result))
  513. {
  514. return result;
  515. }
  516. }
  517. return null;
  518. }
  519. public int? GetRequestedAudioBitDepth(string codec)
  520. {
  521. if (BaseRequest.MaxAudioBitDepth.HasValue)
  522. {
  523. return BaseRequest.MaxAudioBitDepth;
  524. }
  525. if (!string.IsNullOrEmpty(codec))
  526. {
  527. var value = BaseRequest.GetOption(codec, "audiobitdepth");
  528. if (!string.IsNullOrEmpty(value)
  529. && int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out var result))
  530. {
  531. return result;
  532. }
  533. }
  534. return null;
  535. }
  536. public int? GetRequestedAudioChannels(string codec)
  537. {
  538. if (!string.IsNullOrEmpty(codec))
  539. {
  540. var value = BaseRequest.GetOption(codec, "audiochannels");
  541. if (!string.IsNullOrEmpty(value)
  542. && int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out var result))
  543. {
  544. return result;
  545. }
  546. }
  547. if (BaseRequest.MaxAudioChannels.HasValue)
  548. {
  549. return BaseRequest.MaxAudioChannels;
  550. }
  551. if (BaseRequest.AudioChannels.HasValue)
  552. {
  553. return BaseRequest.AudioChannels;
  554. }
  555. if (BaseRequest.TranscodingMaxAudioChannels.HasValue)
  556. {
  557. return BaseRequest.TranscodingMaxAudioChannels;
  558. }
  559. return null;
  560. }
  561. public virtual void ReportTranscodingProgress(TimeSpan? transcodingPosition, float? framerate, double? percentComplete, long? bytesTranscoded, int? bitRate)
  562. {
  563. Progress.Report(percentComplete.Value);
  564. }
  565. }
  566. }