StreamState.cs 6.3 KB

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