StreamState.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. if (IsSegmentedLiveStream)
  86. {
  87. return 6;
  88. }
  89. return 6;
  90. }
  91. if (IsSegmentedLiveStream)
  92. {
  93. return 3;
  94. }
  95. return 6;
  96. }
  97. return 3;
  98. }
  99. }
  100. /// <summary>
  101. /// Gets the minimum number of segments.
  102. /// </summary>
  103. public int MinSegments
  104. {
  105. get
  106. {
  107. if (Request.MinSegments.HasValue)
  108. {
  109. return Request.MinSegments.Value;
  110. }
  111. return SegmentLength >= 10 ? 2 : 3;
  112. }
  113. }
  114. /// <summary>
  115. /// Gets or sets the user agent.
  116. /// </summary>
  117. public string? UserAgent { get; set; }
  118. /// <summary>
  119. /// Gets or sets a value indicating whether to estimate the content length.
  120. /// </summary>
  121. public bool EstimateContentLength { get; set; }
  122. /// <summary>
  123. /// Gets or sets the transcode seek info.
  124. /// </summary>
  125. public TranscodeSeekInfo TranscodeSeekInfo { get; set; }
  126. /// <summary>
  127. /// Gets or sets a value indicating whether to enable dlna headers.
  128. /// </summary>
  129. public bool EnableDlnaHeaders { get; set; }
  130. /// <summary>
  131. /// Gets or sets the device profile.
  132. /// </summary>
  133. public DeviceProfile? DeviceProfile { get; set; }
  134. /// <summary>
  135. /// Gets or sets the transcoding job.
  136. /// </summary>
  137. public TranscodingJobDto? TranscodingJob { get; set; }
  138. /// <inheritdoc />
  139. public void Dispose()
  140. {
  141. Dispose(true);
  142. GC.SuppressFinalize(this);
  143. }
  144. /// <inheritdoc />
  145. public override void ReportTranscodingProgress(TimeSpan? transcodingPosition, float? framerate, double? percentComplete, long? bytesTranscoded, int? bitRate)
  146. {
  147. _transcodingJobHelper.ReportTranscodingProgress(TranscodingJob!, this, transcodingPosition, framerate, percentComplete, bytesTranscoded, bitRate);
  148. }
  149. /// <summary>
  150. /// Disposes the stream state.
  151. /// </summary>
  152. /// <param name="disposing">Whether the object is currently beeing disposed.</param>
  153. protected virtual void Dispose(bool disposing)
  154. {
  155. if (_disposed)
  156. {
  157. return;
  158. }
  159. if (disposing)
  160. {
  161. // REVIEW: Is this the right place for this?
  162. if (MediaSource.RequiresClosing
  163. && string.IsNullOrWhiteSpace(Request.LiveStreamId)
  164. && !string.IsNullOrWhiteSpace(MediaSource.LiveStreamId))
  165. {
  166. _mediaSourceManager.CloseLiveStream(MediaSource.LiveStreamId).GetAwaiter().GetResult();
  167. }
  168. TranscodingThrottler?.Dispose();
  169. }
  170. TranscodingThrottler = null;
  171. TranscodingJob = null;
  172. _disposed = true;
  173. }
  174. }
  175. }