StreamState.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. using System;
  2. using MediaBrowser.Controller.Library;
  3. using MediaBrowser.Controller.MediaEncoding;
  4. using MediaBrowser.Model.Dlna;
  5. namespace MediaBrowser.Controller.Streaming;
  6. /// <summary>
  7. /// The stream state dto.
  8. /// </summary>
  9. public class StreamState : EncodingJobInfo, IDisposable
  10. {
  11. private readonly IMediaSourceManager _mediaSourceManager;
  12. private readonly ITranscodeManager _transcodeManager;
  13. private bool _disposed;
  14. /// <summary>
  15. /// Initializes a new instance of the <see cref="StreamState" /> class.
  16. /// </summary>
  17. /// <param name="mediaSourceManager">Instance of the <see cref="IMediaSourceManager" /> interface.</param>
  18. /// <param name="transcodingType">The <see cref="TranscodingJobType" />.</param>
  19. /// <param name="transcodeManager">The <see cref="ITranscodeManager" /> singleton.</param>
  20. public StreamState(IMediaSourceManager mediaSourceManager, TranscodingJobType transcodingType, ITranscodeManager transcodeManager)
  21. : base(transcodingType)
  22. {
  23. _mediaSourceManager = mediaSourceManager;
  24. _transcodeManager = transcodeManager;
  25. }
  26. /// <summary>
  27. /// Gets or sets the requested url.
  28. /// </summary>
  29. public string? RequestedUrl { get; set; }
  30. /// <summary>
  31. /// Gets or sets the request.
  32. /// </summary>
  33. public StreamingRequestDto Request
  34. {
  35. get => (StreamingRequestDto)BaseRequest;
  36. set
  37. {
  38. BaseRequest = value;
  39. IsVideoRequest = VideoRequest is not null;
  40. }
  41. }
  42. /// <summary>
  43. /// Gets the video request.
  44. /// </summary>
  45. public VideoRequestDto? VideoRequest => Request as VideoRequestDto;
  46. /// <summary>
  47. /// Gets or sets the direct stream provicer.
  48. /// </summary>
  49. /// <remarks>
  50. /// Deprecated.
  51. /// </remarks>
  52. public IDirectStreamProvider? DirectStreamProvider { get; set; }
  53. /// <summary>
  54. /// Gets or sets the path to wait for.
  55. /// </summary>
  56. public string? WaitForPath { get; set; }
  57. /// <summary>
  58. /// Gets a value indicating whether the request outputs video.
  59. /// </summary>
  60. public bool IsOutputVideo => Request is VideoRequestDto;
  61. /// <summary>
  62. /// Gets the segment length.
  63. /// </summary>
  64. public int SegmentLength
  65. {
  66. get
  67. {
  68. if (Request.SegmentLength.HasValue)
  69. {
  70. return Request.SegmentLength.Value;
  71. }
  72. if (EncodingHelper.IsCopyCodec(OutputVideoCodec))
  73. {
  74. var userAgent = UserAgent ?? string.Empty;
  75. if (userAgent.Contains("AppleTV", StringComparison.OrdinalIgnoreCase)
  76. || userAgent.Contains("cfnetwork", StringComparison.OrdinalIgnoreCase)
  77. || userAgent.Contains("ipad", StringComparison.OrdinalIgnoreCase)
  78. || userAgent.Contains("iphone", StringComparison.OrdinalIgnoreCase)
  79. || userAgent.Contains("ipod", StringComparison.OrdinalIgnoreCase))
  80. {
  81. return 6;
  82. }
  83. if (IsSegmentedLiveStream)
  84. {
  85. return 3;
  86. }
  87. return 6;
  88. }
  89. return 3;
  90. }
  91. }
  92. /// <summary>
  93. /// Gets the minimum number of segments.
  94. /// </summary>
  95. public int MinSegments
  96. {
  97. get
  98. {
  99. if (Request.MinSegments.HasValue)
  100. {
  101. return Request.MinSegments.Value;
  102. }
  103. return SegmentLength >= 10 ? 2 : 3;
  104. }
  105. }
  106. /// <summary>
  107. /// Gets or sets the user agent.
  108. /// </summary>
  109. public string? UserAgent { get; set; }
  110. /// <summary>
  111. /// Gets or sets a value indicating whether to estimate the content length.
  112. /// </summary>
  113. public bool EstimateContentLength { get; set; }
  114. /// <summary>
  115. /// Gets or sets the transcode seek info.
  116. /// </summary>
  117. public TranscodeSeekInfo TranscodeSeekInfo { get; set; }
  118. /// <summary>
  119. /// Gets or sets the transcoding job.
  120. /// </summary>
  121. public TranscodingJob? TranscodingJob { get; set; }
  122. /// <inheritdoc />
  123. public void Dispose()
  124. {
  125. Dispose(true);
  126. GC.SuppressFinalize(this);
  127. }
  128. /// <inheritdoc />
  129. public override void ReportTranscodingProgress(TimeSpan? transcodingPosition, float? framerate, double? percentComplete, long? bytesTranscoded, int? bitRate)
  130. {
  131. _transcodeManager.ReportTranscodingProgress(TranscodingJob!, this, transcodingPosition, framerate, percentComplete, bytesTranscoded, bitRate);
  132. }
  133. /// <summary>
  134. /// Disposes the stream state.
  135. /// </summary>
  136. /// <param name="disposing">Whether the object is currently being disposed.</param>
  137. protected virtual void Dispose(bool disposing)
  138. {
  139. if (_disposed)
  140. {
  141. return;
  142. }
  143. if (disposing)
  144. {
  145. // REVIEW: Is this the right place for this?
  146. if (MediaSource.RequiresClosing
  147. && string.IsNullOrWhiteSpace(Request.LiveStreamId)
  148. && !string.IsNullOrWhiteSpace(MediaSource.LiveStreamId))
  149. {
  150. _mediaSourceManager.CloseLiveStream(MediaSource.LiveStreamId).GetAwaiter().GetResult();
  151. }
  152. }
  153. TranscodingJob = null;
  154. _disposed = true;
  155. }
  156. }