StreamState.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 StreamState(ILiveTvManager liveTvManager, ILogger logger)
  50. {
  51. _liveTvManager = liveTvManager;
  52. _logger = logger;
  53. }
  54. public string InputAudioSync { get; set; }
  55. public string InputVideoSync { get; set; }
  56. public bool DeInterlace { get; set; }
  57. public bool ReadInputAtNativeFramerate { get; set; }
  58. public string InputFormat { get; set; }
  59. public string InputVideoCodec { get; set; }
  60. public string InputAudioCodec { get; set; }
  61. public string MimeType { get; set; }
  62. public string OrgPn { get; set; }
  63. // DLNA Settings
  64. public bool EstimateContentLength { get; set; }
  65. public bool EnableMpegtsM2TsMode { get; set; }
  66. public TranscodeSeekInfo TranscodeSeekInfo { get; set; }
  67. public string GetMimeType(string outputPath)
  68. {
  69. if (!string.IsNullOrEmpty(MimeType))
  70. {
  71. return MimeType;
  72. }
  73. return MimeTypes.GetMimeType(outputPath);
  74. }
  75. public void Dispose()
  76. {
  77. DisposeLiveStream();
  78. DisposeLogStream();
  79. DisposeIsoMount();
  80. }
  81. private void DisposeLogStream()
  82. {
  83. if (LogFileStream != null)
  84. {
  85. try
  86. {
  87. LogFileStream.Dispose();
  88. }
  89. catch (Exception ex)
  90. {
  91. _logger.ErrorException("Error disposing log stream", ex);
  92. }
  93. LogFileStream = null;
  94. }
  95. }
  96. private void DisposeIsoMount()
  97. {
  98. if (IsoMount != null)
  99. {
  100. try
  101. {
  102. IsoMount.Dispose();
  103. }
  104. catch (Exception ex)
  105. {
  106. _logger.ErrorException("Error disposing iso mount", ex);
  107. }
  108. IsoMount = null;
  109. }
  110. }
  111. private async void DisposeLiveStream()
  112. {
  113. if (!string.IsNullOrEmpty(LiveTvStreamId))
  114. {
  115. try
  116. {
  117. await _liveTvManager.CloseLiveStream(LiveTvStreamId, CancellationToken.None).ConfigureAwait(false);
  118. }
  119. catch (Exception ex)
  120. {
  121. _logger.ErrorException("Error closing live tv stream", ex);
  122. }
  123. }
  124. }
  125. }
  126. }