StreamState.cs 5.7 KB

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