2
0

EncodingJobInfo.cs 23 KB

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