StreamState.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. using MediaBrowser.Common.Net;
  2. using MediaBrowser.Controller.LiveTv;
  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 System;
  9. using System.Collections.Generic;
  10. using System.IO;
  11. using System.Threading;
  12. namespace MediaBrowser.Api.Playback
  13. {
  14. public class StreamState : IDisposable
  15. {
  16. private readonly ILogger _logger;
  17. private readonly ILiveTvManager _liveTvManager;
  18. public string RequestedUrl { get; set; }
  19. public StreamRequest Request { get; set; }
  20. public VideoStreamRequest VideoRequest
  21. {
  22. get { return Request as VideoStreamRequest; }
  23. }
  24. /// <summary>
  25. /// Gets or sets the log file stream.
  26. /// </summary>
  27. /// <value>The log file stream.</value>
  28. public Stream LogFileStream { get; set; }
  29. public MediaStream AudioStream { get; set; }
  30. public MediaStream VideoStream { get; set; }
  31. public MediaStream SubtitleStream { get; set; }
  32. /// <summary>
  33. /// Gets or sets the iso mount.
  34. /// </summary>
  35. /// <value>The iso mount.</value>
  36. public IIsoMount IsoMount { get; set; }
  37. public string MediaPath { get; set; }
  38. public bool IsRemote { get; set; }
  39. public bool IsInputVideo { get; set; }
  40. public VideoType VideoType { get; set; }
  41. public IsoType? IsoType { get; set; }
  42. public List<string> PlayableStreamFileNames { get; set; }
  43. public bool HasMediaStreams { get; set; }
  44. public string LiveTvStreamId { get; set; }
  45. public int SegmentLength = 10;
  46. public int HlsListSize;
  47. public long? RunTimeTicks;
  48. public string OutputAudioSync = "1";
  49. public string OutputVideoSync = "vfr";
  50. public List<string> SupportedAudioCodecs { get; set; }
  51. public StreamState(ILiveTvManager liveTvManager, ILogger logger)
  52. {
  53. _liveTvManager = liveTvManager;
  54. _logger = logger;
  55. SupportedAudioCodecs = new List<string>();
  56. }
  57. public string InputAudioSync { get; set; }
  58. public string InputVideoSync { get; set; }
  59. public bool DeInterlace { get; set; }
  60. public bool ReadInputAtNativeFramerate { get; set; }
  61. public string InputFormat { get; set; }
  62. public string InputVideoCodec { get; set; }
  63. public string InputAudioCodec { get; set; }
  64. public string MimeType { get; set; }
  65. public string OrgPn { get; set; }
  66. public bool EstimateContentLength { get; set; }
  67. public bool EnableMpegtsM2TsMode { get; set; }
  68. public TranscodeSeekInfo TranscodeSeekInfo { get; set; }
  69. public string GetMimeType(string outputPath)
  70. {
  71. if (!string.IsNullOrEmpty(MimeType))
  72. {
  73. return MimeType;
  74. }
  75. return MimeTypes.GetMimeType(outputPath);
  76. }
  77. public void Dispose()
  78. {
  79. DisposeLiveStream();
  80. DisposeLogStream();
  81. DisposeIsoMount();
  82. }
  83. private void DisposeLogStream()
  84. {
  85. if (LogFileStream != null)
  86. {
  87. try
  88. {
  89. LogFileStream.Dispose();
  90. }
  91. catch (Exception ex)
  92. {
  93. _logger.ErrorException("Error disposing log stream", ex);
  94. }
  95. LogFileStream = null;
  96. }
  97. }
  98. private void DisposeIsoMount()
  99. {
  100. if (IsoMount != null)
  101. {
  102. try
  103. {
  104. IsoMount.Dispose();
  105. }
  106. catch (Exception ex)
  107. {
  108. _logger.ErrorException("Error disposing iso mount", ex);
  109. }
  110. IsoMount = null;
  111. }
  112. }
  113. private async void DisposeLiveStream()
  114. {
  115. if (!string.IsNullOrEmpty(LiveTvStreamId))
  116. {
  117. try
  118. {
  119. await _liveTvManager.CloseLiveStream(LiveTvStreamId, CancellationToken.None).ConfigureAwait(false);
  120. }
  121. catch (Exception ex)
  122. {
  123. _logger.ErrorException("Error closing live tv stream", ex);
  124. }
  125. }
  126. }
  127. public int? OutputAudioChannels;
  128. public int? OutputAudioSampleRate;
  129. public int? OutputAudioBitrate;
  130. public int? OutputVideoBitrate;
  131. public string OutputContainer { get; set; }
  132. public int? TotalOutputBitrate
  133. {
  134. get
  135. {
  136. return (OutputAudioBitrate ?? 0) + (OutputVideoBitrate ?? 0);
  137. }
  138. }
  139. public int? OutputWidth
  140. {
  141. get
  142. {
  143. if (VideoStream != null && VideoStream.Width.HasValue && VideoStream.Height.HasValue)
  144. {
  145. var size = new ImageSize
  146. {
  147. Width = VideoStream.Width.Value,
  148. Height = VideoStream.Height.Value
  149. };
  150. var newSize = DrawingUtils.Resize(size,
  151. VideoRequest.Width,
  152. VideoRequest.Height,
  153. VideoRequest.MaxWidth,
  154. VideoRequest.MaxHeight);
  155. return Convert.ToInt32(newSize.Width);
  156. }
  157. return VideoRequest.MaxWidth ?? VideoRequest.Width;
  158. }
  159. }
  160. public int? OutputHeight
  161. {
  162. get
  163. {
  164. if (VideoStream != null && VideoStream.Width.HasValue && VideoStream.Height.HasValue)
  165. {
  166. var size = new ImageSize
  167. {
  168. Width = VideoStream.Width.Value,
  169. Height = VideoStream.Height.Value
  170. };
  171. var newSize = DrawingUtils.Resize(size,
  172. VideoRequest.Width,
  173. VideoRequest.Height,
  174. VideoRequest.MaxWidth,
  175. VideoRequest.MaxHeight);
  176. return Convert.ToInt32(newSize.Height);
  177. }
  178. return VideoRequest.MaxHeight ?? VideoRequest.Height;
  179. }
  180. }
  181. }
  182. }