StreamState.cs 6.6 KB

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