StreamState.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using System;
  2. using MediaBrowser.Controller.Library;
  3. using MediaBrowser.Controller.MediaEncoding;
  4. using MediaBrowser.Model.Dlna;
  5. namespace MediaBrowser.Api.Playback
  6. {
  7. public class StreamState : EncodingJobInfo, IDisposable
  8. {
  9. private readonly IMediaSourceManager _mediaSourceManager;
  10. private bool _disposed = false;
  11. public string RequestedUrl { get; set; }
  12. public StreamRequest Request
  13. {
  14. get => (StreamRequest)BaseRequest;
  15. set
  16. {
  17. BaseRequest = value;
  18. IsVideoRequest = VideoRequest != null;
  19. }
  20. }
  21. public TranscodingThrottler TranscodingThrottler { get; set; }
  22. public VideoStreamRequest VideoRequest => Request as VideoStreamRequest;
  23. public IDirectStreamProvider DirectStreamProvider { get; set; }
  24. public string WaitForPath { get; set; }
  25. public bool IsOutputVideo => Request is VideoStreamRequest;
  26. public int SegmentLength
  27. {
  28. get
  29. {
  30. if (Request.SegmentLength.HasValue)
  31. {
  32. return Request.SegmentLength.Value;
  33. }
  34. if (EncodingHelper.IsCopyCodec(OutputVideoCodec))
  35. {
  36. var userAgent = UserAgent ?? string.Empty;
  37. if (userAgent.IndexOf("AppleTV", StringComparison.OrdinalIgnoreCase) != -1 ||
  38. userAgent.IndexOf("cfnetwork", StringComparison.OrdinalIgnoreCase) != -1 ||
  39. userAgent.IndexOf("ipad", StringComparison.OrdinalIgnoreCase) != -1 ||
  40. userAgent.IndexOf("iphone", StringComparison.OrdinalIgnoreCase) != -1 ||
  41. userAgent.IndexOf("ipod", StringComparison.OrdinalIgnoreCase) != -1)
  42. {
  43. if (IsSegmentedLiveStream)
  44. {
  45. return 6;
  46. }
  47. return 6;
  48. }
  49. if (IsSegmentedLiveStream)
  50. {
  51. return 3;
  52. }
  53. return 6;
  54. }
  55. return 3;
  56. }
  57. }
  58. public int MinSegments
  59. {
  60. get
  61. {
  62. if (Request.MinSegments.HasValue)
  63. {
  64. return Request.MinSegments.Value;
  65. }
  66. return SegmentLength >= 10 ? 2 : 3;
  67. }
  68. }
  69. public string UserAgent { get; set; }
  70. public bool EstimateContentLength { get; set; }
  71. public TranscodeSeekInfo TranscodeSeekInfo { get; set; }
  72. public bool EnableDlnaHeaders { get; set; }
  73. public DeviceProfile DeviceProfile { get; set; }
  74. public TranscodingJob TranscodingJob { get; set; }
  75. public StreamState(IMediaSourceManager mediaSourceManager, TranscodingJobType transcodingType)
  76. : base(transcodingType)
  77. {
  78. _mediaSourceManager = mediaSourceManager;
  79. }
  80. public override void ReportTranscodingProgress(TimeSpan? transcodingPosition, float? framerate, double? percentComplete, long? bytesTranscoded, int? bitRate)
  81. {
  82. ApiEntryPoint.Instance.ReportTranscodingProgress(TranscodingJob, this, transcodingPosition, framerate, percentComplete, bytesTranscoded, bitRate);
  83. }
  84. public void Dispose()
  85. {
  86. Dispose(true);
  87. GC.SuppressFinalize(this);
  88. }
  89. protected virtual void Dispose(bool disposing)
  90. {
  91. if (_disposed)
  92. {
  93. return;
  94. }
  95. if (disposing)
  96. {
  97. // REVIEW: Is this the right place for this?
  98. if (MediaSource.RequiresClosing
  99. && string.IsNullOrWhiteSpace(Request.LiveStreamId)
  100. && !string.IsNullOrWhiteSpace(MediaSource.LiveStreamId))
  101. {
  102. _mediaSourceManager.CloseLiveStream(MediaSource.LiveStreamId).GetAwaiter().GetResult();
  103. }
  104. TranscodingThrottler?.Dispose();
  105. }
  106. TranscodingThrottler = null;
  107. TranscodingJob = null;
  108. _disposed = true;
  109. }
  110. }
  111. }