EncodingJobInfo.cs 19 KB

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