EncodingJobInfo.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using MediaBrowser.Controller.Entities;
  5. using MediaBrowser.Model.Dlna;
  6. using MediaBrowser.Model.Dto;
  7. using MediaBrowser.Model.Entities;
  8. using MediaBrowser.Model.IO;
  9. using MediaBrowser.Model.Logging;
  10. using MediaBrowser.Model.MediaInfo;
  11. namespace MediaBrowser.Controller.MediaEncoding
  12. {
  13. // For now, a common base class until the API and MediaEncoding classes are unified
  14. public abstract class EncodingJobInfo
  15. {
  16. private readonly ILogger _logger;
  17. public MediaStream VideoStream { get; set; }
  18. public VideoType VideoType { get; set; }
  19. public Dictionary<string, string> RemoteHttpHeaders { get; set; }
  20. public string OutputVideoCodec { get; set; }
  21. public MediaProtocol InputProtocol { get; set; }
  22. public string MediaPath { get; set; }
  23. public bool IsInputVideo { get; set; }
  24. public IIsoMount IsoMount { get; set; }
  25. public List<string> PlayableStreamFileNames { get; set; }
  26. public string OutputAudioCodec { get; set; }
  27. public int? OutputVideoBitrate { get; set; }
  28. public MediaStream SubtitleStream { get; set; }
  29. public SubtitleDeliveryMethod SubtitleDeliveryMethod { get; set; }
  30. public List<string> SupportedSubtitleCodecs { get; set; }
  31. public int InternalSubtitleStreamOffset { get; set; }
  32. public MediaSourceInfo MediaSource { get; set; }
  33. public User User { get; set; }
  34. public long? RunTimeTicks { get; set; }
  35. public bool ReadInputAtNativeFramerate { get; set; }
  36. public string OutputContainer { get; set; }
  37. public string OutputVideoSync
  38. {
  39. get
  40. {
  41. // For live tv + recordings
  42. if (string.Equals(InputContainer, "mpegts", StringComparison.OrdinalIgnoreCase) ||
  43. string.Equals(InputContainer, "ts", StringComparison.OrdinalIgnoreCase))
  44. {
  45. return "cfr";
  46. }
  47. return "-1";
  48. }
  49. }
  50. public string AlbumCoverPath { get; set; }
  51. public string InputAudioSync { get; set; }
  52. public string InputVideoSync { get; set; }
  53. public TransportStreamTimestamp InputTimestamp { get; set; }
  54. public MediaStream AudioStream { get; set; }
  55. public List<string> SupportedAudioCodecs { get; set; }
  56. public List<string> SupportedVideoCodecs { get; set; }
  57. public string InputContainer { get; set; }
  58. public IsoType? IsoType { get; set; }
  59. public bool EnableMpegtsM2TsMode { get; set; }
  60. public BaseEncodingJobOptions BaseRequest { get; set; }
  61. public long? StartTimeTicks
  62. {
  63. get { return BaseRequest.StartTimeTicks; }
  64. }
  65. public bool CopyTimestamps
  66. {
  67. get { return BaseRequest.CopyTimestamps; }
  68. }
  69. public int? OutputAudioBitrate;
  70. public int? OutputAudioChannels;
  71. public int? OutputAudioSampleRate;
  72. public bool DeInterlace { get; set; }
  73. public bool IsVideoRequest { get; set; }
  74. public TranscodingJobType TranscodingType { get; set; }
  75. public EncodingJobInfo(ILogger logger, TranscodingJobType jobType)
  76. {
  77. _logger = logger;
  78. TranscodingType = jobType;
  79. RemoteHttpHeaders = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
  80. PlayableStreamFileNames = new List<string>();
  81. SupportedAudioCodecs = new List<string>();
  82. SupportedVideoCodecs = new List<string>();
  83. SupportedSubtitleCodecs = new List<string>();
  84. }
  85. public bool IsSegmentedLiveStream
  86. {
  87. get
  88. {
  89. return TranscodingType != TranscodingJobType.Progressive && !RunTimeTicks.HasValue;
  90. }
  91. }
  92. public bool EnableBreakOnNonKeyFrames(string videoCodec)
  93. {
  94. if (TranscodingType != TranscodingJobType.Progressive)
  95. {
  96. if (IsSegmentedLiveStream)
  97. {
  98. return false;
  99. }
  100. return BaseRequest.BreakOnNonKeyFrames && string.Equals(videoCodec, "copy", StringComparison.OrdinalIgnoreCase);
  101. }
  102. return false;
  103. }
  104. /// <summary>
  105. /// Predicts the audio sample rate that will be in the output stream
  106. /// </summary>
  107. public double? TargetVideoLevel
  108. {
  109. get
  110. {
  111. var stream = VideoStream;
  112. var request = BaseRequest;
  113. return !string.IsNullOrEmpty(request.Level) && !request.Static
  114. ? double.Parse(request.Level, CultureInfo.InvariantCulture)
  115. : stream == null ? null : stream.Level;
  116. }
  117. }
  118. protected void DisposeIsoMount()
  119. {
  120. if (IsoMount != null)
  121. {
  122. try
  123. {
  124. IsoMount.Dispose();
  125. }
  126. catch (Exception ex)
  127. {
  128. _logger.ErrorException("Error disposing iso mount", ex);
  129. }
  130. IsoMount = null;
  131. }
  132. }
  133. public abstract void ReportTranscodingProgress(TimeSpan? transcodingPosition, float? framerate, double? percentComplete, long? bytesTranscoded, int? bitRate);
  134. }
  135. /// <summary>
  136. /// Enum TranscodingJobType
  137. /// </summary>
  138. public enum TranscodingJobType
  139. {
  140. /// <summary>
  141. /// The progressive
  142. /// </summary>
  143. Progressive,
  144. /// <summary>
  145. /// The HLS
  146. /// </summary>
  147. Hls,
  148. /// <summary>
  149. /// The dash
  150. /// </summary>
  151. Dash
  152. }
  153. }