EncodingJobInfo.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  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 bool EnableMpDecimate
  114. {
  115. get { return MediaSource.EnableMpDecimate; }
  116. }
  117. public string AlbumCoverPath { get; set; }
  118. public string InputAudioSync { get; set; }
  119. public string InputVideoSync { get; set; }
  120. public TransportStreamTimestamp InputTimestamp { get; set; }
  121. public MediaStream AudioStream { get; set; }
  122. public List<string> SupportedAudioCodecs { get; set; }
  123. public List<string> SupportedVideoCodecs { get; set; }
  124. public string InputContainer { get; set; }
  125. public IsoType? IsoType { get; set; }
  126. public bool EnableMpegtsM2TsMode { get; set; }
  127. public BaseEncodingJobOptions BaseRequest { get; set; }
  128. public long? StartTimeTicks
  129. {
  130. get { return BaseRequest.StartTimeTicks; }
  131. }
  132. public bool CopyTimestamps
  133. {
  134. get { return BaseRequest.CopyTimestamps; }
  135. }
  136. public int? OutputAudioBitrate;
  137. public int? OutputAudioChannels;
  138. public bool DeInterlace(string videoCodec)
  139. {
  140. // Support general param
  141. if (BaseRequest.DeInterlace)
  142. {
  143. return true;
  144. }
  145. if (!string.IsNullOrWhiteSpace(videoCodec))
  146. {
  147. if (string.Equals(BaseRequest.GetOption(videoCodec, "deinterlace"), "true", StringComparison.OrdinalIgnoreCase))
  148. {
  149. return true;
  150. }
  151. }
  152. return false;
  153. }
  154. public string[] GetRequestedProfiles(string codec)
  155. {
  156. if (!string.IsNullOrWhiteSpace(BaseRequest.Profile))
  157. {
  158. return BaseRequest.Profile.Split(new[] { '|', ',' }, StringSplitOptions.RemoveEmptyEntries);
  159. }
  160. if (!string.IsNullOrWhiteSpace(codec))
  161. {
  162. var profile = BaseRequest.GetOption(codec, "profile");
  163. if (!string.IsNullOrWhiteSpace(profile))
  164. {
  165. return profile.Split(new[] { '|', ',' }, StringSplitOptions.RemoveEmptyEntries);
  166. }
  167. }
  168. return new string[] { };
  169. }
  170. public string GetRequestedLevel(string codec)
  171. {
  172. if (!string.IsNullOrWhiteSpace(BaseRequest.Level))
  173. {
  174. return BaseRequest.Level;
  175. }
  176. if (!string.IsNullOrWhiteSpace(codec))
  177. {
  178. return BaseRequest.GetOption(codec, "level");
  179. }
  180. return null;
  181. }
  182. public int? GetRequestedMaxRefFrames(string codec)
  183. {
  184. if (!string.IsNullOrWhiteSpace(BaseRequest.Level))
  185. {
  186. return BaseRequest.MaxRefFrames;
  187. }
  188. if (!string.IsNullOrWhiteSpace(codec))
  189. {
  190. var value = BaseRequest.GetOption(codec, "maxrefframes");
  191. int result;
  192. if (!string.IsNullOrWhiteSpace(value) && int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out result))
  193. {
  194. return result;
  195. }
  196. }
  197. return null;
  198. }
  199. public bool IsVideoRequest { get; set; }
  200. public TranscodingJobType TranscodingType { get; set; }
  201. public EncodingJobInfo(ILogger logger, TranscodingJobType jobType)
  202. {
  203. _logger = logger;
  204. TranscodingType = jobType;
  205. RemoteHttpHeaders = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
  206. PlayableStreamFileNames = new string[] { };
  207. SupportedAudioCodecs = new List<string>();
  208. SupportedVideoCodecs = new List<string>();
  209. SupportedSubtitleCodecs = new List<string>();
  210. }
  211. public bool IsSegmentedLiveStream
  212. {
  213. get
  214. {
  215. return TranscodingType != TranscodingJobType.Progressive && !RunTimeTicks.HasValue;
  216. }
  217. }
  218. public bool EnableBreakOnNonKeyFrames(string videoCodec)
  219. {
  220. if (TranscodingType != TranscodingJobType.Progressive)
  221. {
  222. if (IsSegmentedLiveStream)
  223. {
  224. return false;
  225. }
  226. return BaseRequest.BreakOnNonKeyFrames && string.Equals(videoCodec, "copy", StringComparison.OrdinalIgnoreCase);
  227. }
  228. return false;
  229. }
  230. public int? TotalOutputBitrate
  231. {
  232. get
  233. {
  234. return (OutputAudioBitrate ?? 0) + (OutputVideoBitrate ?? 0);
  235. }
  236. }
  237. public int? OutputWidth
  238. {
  239. get
  240. {
  241. if (VideoStream != null && VideoStream.Width.HasValue && VideoStream.Height.HasValue)
  242. {
  243. var size = new ImageSize
  244. {
  245. Width = VideoStream.Width.Value,
  246. Height = VideoStream.Height.Value
  247. };
  248. var newSize = DrawingUtils.Resize(size,
  249. BaseRequest.Width,
  250. BaseRequest.Height,
  251. BaseRequest.MaxWidth,
  252. BaseRequest.MaxHeight);
  253. return Convert.ToInt32(newSize.Width);
  254. }
  255. if (!IsVideoRequest)
  256. {
  257. return null;
  258. }
  259. return BaseRequest.MaxWidth ?? BaseRequest.Width;
  260. }
  261. }
  262. public int? OutputHeight
  263. {
  264. get
  265. {
  266. if (VideoStream != null && VideoStream.Width.HasValue && VideoStream.Height.HasValue)
  267. {
  268. var size = new ImageSize
  269. {
  270. Width = VideoStream.Width.Value,
  271. Height = VideoStream.Height.Value
  272. };
  273. var newSize = DrawingUtils.Resize(size,
  274. BaseRequest.Width,
  275. BaseRequest.Height,
  276. BaseRequest.MaxWidth,
  277. BaseRequest.MaxHeight);
  278. return Convert.ToInt32(newSize.Height);
  279. }
  280. if (!IsVideoRequest)
  281. {
  282. return null;
  283. }
  284. return BaseRequest.MaxHeight ?? BaseRequest.Height;
  285. }
  286. }
  287. public int? OutputAudioSampleRate
  288. {
  289. get
  290. {
  291. if (BaseRequest.Static || string.Equals(OutputAudioCodec, "copy", StringComparison.OrdinalIgnoreCase))
  292. {
  293. if (AudioStream != null)
  294. {
  295. return AudioStream.SampleRate;
  296. }
  297. }
  298. else if (BaseRequest.AudioSampleRate.HasValue)
  299. {
  300. // Don't exceed what the encoder supports
  301. // Seeing issues of attempting to encode to 88200
  302. return Math.Min(44100, BaseRequest.AudioSampleRate.Value);
  303. }
  304. return null;
  305. }
  306. }
  307. public int? OutputAudioBitDepth
  308. {
  309. get
  310. {
  311. if (BaseRequest.Static || string.Equals(OutputAudioCodec, "copy", StringComparison.OrdinalIgnoreCase))
  312. {
  313. if (AudioStream != null)
  314. {
  315. return AudioStream.BitDepth;
  316. }
  317. }
  318. //else if (BaseRequest.AudioSampleRate.HasValue)
  319. //{
  320. // // Don't exceed what the encoder supports
  321. // // Seeing issues of attempting to encode to 88200
  322. // return Math.Min(44100, BaseRequest.AudioSampleRate.Value);
  323. //}
  324. return null;
  325. }
  326. }
  327. /// <summary>
  328. /// Predicts the audio sample rate that will be in the output stream
  329. /// </summary>
  330. public double? TargetVideoLevel
  331. {
  332. get
  333. {
  334. if (BaseRequest.Static)
  335. {
  336. return VideoStream == null ? null : VideoStream.Level;
  337. }
  338. var level = GetRequestedLevel(ActualOutputVideoCodec);
  339. double result;
  340. if (!string.IsNullOrWhiteSpace(level) && double.TryParse(level, NumberStyles.Any, CultureInfo.InvariantCulture, out result))
  341. {
  342. return result;
  343. }
  344. return null;
  345. }
  346. }
  347. /// <summary>
  348. /// Predicts the audio sample rate that will be in the output stream
  349. /// </summary>
  350. public int? TargetVideoBitDepth
  351. {
  352. get
  353. {
  354. var stream = VideoStream;
  355. return stream == null || !BaseRequest.Static ? null : stream.BitDepth;
  356. }
  357. }
  358. /// <summary>
  359. /// Gets the target reference frames.
  360. /// </summary>
  361. /// <value>The target reference frames.</value>
  362. public int? TargetRefFrames
  363. {
  364. get
  365. {
  366. if (BaseRequest.Static)
  367. {
  368. return VideoStream == null ? null : VideoStream.RefFrames;
  369. }
  370. return null;
  371. }
  372. }
  373. /// <summary>
  374. /// Predicts the audio sample rate that will be in the output stream
  375. /// </summary>
  376. public float? TargetFramerate
  377. {
  378. get
  379. {
  380. var stream = VideoStream;
  381. var requestedFramerate = BaseRequest.MaxFramerate ?? BaseRequest.Framerate;
  382. return requestedFramerate.HasValue && !BaseRequest.Static
  383. ? requestedFramerate
  384. : stream == null ? null : stream.AverageFrameRate ?? stream.RealFrameRate;
  385. }
  386. }
  387. public TransportStreamTimestamp TargetTimestamp
  388. {
  389. get
  390. {
  391. var defaultValue = string.Equals(OutputContainer, "m2ts", StringComparison.OrdinalIgnoreCase) ?
  392. TransportStreamTimestamp.Valid :
  393. TransportStreamTimestamp.None;
  394. return !BaseRequest.Static
  395. ? defaultValue
  396. : InputTimestamp;
  397. }
  398. }
  399. /// <summary>
  400. /// Predicts the audio sample rate that will be in the output stream
  401. /// </summary>
  402. public int? TargetPacketLength
  403. {
  404. get
  405. {
  406. var stream = VideoStream;
  407. return !BaseRequest.Static
  408. ? null
  409. : stream == null ? null : stream.PacketLength;
  410. }
  411. }
  412. /// <summary>
  413. /// Predicts the audio sample rate that will be in the output stream
  414. /// </summary>
  415. public string TargetVideoProfile
  416. {
  417. get
  418. {
  419. if (BaseRequest.Static)
  420. {
  421. return VideoStream == null ? null : VideoStream.Profile;
  422. }
  423. var requestedProfile = GetRequestedProfiles(ActualOutputVideoCodec).FirstOrDefault();
  424. if (!string.IsNullOrWhiteSpace(requestedProfile))
  425. {
  426. return requestedProfile;
  427. }
  428. return null;
  429. }
  430. }
  431. public string TargetVideoCodecTag
  432. {
  433. get
  434. {
  435. var stream = VideoStream;
  436. return !BaseRequest.Static
  437. ? null
  438. : stream == null ? null : stream.CodecTag;
  439. }
  440. }
  441. public bool? IsTargetAnamorphic
  442. {
  443. get
  444. {
  445. if (BaseRequest.Static)
  446. {
  447. return VideoStream == null ? null : VideoStream.IsAnamorphic;
  448. }
  449. return false;
  450. }
  451. }
  452. public string ActualOutputVideoCodec
  453. {
  454. get
  455. {
  456. var codec = OutputVideoCodec;
  457. if (string.Equals(codec, "copy", StringComparison.OrdinalIgnoreCase))
  458. {
  459. var stream = VideoStream;
  460. if (stream != null)
  461. {
  462. return stream.Codec;
  463. }
  464. return null;
  465. }
  466. return codec;
  467. }
  468. }
  469. public bool? IsTargetInterlaced
  470. {
  471. get
  472. {
  473. if (BaseRequest.Static)
  474. {
  475. return VideoStream == null ? (bool?)null : VideoStream.IsInterlaced;
  476. }
  477. if (DeInterlace(ActualOutputVideoCodec))
  478. {
  479. return false;
  480. }
  481. return VideoStream == null ? (bool?)null : VideoStream.IsInterlaced;
  482. }
  483. }
  484. public bool? IsTargetAVC
  485. {
  486. get
  487. {
  488. if (BaseRequest.Static)
  489. {
  490. return VideoStream == null ? null : VideoStream.IsAVC;
  491. }
  492. return false;
  493. }
  494. }
  495. public int? TargetVideoStreamCount
  496. {
  497. get
  498. {
  499. if (BaseRequest.Static)
  500. {
  501. return GetMediaStreamCount(MediaStreamType.Video, int.MaxValue);
  502. }
  503. return GetMediaStreamCount(MediaStreamType.Video, 1);
  504. }
  505. }
  506. public int? TargetAudioStreamCount
  507. {
  508. get
  509. {
  510. if (BaseRequest.Static)
  511. {
  512. return GetMediaStreamCount(MediaStreamType.Audio, int.MaxValue);
  513. }
  514. return GetMediaStreamCount(MediaStreamType.Audio, 1);
  515. }
  516. }
  517. private int? GetMediaStreamCount(MediaStreamType type, int limit)
  518. {
  519. var count = MediaSource.GetStreamCount(type);
  520. if (count.HasValue)
  521. {
  522. count = Math.Min(count.Value, limit);
  523. }
  524. return count;
  525. }
  526. protected void DisposeIsoMount()
  527. {
  528. if (IsoMount != null)
  529. {
  530. try
  531. {
  532. IsoMount.Dispose();
  533. }
  534. catch (Exception ex)
  535. {
  536. _logger.ErrorException("Error disposing iso mount", ex);
  537. }
  538. IsoMount = null;
  539. }
  540. }
  541. public abstract void ReportTranscodingProgress(TimeSpan? transcodingPosition, float? framerate, double? percentComplete, long? bytesTranscoded, int? bitRate);
  542. }
  543. /// <summary>
  544. /// Enum TranscodingJobType
  545. /// </summary>
  546. public enum TranscodingJobType
  547. {
  548. /// <summary>
  549. /// The progressive
  550. /// </summary>
  551. Progressive,
  552. /// <summary>
  553. /// The HLS
  554. /// </summary>
  555. Hls,
  556. /// <summary>
  557. /// The dash
  558. /// </summary>
  559. Dash
  560. }
  561. }