StreamState.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. /// <summary>
  140. /// Gets or sets the device id.
  141. /// </summary>
  142. public string? DeviceId { get; set; }
  143. /// <summary>
  144. /// Gets or sets the play session id.
  145. /// </summary>
  146. public string? PlaySessionId { get; set; }
  147. /// <summary>
  148. /// Gets or sets the live stream id.
  149. /// </summary>
  150. public string? LiveStreamId { get; set; }
  151. /// <summary>
  152. /// Gets or sets the video coded.
  153. /// </summary>
  154. public string? VideoCodec { get; set; }
  155. /// <summary>
  156. /// Gets or sets the audio codec.
  157. /// </summary>
  158. public string? AudioCodec { get; set; }
  159. /// <summary>
  160. /// Gets or sets the subtitle codec.
  161. /// </summary>
  162. public string? SubtitleCodec { get; set; }
  163. /// <inheritdoc />
  164. public void Dispose()
  165. {
  166. Dispose(true);
  167. GC.SuppressFinalize(this);
  168. }
  169. /// <inheritdoc />
  170. public override void ReportTranscodingProgress(TimeSpan? transcodingPosition, float? framerate, double? percentComplete, long? bytesTranscoded, int? bitRate)
  171. {
  172. _transcodingJobHelper.ReportTranscodingProgress(TranscodingJob!, this, transcodingPosition, framerate, percentComplete, bytesTranscoded, bitRate);
  173. }
  174. /// <summary>
  175. /// Disposes the stream state.
  176. /// </summary>
  177. /// <param name="disposing">Whether the object is currently beeing disposed.</param>
  178. protected virtual void Dispose(bool disposing)
  179. {
  180. if (_disposed)
  181. {
  182. return;
  183. }
  184. if (disposing)
  185. {
  186. // REVIEW: Is this the right place for this?
  187. if (MediaSource.RequiresClosing
  188. && string.IsNullOrWhiteSpace(LiveStreamId)
  189. && !string.IsNullOrWhiteSpace(MediaSource.LiveStreamId))
  190. {
  191. _mediaSourceManager.CloseLiveStream(MediaSource.LiveStreamId).GetAwaiter().GetResult();
  192. }
  193. TranscodingThrottler?.Dispose();
  194. }
  195. TranscodingThrottler = null;
  196. TranscodingJob = null;
  197. _disposed = true;
  198. }
  199. }
  200. }