StreamState.cs 6.7 KB

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