| | 1 | | using System; |
| | 2 | | using System.Diagnostics; |
| | 3 | | using System.IO; |
| | 4 | | using System.Threading; |
| | 5 | | using System.Threading.Tasks; |
| | 6 | | using MediaBrowser.Controller.MediaEncoding; |
| | 7 | | using MediaBrowser.Model.IO; |
| | 8 | |
|
| | 9 | | namespace MediaBrowser.Controller.Streaming; |
| | 10 | |
|
| | 11 | | /// <summary> |
| | 12 | | /// A progressive file stream for transferring transcoded files as they are written to. |
| | 13 | | /// </summary> |
| | 14 | | public class ProgressiveFileStream : Stream |
| | 15 | | { |
| | 16 | | private readonly Stream _stream; |
| | 17 | | private readonly TranscodingJob? _job; |
| | 18 | | private readonly ITranscodeManager? _transcodeManager; |
| | 19 | | private readonly int _timeoutMs; |
| | 20 | | private bool _disposed; |
| | 21 | |
|
| | 22 | | /// <summary> |
| | 23 | | /// Initializes a new instance of the <see cref="ProgressiveFileStream"/> class. |
| | 24 | | /// </summary> |
| | 25 | | /// <param name="filePath">The path to the transcoded file.</param> |
| | 26 | | /// <param name="job">The transcoding job information.</param> |
| | 27 | | /// <param name="transcodeManager">The transcode manager.</param> |
| | 28 | | /// <param name="timeoutMs">The timeout duration in milliseconds.</param> |
| 0 | 29 | | public ProgressiveFileStream(string filePath, TranscodingJob? job, ITranscodeManager transcodeManager, int timeoutMs |
| | 30 | | { |
| 0 | 31 | | _job = job; |
| 0 | 32 | | _transcodeManager = transcodeManager; |
| 0 | 33 | | _timeoutMs = timeoutMs; |
| | 34 | |
|
| 0 | 35 | | _stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, IODefaults.FileStreamBuf |
| 0 | 36 | | } |
| | 37 | |
|
| | 38 | | /// <summary> |
| | 39 | | /// Initializes a new instance of the <see cref="ProgressiveFileStream"/> class. |
| | 40 | | /// </summary> |
| | 41 | | /// <param name="stream">The stream to progressively copy.</param> |
| | 42 | | /// <param name="timeoutMs">The timeout duration in milliseconds.</param> |
| 0 | 43 | | public ProgressiveFileStream(Stream stream, int timeoutMs = 30000) |
| | 44 | | { |
| 0 | 45 | | _job = null; |
| 0 | 46 | | _transcodeManager = null; |
| 0 | 47 | | _timeoutMs = timeoutMs; |
| 0 | 48 | | _stream = stream; |
| 0 | 49 | | } |
| | 50 | |
|
| | 51 | | /// <inheritdoc /> |
| 0 | 52 | | public override bool CanRead => _stream.CanRead; |
| | 53 | |
|
| | 54 | | /// <inheritdoc /> |
| 0 | 55 | | public override bool CanSeek => false; |
| | 56 | |
|
| | 57 | | /// <inheritdoc /> |
| 0 | 58 | | public override bool CanWrite => false; |
| | 59 | |
|
| | 60 | | /// <inheritdoc /> |
| 0 | 61 | | public override long Length => throw new NotSupportedException(); |
| | 62 | |
|
| | 63 | | /// <inheritdoc /> |
| | 64 | | public override long Position |
| | 65 | | { |
| 0 | 66 | | get => throw new NotSupportedException(); |
| 0 | 67 | | set => throw new NotSupportedException(); |
| | 68 | | } |
| | 69 | |
|
| | 70 | | /// <inheritdoc /> |
| | 71 | | public override void Flush() |
| | 72 | | { |
| | 73 | | // Not supported |
| 0 | 74 | | } |
| | 75 | |
|
| | 76 | | /// <inheritdoc /> |
| | 77 | | public override int Read(byte[] buffer, int offset, int count) |
| 0 | 78 | | => Read(buffer.AsSpan(offset, count)); |
| | 79 | |
|
| | 80 | | /// <inheritdoc /> |
| | 81 | | public override int Read(Span<byte> buffer) |
| | 82 | | { |
| 0 | 83 | | int totalBytesRead = 0; |
| 0 | 84 | | var stopwatch = Stopwatch.StartNew(); |
| | 85 | |
|
| 0 | 86 | | while (true) |
| | 87 | | { |
| 0 | 88 | | totalBytesRead += _stream.Read(buffer); |
| 0 | 89 | | if (StopReading(totalBytesRead, stopwatch.ElapsedMilliseconds)) |
| | 90 | | { |
| | 91 | | break; |
| | 92 | | } |
| | 93 | |
|
| 0 | 94 | | Thread.Sleep(50); |
| | 95 | | } |
| | 96 | |
|
| 0 | 97 | | UpdateBytesWritten(totalBytesRead); |
| | 98 | |
|
| 0 | 99 | | return totalBytesRead; |
| | 100 | | } |
| | 101 | |
|
| | 102 | | /// <inheritdoc /> |
| | 103 | | public override async Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) |
| | 104 | | => await ReadAsync(buffer.AsMemory(offset, count), cancellationToken).ConfigureAwait(false); |
| | 105 | |
|
| | 106 | | /// <inheritdoc /> |
| | 107 | | public override async ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = default) |
| | 108 | | { |
| | 109 | | int totalBytesRead = 0; |
| | 110 | | var stopwatch = Stopwatch.StartNew(); |
| | 111 | |
|
| | 112 | | while (true) |
| | 113 | | { |
| | 114 | | totalBytesRead += await _stream.ReadAsync(buffer, cancellationToken).ConfigureAwait(false); |
| | 115 | | if (StopReading(totalBytesRead, stopwatch.ElapsedMilliseconds)) |
| | 116 | | { |
| | 117 | | break; |
| | 118 | | } |
| | 119 | |
|
| | 120 | | await Task.Delay(50, cancellationToken).ConfigureAwait(false); |
| | 121 | | } |
| | 122 | |
|
| | 123 | | UpdateBytesWritten(totalBytesRead); |
| | 124 | |
|
| | 125 | | return totalBytesRead; |
| | 126 | | } |
| | 127 | |
|
| | 128 | | /// <inheritdoc /> |
| | 129 | | public override long Seek(long offset, SeekOrigin origin) |
| 0 | 130 | | => throw new NotSupportedException(); |
| | 131 | |
|
| | 132 | | /// <inheritdoc /> |
| | 133 | | public override void SetLength(long value) |
| 0 | 134 | | => throw new NotSupportedException(); |
| | 135 | |
|
| | 136 | | /// <inheritdoc /> |
| | 137 | | public override void Write(byte[] buffer, int offset, int count) |
| 0 | 138 | | => throw new NotSupportedException(); |
| | 139 | |
|
| | 140 | | /// <inheritdoc /> |
| | 141 | | protected override void Dispose(bool disposing) |
| | 142 | | { |
| 0 | 143 | | if (_disposed) |
| | 144 | | { |
| 0 | 145 | | return; |
| | 146 | | } |
| | 147 | |
|
| | 148 | | try |
| | 149 | | { |
| 0 | 150 | | if (disposing) |
| | 151 | | { |
| 0 | 152 | | _stream.Dispose(); |
| | 153 | |
|
| 0 | 154 | | if (_job is not null) |
| | 155 | | { |
| 0 | 156 | | _transcodeManager?.OnTranscodeEndRequest(_job); |
| | 157 | | } |
| | 158 | | } |
| 0 | 159 | | } |
| | 160 | | finally |
| | 161 | | { |
| 0 | 162 | | _disposed = true; |
| 0 | 163 | | base.Dispose(disposing); |
| 0 | 164 | | } |
| 0 | 165 | | } |
| | 166 | |
|
| | 167 | | private void UpdateBytesWritten(int totalBytesRead) |
| | 168 | | { |
| 0 | 169 | | if (_job is not null) |
| | 170 | | { |
| 0 | 171 | | _job.BytesDownloaded += totalBytesRead; |
| | 172 | | } |
| 0 | 173 | | } |
| | 174 | |
|
| | 175 | | private bool StopReading(int bytesRead, long elapsed) |
| | 176 | | { |
| | 177 | | // It should stop reading when anything has been successfully read or if the job has exited |
| | 178 | | // If the job is null, however, it's a live stream and will require user action to close, |
| | 179 | | // but don't keep it open indefinitely if it isn't reading anything |
| 0 | 180 | | return bytesRead > 0 || (_job?.HasExited ?? elapsed >= _timeoutMs); |
| | 181 | | } |
| | 182 | | } |