StreamState.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. using MediaBrowser.Controller.Library;
  2. using MediaBrowser.Model.Dlna;
  3. using MediaBrowser.Model.Drawing;
  4. using MediaBrowser.Model.Dto;
  5. using MediaBrowser.Model.Entities;
  6. using MediaBrowser.Model.IO;
  7. using MediaBrowser.Model.MediaInfo;
  8. using MediaBrowser.Model.Net;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Globalization;
  12. using System.IO;
  13. using System.Linq;
  14. using System.Threading;
  15. using MediaBrowser.Controller.MediaEncoding;
  16. using Microsoft.Extensions.Logging;
  17. namespace MediaBrowser.Api.Playback
  18. {
  19. public class StreamState : EncodingJobInfo, IDisposable
  20. {
  21. private readonly ILogger _logger;
  22. private readonly IMediaSourceManager _mediaSourceManager;
  23. public string RequestedUrl { get; set; }
  24. public StreamRequest Request
  25. {
  26. get { return (StreamRequest)BaseRequest; }
  27. set
  28. {
  29. BaseRequest = value;
  30. IsVideoRequest = VideoRequest != null;
  31. }
  32. }
  33. public TranscodingThrottler TranscodingThrottler { get; set; }
  34. public VideoStreamRequest VideoRequest
  35. {
  36. get { return Request as VideoStreamRequest; }
  37. }
  38. /// <summary>
  39. /// Gets or sets the log file stream.
  40. /// </summary>
  41. /// <value>The log file stream.</value>
  42. public Stream LogFileStream { get; set; }
  43. public IDirectStreamProvider DirectStreamProvider { get; set; }
  44. public string WaitForPath { get; set; }
  45. public bool IsOutputVideo
  46. {
  47. get { return Request is VideoStreamRequest; }
  48. }
  49. public int SegmentLength
  50. {
  51. get
  52. {
  53. if (Request.SegmentLength.HasValue)
  54. {
  55. return Request.SegmentLength.Value;
  56. }
  57. if (string.Equals(OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase))
  58. {
  59. var userAgent = UserAgent ?? string.Empty;
  60. if (userAgent.IndexOf("AppleTV", StringComparison.OrdinalIgnoreCase) != -1 ||
  61. userAgent.IndexOf("cfnetwork", StringComparison.OrdinalIgnoreCase) != -1 ||
  62. userAgent.IndexOf("ipad", StringComparison.OrdinalIgnoreCase) != -1 ||
  63. userAgent.IndexOf("iphone", StringComparison.OrdinalIgnoreCase) != -1 ||
  64. userAgent.IndexOf("ipod", StringComparison.OrdinalIgnoreCase) != -1)
  65. {
  66. if (IsSegmentedLiveStream)
  67. {
  68. return 6;
  69. }
  70. return 6;
  71. }
  72. if (IsSegmentedLiveStream)
  73. {
  74. return 3;
  75. }
  76. return 6;
  77. }
  78. return 3;
  79. }
  80. }
  81. public int MinSegments
  82. {
  83. get
  84. {
  85. if (Request.MinSegments.HasValue)
  86. {
  87. return Request.MinSegments.Value;
  88. }
  89. return SegmentLength >= 10 ? 2 : 3;
  90. }
  91. }
  92. public int HlsListSize
  93. {
  94. get
  95. {
  96. return 0;
  97. }
  98. }
  99. public string UserAgent { get; set; }
  100. public StreamState(IMediaSourceManager mediaSourceManager, ILogger logger, TranscodingJobType transcodingType)
  101. : base(logger, mediaSourceManager, transcodingType)
  102. {
  103. _mediaSourceManager = mediaSourceManager;
  104. _logger = logger;
  105. }
  106. public string MimeType { get; set; }
  107. public bool EstimateContentLength { get; set; }
  108. public TranscodeSeekInfo TranscodeSeekInfo { get; set; }
  109. public long? EncodingDurationTicks { get; set; }
  110. public string GetMimeType(string outputPath, bool enableStreamDefault = true)
  111. {
  112. if (!string.IsNullOrEmpty(MimeType))
  113. {
  114. return MimeType;
  115. }
  116. return MimeTypes.GetMimeType(outputPath, enableStreamDefault);
  117. }
  118. public bool EnableDlnaHeaders { get; set; }
  119. public void Dispose()
  120. {
  121. DisposeTranscodingThrottler();
  122. DisposeLiveStream();
  123. DisposeLogStream();
  124. TranscodingJob = null;
  125. }
  126. private void DisposeLogStream()
  127. {
  128. if (LogFileStream != null)
  129. {
  130. try
  131. {
  132. LogFileStream.Dispose();
  133. }
  134. catch (Exception ex)
  135. {
  136. _logger.LogError(ex, "Error disposing log stream");
  137. }
  138. LogFileStream = null;
  139. }
  140. }
  141. private void DisposeTranscodingThrottler()
  142. {
  143. if (TranscodingThrottler != null)
  144. {
  145. try
  146. {
  147. TranscodingThrottler.Dispose();
  148. }
  149. catch (Exception ex)
  150. {
  151. _logger.LogError(ex, "Error disposing TranscodingThrottler");
  152. }
  153. TranscodingThrottler = null;
  154. }
  155. }
  156. private async void DisposeLiveStream()
  157. {
  158. if (MediaSource.RequiresClosing && string.IsNullOrWhiteSpace(Request.LiveStreamId) && !string.IsNullOrWhiteSpace(MediaSource.LiveStreamId))
  159. {
  160. try
  161. {
  162. await _mediaSourceManager.CloseLiveStream(MediaSource.LiveStreamId).ConfigureAwait(false);
  163. }
  164. catch (Exception ex)
  165. {
  166. _logger.LogError(ex, "Error closing media source");
  167. }
  168. }
  169. }
  170. public string OutputFilePath { get; set; }
  171. public string ActualOutputVideoCodec
  172. {
  173. get
  174. {
  175. var codec = OutputVideoCodec;
  176. if (string.Equals(codec, "copy", StringComparison.OrdinalIgnoreCase))
  177. {
  178. var stream = VideoStream;
  179. if (stream != null)
  180. {
  181. return stream.Codec;
  182. }
  183. return null;
  184. }
  185. return codec;
  186. }
  187. }
  188. public string ActualOutputAudioCodec
  189. {
  190. get
  191. {
  192. var codec = OutputAudioCodec;
  193. if (string.Equals(codec, "copy", StringComparison.OrdinalIgnoreCase))
  194. {
  195. var stream = AudioStream;
  196. if (stream != null)
  197. {
  198. return stream.Codec;
  199. }
  200. return null;
  201. }
  202. return codec;
  203. }
  204. }
  205. public DeviceProfile DeviceProfile { get; set; }
  206. public TranscodingJob TranscodingJob;
  207. public override void ReportTranscodingProgress(TimeSpan? transcodingPosition, float framerate, double? percentComplete, long bytesTranscoded, int? bitRate)
  208. {
  209. ApiEntryPoint.Instance.ReportTranscodingProgress(TranscodingJob, this, transcodingPosition, 0, percentComplete, 0, bitRate);
  210. }
  211. }
  212. }