EncodingJob.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. using MediaBrowser.Controller.LiveTv;
  2. using MediaBrowser.Controller.MediaEncoding;
  3. using MediaBrowser.Model.Dlna;
  4. using MediaBrowser.Model.Drawing;
  5. using MediaBrowser.Model.Entities;
  6. using MediaBrowser.Model.IO;
  7. using MediaBrowser.Model.Logging;
  8. using MediaBrowser.Model.MediaInfo;
  9. using MediaBrowser.Model.Net;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.IO;
  13. using System.Threading;
  14. using System.Threading.Tasks;
  15. namespace MediaBrowser.MediaEncoding.Encoder
  16. {
  17. public class EncodingJob : IDisposable
  18. {
  19. public bool HasExited { get; internal set; }
  20. public Stream LogFileStream { get; set; }
  21. public IProgress<double> Progress { get; set; }
  22. public TaskCompletionSource<bool> TaskCompletionSource;
  23. public EncodingJobOptions Options { get; set; }
  24. public string InputContainer { get; set; }
  25. public List<MediaStream> AllMediaStreams { get; set; }
  26. public MediaStream AudioStream { get; set; }
  27. public MediaStream VideoStream { get; set; }
  28. public MediaStream SubtitleStream { get; set; }
  29. public IIsoMount IsoMount { get; set; }
  30. public bool ReadInputAtNativeFramerate { get; set; }
  31. public bool IsVideoRequest { get; set; }
  32. public string InputAudioSync { get; set; }
  33. public string InputVideoSync { get; set; }
  34. public string Id { get; set; }
  35. public string MediaPath { get; set; }
  36. public MediaProtocol InputProtocol { get; set; }
  37. public bool IsInputVideo { get; set; }
  38. public VideoType VideoType { get; set; }
  39. public IsoType? IsoType { get; set; }
  40. public List<string> PlayableStreamFileNames { get; set; }
  41. public List<string> SupportedAudioCodecs { get; set; }
  42. public Dictionary<string, string> RemoteHttpHeaders { get; set; }
  43. public TransportStreamTimestamp InputTimestamp { get; set; }
  44. public bool DeInterlace { get; set; }
  45. public string MimeType { get; set; }
  46. public bool EstimateContentLength { get; set; }
  47. public bool EnableMpegtsM2TsMode { get; set; }
  48. public TranscodeSeekInfo TranscodeSeekInfo { get; set; }
  49. public long? EncodingDurationTicks { get; set; }
  50. public string LiveTvStreamId { get; set; }
  51. public long? RunTimeTicks;
  52. public string ItemType { get; set; }
  53. public long? InputBitrate { get; set; }
  54. public long? InputFileSize { get; set; }
  55. public string OutputAudioSync = "1";
  56. public string OutputVideoSync = "vfr";
  57. public string GetMimeType(string outputPath)
  58. {
  59. if (!string.IsNullOrEmpty(MimeType))
  60. {
  61. return MimeType;
  62. }
  63. return MimeTypes.GetMimeType(outputPath);
  64. }
  65. private readonly ILogger _logger;
  66. private readonly ILiveTvManager _liveTvManager;
  67. public EncodingJob(ILogger logger, ILiveTvManager liveTvManager)
  68. {
  69. _logger = logger;
  70. _liveTvManager = liveTvManager;
  71. Id = Guid.NewGuid().ToString("N");
  72. RemoteHttpHeaders = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
  73. _logger = logger;
  74. SupportedAudioCodecs = new List<string>();
  75. PlayableStreamFileNames = new List<string>();
  76. RemoteHttpHeaders = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
  77. AllMediaStreams = new List<MediaStream>();
  78. TaskCompletionSource = new TaskCompletionSource<bool>();
  79. }
  80. public void Dispose()
  81. {
  82. DisposeLiveStream();
  83. DisposeLogStream();
  84. DisposeIsoMount();
  85. }
  86. private void DisposeLogStream()
  87. {
  88. if (LogFileStream != null)
  89. {
  90. try
  91. {
  92. LogFileStream.Dispose();
  93. }
  94. catch (Exception ex)
  95. {
  96. _logger.ErrorException("Error disposing log stream", ex);
  97. }
  98. LogFileStream = null;
  99. }
  100. }
  101. private void DisposeIsoMount()
  102. {
  103. if (IsoMount != null)
  104. {
  105. try
  106. {
  107. IsoMount.Dispose();
  108. }
  109. catch (Exception ex)
  110. {
  111. _logger.ErrorException("Error disposing iso mount", ex);
  112. }
  113. IsoMount = null;
  114. }
  115. }
  116. private async void DisposeLiveStream()
  117. {
  118. if (!string.IsNullOrEmpty(LiveTvStreamId))
  119. {
  120. try
  121. {
  122. await _liveTvManager.CloseLiveStream(LiveTvStreamId, CancellationToken.None).ConfigureAwait(false);
  123. }
  124. catch (Exception ex)
  125. {
  126. _logger.ErrorException("Error closing live tv stream", ex);
  127. }
  128. }
  129. }
  130. public int InternalSubtitleStreamOffset { get; set; }
  131. public string OutputFilePath { get; set; }
  132. public string OutputVideoCodec { get; set; }
  133. public string OutputAudioCodec { get; set; }
  134. public int? OutputAudioChannels;
  135. public int? OutputAudioSampleRate;
  136. public int? OutputAudioBitrate;
  137. public int? OutputVideoBitrate;
  138. public string ActualOutputVideoCodec
  139. {
  140. get
  141. {
  142. var codec = OutputVideoCodec;
  143. if (string.Equals(codec, "copy", StringComparison.OrdinalIgnoreCase))
  144. {
  145. var stream = VideoStream;
  146. if (stream != null)
  147. {
  148. return stream.Codec;
  149. }
  150. return null;
  151. }
  152. return codec;
  153. }
  154. }
  155. public string ActualOutputAudioCodec
  156. {
  157. get
  158. {
  159. var codec = OutputAudioCodec;
  160. if (string.Equals(codec, "copy", StringComparison.OrdinalIgnoreCase))
  161. {
  162. var stream = AudioStream;
  163. if (stream != null)
  164. {
  165. return stream.Codec;
  166. }
  167. return null;
  168. }
  169. return codec;
  170. }
  171. }
  172. public int? TotalOutputBitrate
  173. {
  174. get
  175. {
  176. return (OutputAudioBitrate ?? 0) + (OutputVideoBitrate ?? 0);
  177. }
  178. }
  179. public int? OutputWidth
  180. {
  181. get
  182. {
  183. if (VideoStream != null && VideoStream.Width.HasValue && VideoStream.Height.HasValue)
  184. {
  185. var size = new ImageSize
  186. {
  187. Width = VideoStream.Width.Value,
  188. Height = VideoStream.Height.Value
  189. };
  190. var newSize = DrawingUtils.Resize(size,
  191. Options.Width,
  192. Options.Height,
  193. Options.MaxWidth,
  194. Options.MaxHeight);
  195. return Convert.ToInt32(newSize.Width);
  196. }
  197. if (!IsVideoRequest)
  198. {
  199. return null;
  200. }
  201. return Options.MaxWidth ?? Options.Width;
  202. }
  203. }
  204. public int? OutputHeight
  205. {
  206. get
  207. {
  208. if (VideoStream != null && VideoStream.Width.HasValue && VideoStream.Height.HasValue)
  209. {
  210. var size = new ImageSize
  211. {
  212. Width = VideoStream.Width.Value,
  213. Height = VideoStream.Height.Value
  214. };
  215. var newSize = DrawingUtils.Resize(size,
  216. Options.Width,
  217. Options.Height,
  218. Options.MaxWidth,
  219. Options.MaxHeight);
  220. return Convert.ToInt32(newSize.Height);
  221. }
  222. if (!IsVideoRequest)
  223. {
  224. return null;
  225. }
  226. return Options.MaxHeight ?? Options.Height;
  227. }
  228. }
  229. /// <summary>
  230. /// Predicts the audio sample rate that will be in the output stream
  231. /// </summary>
  232. public int? TargetVideoBitDepth
  233. {
  234. get
  235. {
  236. var stream = VideoStream;
  237. return stream == null || !Options.Static ? null : stream.BitDepth;
  238. }
  239. }
  240. /// <summary>
  241. /// Gets the target reference frames.
  242. /// </summary>
  243. /// <value>The target reference frames.</value>
  244. public int? TargetRefFrames
  245. {
  246. get
  247. {
  248. var stream = VideoStream;
  249. return stream == null || !Options.Static ? null : stream.RefFrames;
  250. }
  251. }
  252. /// <summary>
  253. /// Predicts the audio sample rate that will be in the output stream
  254. /// </summary>
  255. public float? TargetFramerate
  256. {
  257. get
  258. {
  259. var stream = VideoStream;
  260. var requestedFramerate = Options.MaxFramerate ?? Options.Framerate;
  261. return requestedFramerate.HasValue && !Options.Static
  262. ? requestedFramerate
  263. : stream == null ? null : stream.AverageFrameRate ?? stream.RealFrameRate;
  264. }
  265. }
  266. /// <summary>
  267. /// Predicts the audio sample rate that will be in the output stream
  268. /// </summary>
  269. public double? TargetVideoLevel
  270. {
  271. get
  272. {
  273. var stream = VideoStream;
  274. return Options.Level.HasValue && !Options.Static
  275. ? Options.Level.Value
  276. : stream == null ? null : stream.Level;
  277. }
  278. }
  279. public TransportStreamTimestamp TargetTimestamp
  280. {
  281. get
  282. {
  283. var defaultValue = string.Equals(Options.OutputContainer, "m2ts", StringComparison.OrdinalIgnoreCase) ?
  284. TransportStreamTimestamp.Valid :
  285. TransportStreamTimestamp.None;
  286. return !Options.Static
  287. ? defaultValue
  288. : InputTimestamp;
  289. }
  290. }
  291. /// <summary>
  292. /// Predicts the audio sample rate that will be in the output stream
  293. /// </summary>
  294. public int? TargetPacketLength
  295. {
  296. get
  297. {
  298. var stream = VideoStream;
  299. return !Options.Static
  300. ? null
  301. : stream == null ? null : stream.PacketLength;
  302. }
  303. }
  304. /// <summary>
  305. /// Predicts the audio sample rate that will be in the output stream
  306. /// </summary>
  307. public string TargetVideoProfile
  308. {
  309. get
  310. {
  311. var stream = VideoStream;
  312. return !string.IsNullOrEmpty(Options.Profile) && !Options.Static
  313. ? Options.Profile
  314. : stream == null ? null : stream.Profile;
  315. }
  316. }
  317. public bool? IsTargetAnamorphic
  318. {
  319. get
  320. {
  321. if (Options.Static)
  322. {
  323. return VideoStream == null ? null : VideoStream.IsAnamorphic;
  324. }
  325. return false;
  326. }
  327. }
  328. public bool? IsTargetCabac
  329. {
  330. get
  331. {
  332. if (Options.Static)
  333. {
  334. return VideoStream == null ? null : VideoStream.IsCabac;
  335. }
  336. return true;
  337. }
  338. }
  339. public void ReportTranscodingProgress(TimeSpan? transcodingPosition, float? framerate, double? percentComplete, long? bytesTranscoded)
  340. {
  341. var ticks = transcodingPosition.HasValue ? transcodingPosition.Value.Ticks : (long?)null;
  342. // job.Framerate = framerate;
  343. if (percentComplete.HasValue)
  344. {
  345. Progress.Report(percentComplete.Value);
  346. }
  347. // job.TranscodingPositionTicks = ticks;
  348. // job.BytesTranscoded = bytesTranscoded;
  349. var deviceId = Options.DeviceId;
  350. if (!string.IsNullOrWhiteSpace(deviceId))
  351. {
  352. var audioCodec = ActualOutputVideoCodec;
  353. var videoCodec = ActualOutputVideoCodec;
  354. // SessionManager.ReportTranscodingInfo(deviceId, new TranscodingInfo
  355. // {
  356. // Bitrate = job.TotalOutputBitrate,
  357. // AudioCodec = audioCodec,
  358. // VideoCodec = videoCodec,
  359. // Container = job.Options.OutputContainer,
  360. // Framerate = framerate,
  361. // CompletionPercentage = percentComplete,
  362. // Width = job.OutputWidth,
  363. // Height = job.OutputHeight,
  364. // AudioChannels = job.OutputAudioChannels,
  365. // IsAudioDirect = string.Equals(job.OutputAudioCodec, "copy", StringComparison.OrdinalIgnoreCase),
  366. // IsVideoDirect = string.Equals(job.OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase)
  367. // });
  368. }
  369. }
  370. }
  371. }