EncodingJob.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. using MediaBrowser.Controller.Library;
  2. using MediaBrowser.Controller.MediaEncoding;
  3. using MediaBrowser.Model.Dlna;
  4. using MediaBrowser.Model.Drawing;
  5. using MediaBrowser.Model.Dto;
  6. using MediaBrowser.Model.Entities;
  7. using MediaBrowser.Model.IO;
  8. using MediaBrowser.Model.Logging;
  9. using MediaBrowser.Model.MediaInfo;
  10. using MediaBrowser.Model.Net;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.IO;
  14. using System.Linq;
  15. using System.Threading;
  16. using System.Threading.Tasks;
  17. namespace MediaBrowser.MediaEncoding.Encoder
  18. {
  19. public class EncodingJob : EncodingJobInfo, 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 EncodingJobOptions Options
  27. {
  28. get { return (EncodingJobOptions) BaseRequest; }
  29. set { BaseRequest = value; }
  30. }
  31. public string Id { get; set; }
  32. public string MimeType { get; set; }
  33. public bool EstimateContentLength { get; set; }
  34. public TranscodeSeekInfo TranscodeSeekInfo { get; set; }
  35. public long? EncodingDurationTicks { get; set; }
  36. public string ItemType { get; set; }
  37. public string GetMimeType(string outputPath)
  38. {
  39. if (!string.IsNullOrEmpty(MimeType))
  40. {
  41. return MimeType;
  42. }
  43. return MimeTypes.GetMimeType(outputPath);
  44. }
  45. private readonly ILogger _logger;
  46. private readonly IMediaSourceManager _mediaSourceManager;
  47. public EncodingJob(ILogger logger, IMediaSourceManager mediaSourceManager) :
  48. base(logger, TranscodingJobType.Progressive)
  49. {
  50. _logger = logger;
  51. _mediaSourceManager = mediaSourceManager;
  52. Id = Guid.NewGuid().ToString("N");
  53. _logger = logger;
  54. TaskCompletionSource = new TaskCompletionSource<bool>();
  55. }
  56. public void Dispose()
  57. {
  58. DisposeLiveStream();
  59. DisposeLogStream();
  60. DisposeIsoMount();
  61. }
  62. private void DisposeLogStream()
  63. {
  64. if (LogFileStream != null)
  65. {
  66. try
  67. {
  68. LogFileStream.Dispose();
  69. }
  70. catch (Exception ex)
  71. {
  72. _logger.ErrorException("Error disposing log stream", ex);
  73. }
  74. LogFileStream = null;
  75. }
  76. }
  77. private async void DisposeLiveStream()
  78. {
  79. if (MediaSource.RequiresClosing && string.IsNullOrWhiteSpace(Options.LiveStreamId) && !string.IsNullOrWhiteSpace(MediaSource.LiveStreamId))
  80. {
  81. try
  82. {
  83. await _mediaSourceManager.CloseLiveStream(MediaSource.LiveStreamId).ConfigureAwait(false);
  84. }
  85. catch (Exception ex)
  86. {
  87. _logger.ErrorException("Error closing media source", ex);
  88. }
  89. }
  90. }
  91. public string OutputFilePath { get; set; }
  92. public string ActualOutputVideoCodec
  93. {
  94. get
  95. {
  96. var codec = OutputVideoCodec;
  97. if (string.Equals(codec, "copy", StringComparison.OrdinalIgnoreCase))
  98. {
  99. var stream = VideoStream;
  100. if (stream != null)
  101. {
  102. return stream.Codec;
  103. }
  104. return null;
  105. }
  106. return codec;
  107. }
  108. }
  109. public string ActualOutputAudioCodec
  110. {
  111. get
  112. {
  113. var codec = OutputAudioCodec;
  114. if (string.Equals(codec, "copy", StringComparison.OrdinalIgnoreCase))
  115. {
  116. var stream = AudioStream;
  117. if (stream != null)
  118. {
  119. return stream.Codec;
  120. }
  121. return null;
  122. }
  123. return codec;
  124. }
  125. }
  126. public override void ReportTranscodingProgress(TimeSpan? transcodingPosition, float? framerate, double? percentComplete, long? bytesTranscoded, int? bitRate)
  127. {
  128. var ticks = transcodingPosition.HasValue ? transcodingPosition.Value.Ticks : (long?)null;
  129. // job.Framerate = framerate;
  130. if (!percentComplete.HasValue && ticks.HasValue && RunTimeTicks.HasValue)
  131. {
  132. var pct = ticks.Value / RunTimeTicks.Value;
  133. percentComplete = pct * 100;
  134. }
  135. if (percentComplete.HasValue)
  136. {
  137. Progress.Report(percentComplete.Value);
  138. }
  139. // job.TranscodingPositionTicks = ticks;
  140. // job.BytesTranscoded = bytesTranscoded;
  141. var deviceId = Options.DeviceId;
  142. if (!string.IsNullOrWhiteSpace(deviceId))
  143. {
  144. var audioCodec = ActualOutputVideoCodec;
  145. var videoCodec = ActualOutputVideoCodec;
  146. // SessionManager.ReportTranscodingInfo(deviceId, new TranscodingInfo
  147. // {
  148. // Bitrate = job.TotalOutputBitrate,
  149. // AudioCodec = audioCodec,
  150. // VideoCodec = videoCodec,
  151. // Container = job.Options.OutputContainer,
  152. // Framerate = framerate,
  153. // CompletionPercentage = percentComplete,
  154. // Width = job.OutputWidth,
  155. // Height = job.OutputHeight,
  156. // AudioChannels = job.OutputAudioChannels,
  157. // IsAudioDirect = string.Equals(job.OutputAudioCodec, "copy", StringComparison.OrdinalIgnoreCase),
  158. // IsVideoDirect = string.Equals(job.OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase)
  159. // });
  160. }
  161. }
  162. }
  163. }