aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Controller/Streaming
diff options
context:
space:
mode:
Diffstat (limited to 'MediaBrowser.Controller/Streaming')
-rw-r--r--MediaBrowser.Controller/Streaming/ProgressiveFileStream.cs182
-rw-r--r--MediaBrowser.Controller/Streaming/StreamState.cs183
-rw-r--r--MediaBrowser.Controller/Streaming/StreamingRequestDto.cs49
-rw-r--r--MediaBrowser.Controller/Streaming/VideoRequestDto.cs23
4 files changed, 437 insertions, 0 deletions
diff --git a/MediaBrowser.Controller/Streaming/ProgressiveFileStream.cs b/MediaBrowser.Controller/Streaming/ProgressiveFileStream.cs
new file mode 100644
index 000000000..f44dc92d7
--- /dev/null
+++ b/MediaBrowser.Controller/Streaming/ProgressiveFileStream.cs
@@ -0,0 +1,182 @@
+using System;
+using System.Diagnostics;
+using System.IO;
+using System.Threading;
+using System.Threading.Tasks;
+using MediaBrowser.Controller.MediaEncoding;
+using MediaBrowser.Model.IO;
+
+namespace MediaBrowser.Controller.Streaming;
+
+/// <summary>
+/// A progressive file stream for transferring transcoded files as they are written to.
+/// </summary>
+public class ProgressiveFileStream : Stream
+{
+ private readonly Stream _stream;
+ private readonly TranscodingJob? _job;
+ private readonly ITranscodeManager? _transcodeManager;
+ private readonly int _timeoutMs;
+ private bool _disposed;
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="ProgressiveFileStream"/> class.
+ /// </summary>
+ /// <param name="filePath">The path to the transcoded file.</param>
+ /// <param name="job">The transcoding job information.</param>
+ /// <param name="transcodeManager">The transcode manager.</param>
+ /// <param name="timeoutMs">The timeout duration in milliseconds.</param>
+ public ProgressiveFileStream(string filePath, TranscodingJob? job, ITranscodeManager transcodeManager, int timeoutMs = 30000)
+ {
+ _job = job;
+ _transcodeManager = transcodeManager;
+ _timeoutMs = timeoutMs;
+
+ _stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, IODefaults.FileStreamBufferSize, FileOptions.Asynchronous | FileOptions.SequentialScan);
+ }
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="ProgressiveFileStream"/> class.
+ /// </summary>
+ /// <param name="stream">The stream to progressively copy.</param>
+ /// <param name="timeoutMs">The timeout duration in milliseconds.</param>
+ public ProgressiveFileStream(Stream stream, int timeoutMs = 30000)
+ {
+ _job = null;
+ _transcodeManager = null;
+ _timeoutMs = timeoutMs;
+ _stream = stream;
+ }
+
+ /// <inheritdoc />
+ public override bool CanRead => _stream.CanRead;
+
+ /// <inheritdoc />
+ public override bool CanSeek => false;
+
+ /// <inheritdoc />
+ public override bool CanWrite => false;
+
+ /// <inheritdoc />
+ public override long Length => throw new NotSupportedException();
+
+ /// <inheritdoc />
+ public override long Position
+ {
+ get => throw new NotSupportedException();
+ set => throw new NotSupportedException();
+ }
+
+ /// <inheritdoc />
+ public override void Flush()
+ {
+ // Not supported
+ }
+
+ /// <inheritdoc />
+ public override int Read(byte[] buffer, int offset, int count)
+ => Read(buffer.AsSpan(offset, count));
+
+ /// <inheritdoc />
+ public override int Read(Span<byte> buffer)
+ {
+ int totalBytesRead = 0;
+ var stopwatch = Stopwatch.StartNew();
+
+ while (true)
+ {
+ totalBytesRead += _stream.Read(buffer);
+ if (StopReading(totalBytesRead, stopwatch.ElapsedMilliseconds))
+ {
+ break;
+ }
+
+ Thread.Sleep(50);
+ }
+
+ UpdateBytesWritten(totalBytesRead);
+
+ return totalBytesRead;
+ }
+
+ /// <inheritdoc />
+ public override async Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
+ => await ReadAsync(buffer.AsMemory(offset, count), cancellationToken).ConfigureAwait(false);
+
+ /// <inheritdoc />
+ public override async ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = default)
+ {
+ int totalBytesRead = 0;
+ var stopwatch = Stopwatch.StartNew();
+
+ while (true)
+ {
+ totalBytesRead += await _stream.ReadAsync(buffer, cancellationToken).ConfigureAwait(false);
+ if (StopReading(totalBytesRead, stopwatch.ElapsedMilliseconds))
+ {
+ break;
+ }
+
+ await Task.Delay(50, cancellationToken).ConfigureAwait(false);
+ }
+
+ UpdateBytesWritten(totalBytesRead);
+
+ return totalBytesRead;
+ }
+
+ /// <inheritdoc />
+ public override long Seek(long offset, SeekOrigin origin)
+ => throw new NotSupportedException();
+
+ /// <inheritdoc />
+ public override void SetLength(long value)
+ => throw new NotSupportedException();
+
+ /// <inheritdoc />
+ public override void Write(byte[] buffer, int offset, int count)
+ => throw new NotSupportedException();
+
+ /// <inheritdoc />
+ protected override void Dispose(bool disposing)
+ {
+ if (_disposed)
+ {
+ return;
+ }
+
+ try
+ {
+ if (disposing)
+ {
+ _stream.Dispose();
+
+ if (_job is not null)
+ {
+ _transcodeManager?.OnTranscodeEndRequest(_job);
+ }
+ }
+ }
+ finally
+ {
+ _disposed = true;
+ base.Dispose(disposing);
+ }
+ }
+
+ private void UpdateBytesWritten(int totalBytesRead)
+ {
+ if (_job is not null)
+ {
+ _job.BytesDownloaded += totalBytesRead;
+ }
+ }
+
+ private bool StopReading(int bytesRead, long elapsed)
+ {
+ // It should stop reading when anything has been successfully read or if the job has exited
+ // If the job is null, however, it's a live stream and will require user action to close,
+ // but don't keep it open indefinitely if it isn't reading anything
+ return bytesRead > 0 || (_job?.HasExited ?? elapsed >= _timeoutMs);
+ }
+}
diff --git a/MediaBrowser.Controller/Streaming/StreamState.cs b/MediaBrowser.Controller/Streaming/StreamState.cs
new file mode 100644
index 000000000..b5dbe29ec
--- /dev/null
+++ b/MediaBrowser.Controller/Streaming/StreamState.cs
@@ -0,0 +1,183 @@
+using System;
+using MediaBrowser.Controller.Library;
+using MediaBrowser.Controller.MediaEncoding;
+using MediaBrowser.Model.Dlna;
+
+namespace MediaBrowser.Controller.Streaming;
+
+/// <summary>
+/// The stream state dto.
+/// </summary>
+public class StreamState : EncodingJobInfo, IDisposable
+{
+ private readonly IMediaSourceManager _mediaSourceManager;
+ private readonly ITranscodeManager _transcodeManager;
+ private bool _disposed;
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="StreamState" /> class.
+ /// </summary>
+ /// <param name="mediaSourceManager">Instance of the <see cref="IMediaSourceManager" /> interface.</param>
+ /// <param name="transcodingType">The <see cref="TranscodingJobType" />.</param>
+ /// <param name="transcodeManager">The <see cref="ITranscodeManager" /> singleton.</param>
+ public StreamState(IMediaSourceManager mediaSourceManager, TranscodingJobType transcodingType, ITranscodeManager transcodeManager)
+ : base(transcodingType)
+ {
+ _mediaSourceManager = mediaSourceManager;
+ _transcodeManager = transcodeManager;
+ }
+
+ /// <summary>
+ /// Gets or sets the requested url.
+ /// </summary>
+ public string? RequestedUrl { get; set; }
+
+ /// <summary>
+ /// Gets or sets the request.
+ /// </summary>
+ public StreamingRequestDto Request
+ {
+ get => (StreamingRequestDto)BaseRequest;
+ set
+ {
+ BaseRequest = value;
+ IsVideoRequest = VideoRequest is not null;
+ }
+ }
+
+ /// <summary>
+ /// Gets the video request.
+ /// </summary>
+ public VideoRequestDto? VideoRequest => Request as VideoRequestDto;
+
+ /// <summary>
+ /// Gets or sets the direct stream provicer.
+ /// </summary>
+ /// <remarks>
+ /// Deprecated.
+ /// </remarks>
+ public IDirectStreamProvider? DirectStreamProvider { get; set; }
+
+ /// <summary>
+ /// Gets or sets the path to wait for.
+ /// </summary>
+ public string? WaitForPath { get; set; }
+
+ /// <summary>
+ /// Gets a value indicating whether the request outputs video.
+ /// </summary>
+ public bool IsOutputVideo => Request is VideoRequestDto;
+
+ /// <summary>
+ /// Gets the segment length.
+ /// </summary>
+ public int SegmentLength
+ {
+ get
+ {
+ if (Request.SegmentLength.HasValue)
+ {
+ return Request.SegmentLength.Value;
+ }
+
+ if (EncodingHelper.IsCopyCodec(OutputVideoCodec))
+ {
+ var userAgent = UserAgent ?? string.Empty;
+
+ if (userAgent.Contains("AppleTV", StringComparison.OrdinalIgnoreCase)
+ || userAgent.Contains("cfnetwork", StringComparison.OrdinalIgnoreCase)
+ || userAgent.Contains("ipad", StringComparison.OrdinalIgnoreCase)
+ || userAgent.Contains("iphone", StringComparison.OrdinalIgnoreCase)
+ || userAgent.Contains("ipod", StringComparison.OrdinalIgnoreCase))
+ {
+ return 6;
+ }
+
+ if (IsSegmentedLiveStream)
+ {
+ return 3;
+ }
+
+ return 6;
+ }
+
+ return 3;
+ }
+ }
+
+ /// <summary>
+ /// Gets the minimum number of segments.
+ /// </summary>
+ public int MinSegments
+ {
+ get
+ {
+ if (Request.MinSegments.HasValue)
+ {
+ return Request.MinSegments.Value;
+ }
+
+ return SegmentLength >= 10 ? 2 : 3;
+ }
+ }
+
+ /// <summary>
+ /// Gets or sets the user agent.
+ /// </summary>
+ public string? UserAgent { get; set; }
+
+ /// <summary>
+ /// Gets or sets a value indicating whether to estimate the content length.
+ /// </summary>
+ public bool EstimateContentLength { get; set; }
+
+ /// <summary>
+ /// Gets or sets the transcode seek info.
+ /// </summary>
+ public TranscodeSeekInfo TranscodeSeekInfo { get; set; }
+
+ /// <summary>
+ /// Gets or sets the transcoding job.
+ /// </summary>
+ public TranscodingJob? TranscodingJob { get; set; }
+
+ /// <inheritdoc />
+ public void Dispose()
+ {
+ Dispose(true);
+ GC.SuppressFinalize(this);
+ }
+
+ /// <inheritdoc />
+ public override void ReportTranscodingProgress(TimeSpan? transcodingPosition, float? framerate, double? percentComplete, long? bytesTranscoded, int? bitRate)
+ {
+ _transcodeManager.ReportTranscodingProgress(TranscodingJob!, this, transcodingPosition, framerate, percentComplete, bytesTranscoded, bitRate);
+ }
+
+ /// <summary>
+ /// Disposes the stream state.
+ /// </summary>
+ /// <param name="disposing">Whether the object is currently being disposed.</param>
+ protected virtual void Dispose(bool disposing)
+ {
+ if (_disposed)
+ {
+ return;
+ }
+
+ if (disposing)
+ {
+ // REVIEW: Is this the right place for this?
+ if (MediaSource.RequiresClosing
+ && string.IsNullOrWhiteSpace(Request.LiveStreamId)
+ && !string.IsNullOrWhiteSpace(MediaSource.LiveStreamId))
+ {
+ _mediaSourceManager.CloseLiveStream(MediaSource.LiveStreamId).GetAwaiter().GetResult();
+ }
+ }
+
+ TranscodingJob = null;
+
+ _disposed = true;
+ }
+}
diff --git a/MediaBrowser.Controller/Streaming/StreamingRequestDto.cs b/MediaBrowser.Controller/Streaming/StreamingRequestDto.cs
new file mode 100644
index 000000000..e47ef65f0
--- /dev/null
+++ b/MediaBrowser.Controller/Streaming/StreamingRequestDto.cs
@@ -0,0 +1,49 @@
+using MediaBrowser.Controller.MediaEncoding;
+
+namespace MediaBrowser.Controller.Streaming;
+
+/// <summary>
+/// The audio streaming request dto.
+/// </summary>
+public class StreamingRequestDto : BaseEncodingJobOptions
+{
+ /// <summary>
+ /// Gets or sets the params.
+ /// </summary>
+ public string? Params { get; set; }
+
+ /// <summary>
+ /// Gets or sets the play session id.
+ /// </summary>
+ public string? PlaySessionId { get; set; }
+
+ /// <summary>
+ /// Gets or sets the tag.
+ /// </summary>
+ public string? Tag { get; set; }
+
+ /// <summary>
+ /// Gets or sets the segment container.
+ /// </summary>
+ public string? SegmentContainer { get; set; }
+
+ /// <summary>
+ /// Gets or sets the segment length.
+ /// </summary>
+ public int? SegmentLength { get; set; }
+
+ /// <summary>
+ /// Gets or sets the min segments.
+ /// </summary>
+ public int? MinSegments { get; set; }
+
+ /// <summary>
+ /// Gets or sets the position of the requested segment in ticks.
+ /// </summary>
+ public long CurrentRuntimeTicks { get; set; }
+
+ /// <summary>
+ /// Gets or sets the actual segment length in ticks.
+ /// </summary>
+ public long ActualSegmentLengthTicks { get; set; }
+}
diff --git a/MediaBrowser.Controller/Streaming/VideoRequestDto.cs b/MediaBrowser.Controller/Streaming/VideoRequestDto.cs
new file mode 100644
index 000000000..44dc831fd
--- /dev/null
+++ b/MediaBrowser.Controller/Streaming/VideoRequestDto.cs
@@ -0,0 +1,23 @@
+namespace MediaBrowser.Controller.Streaming;
+
+/// <summary>
+/// The video request dto.
+/// </summary>
+public class VideoRequestDto : StreamingRequestDto
+{
+ /// <summary>
+ /// Gets a value indicating whether this instance has fixed resolution.
+ /// </summary>
+ /// <value><c>true</c> if this instance has fixed resolution; otherwise, <c>false</c>.</value>
+ public bool HasFixedResolution => Width.HasValue || Height.HasValue;
+
+ /// <summary>
+ /// Gets or sets a value indicating whether to enable subtitles in the manifest.
+ /// </summary>
+ public bool EnableSubtitlesInManifest { get; set; }
+
+ /// <summary>
+ /// Gets or sets a value indicating whether to enable trickplay images.
+ /// </summary>
+ public bool EnableTrickplay { get; set; }
+}