StreamState.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. using MediaBrowser.Common.Net;
  2. using MediaBrowser.Controller.LiveTv;
  3. using MediaBrowser.Model.Dlna;
  4. using MediaBrowser.Model.Entities;
  5. using MediaBrowser.Model.IO;
  6. using MediaBrowser.Model.Logging;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.IO;
  10. using System.Threading;
  11. namespace MediaBrowser.Api.Playback
  12. {
  13. public class StreamState : IDisposable
  14. {
  15. private readonly ILogger _logger;
  16. private readonly ILiveTvManager _liveTvManager;
  17. public string RequestedUrl { get; set; }
  18. public StreamRequest Request { get; set; }
  19. public VideoStreamRequest VideoRequest
  20. {
  21. get { return Request as VideoStreamRequest; }
  22. }
  23. /// <summary>
  24. /// Gets or sets the log file stream.
  25. /// </summary>
  26. /// <value>The log file stream.</value>
  27. public Stream LogFileStream { get; set; }
  28. public MediaStream AudioStream { get; set; }
  29. public MediaStream VideoStream { get; set; }
  30. public MediaStream SubtitleStream { get; set; }
  31. /// <summary>
  32. /// Gets or sets the iso mount.
  33. /// </summary>
  34. /// <value>The iso mount.</value>
  35. public IIsoMount IsoMount { get; set; }
  36. public string MediaPath { get; set; }
  37. public bool IsRemote { get; set; }
  38. public bool IsInputVideo { get; set; }
  39. public VideoType VideoType { get; set; }
  40. public IsoType? IsoType { get; set; }
  41. public List<string> PlayableStreamFileNames { get; set; }
  42. public bool HasMediaStreams { get; set; }
  43. public string LiveTvStreamId { get; set; }
  44. public int SegmentLength = 10;
  45. public int HlsListSize;
  46. public long? RunTimeTicks;
  47. public string AudioSync = "1";
  48. public string VideoSync = "vfr";
  49. public List<string> SupportedAudioCodecs { get; set; }
  50. public StreamState(ILiveTvManager liveTvManager, ILogger logger)
  51. {
  52. _liveTvManager = liveTvManager;
  53. _logger = logger;
  54. SupportedAudioCodecs = new List<string>();
  55. }
  56. public string InputAudioSync { get; set; }
  57. public string InputVideoSync { get; set; }
  58. public bool DeInterlace { get; set; }
  59. public bool ReadInputAtNativeFramerate { get; set; }
  60. public string InputFormat { get; set; }
  61. public string InputVideoCodec { get; set; }
  62. public string InputAudioCodec { get; set; }
  63. public string MimeType { get; set; }
  64. public string OrgPn { get; set; }
  65. // DLNA Settings
  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. }
  128. }