EncodingJob.cs 15 KB

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