StreamState.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. public IDirectStreamProvider? DirectStreamProvider { get; set; }
  57. /// <summary>
  58. /// Gets or sets the path to wait for.
  59. /// </summary>
  60. public string? WaitForPath { get; set; }
  61. /// <summary>
  62. /// Gets a value indicating whether the request outputs video.
  63. /// </summary>
  64. public bool IsOutputVideo => Request is VideoRequestDto;
  65. /// <summary>
  66. /// Gets the segment length.
  67. /// </summary>
  68. public int SegmentLength
  69. {
  70. get
  71. {
  72. if (Request.SegmentLength.HasValue)
  73. {
  74. return Request.SegmentLength.Value;
  75. }
  76. if (EncodingHelper.IsCopyCodec(OutputVideoCodec))
  77. {
  78. var userAgent = UserAgent ?? string.Empty;
  79. if (userAgent.IndexOf("AppleTV", StringComparison.OrdinalIgnoreCase) != -1
  80. || userAgent.IndexOf("cfnetwork", StringComparison.OrdinalIgnoreCase) != -1
  81. || userAgent.IndexOf("ipad", StringComparison.OrdinalIgnoreCase) != -1
  82. || userAgent.IndexOf("iphone", StringComparison.OrdinalIgnoreCase) != -1
  83. || userAgent.IndexOf("ipod", StringComparison.OrdinalIgnoreCase) != -1)
  84. {
  85. return 6;
  86. }
  87. if (IsSegmentedLiveStream)
  88. {
  89. return 3;
  90. }
  91. return 6;
  92. }
  93. return 3;
  94. }
  95. }
  96. /// <summary>
  97. /// Gets the minimum number of segments.
  98. /// </summary>
  99. public int MinSegments
  100. {
  101. get
  102. {
  103. if (Request.MinSegments.HasValue)
  104. {
  105. return Request.MinSegments.Value;
  106. }
  107. return SegmentLength >= 10 ? 2 : 3;
  108. }
  109. }
  110. /// <summary>
  111. /// Gets or sets the user agent.
  112. /// </summary>
  113. public string? UserAgent { get; set; }
  114. /// <summary>
  115. /// Gets or sets a value indicating whether to estimate the content length.
  116. /// </summary>
  117. public bool EstimateContentLength { get; set; }
  118. /// <summary>
  119. /// Gets or sets the transcode seek info.
  120. /// </summary>
  121. public TranscodeSeekInfo TranscodeSeekInfo { get; set; }
  122. /// <summary>
  123. /// Gets or sets a value indicating whether to enable dlna headers.
  124. /// </summary>
  125. public bool EnableDlnaHeaders { get; set; }
  126. /// <summary>
  127. /// Gets or sets the device profile.
  128. /// </summary>
  129. public DeviceProfile? DeviceProfile { get; set; }
  130. /// <summary>
  131. /// Gets or sets the transcoding job.
  132. /// </summary>
  133. public TranscodingJobDto? TranscodingJob { get; set; }
  134. /// <inheritdoc />
  135. public void Dispose()
  136. {
  137. Dispose(true);
  138. GC.SuppressFinalize(this);
  139. }
  140. /// <inheritdoc />
  141. public override void ReportTranscodingProgress(TimeSpan? transcodingPosition, float? framerate, double? percentComplete, long? bytesTranscoded, int? bitRate)
  142. {
  143. _transcodingJobHelper.ReportTranscodingProgress(TranscodingJob!, this, transcodingPosition, framerate, percentComplete, bytesTranscoded, bitRate);
  144. }
  145. /// <summary>
  146. /// Disposes the stream state.
  147. /// </summary>
  148. /// <param name="disposing">Whether the object is currently beeing disposed.</param>
  149. protected virtual void Dispose(bool disposing)
  150. {
  151. if (_disposed)
  152. {
  153. return;
  154. }
  155. if (disposing)
  156. {
  157. // REVIEW: Is this the right place for this?
  158. if (MediaSource.RequiresClosing
  159. && string.IsNullOrWhiteSpace(Request.LiveStreamId)
  160. && !string.IsNullOrWhiteSpace(MediaSource.LiveStreamId))
  161. {
  162. _mediaSourceManager.CloseLiveStream(MediaSource.LiveStreamId).GetAwaiter().GetResult();
  163. }
  164. TranscodingThrottler?.Dispose();
  165. }
  166. TranscodingThrottler = null;
  167. TranscodingJob = null;
  168. _disposed = true;
  169. }
  170. }
  171. }