jellyfin/MediaBrowser.Controller/Streaming/StreamState.cs

184 lines
5.3 KiB
C#
Raw Normal View History

2020-07-11 09:14:23 +00:00
using System;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.MediaEncoding;
using MediaBrowser.Model.Dlna;
2023-10-31 17:26:37 +00:00
namespace MediaBrowser.Controller.Streaming;
2023-01-31 11:18:10 +00:00
/// <summary>
/// The stream state dto.
/// </summary>
public class StreamState : EncodingJobInfo, IDisposable
2020-07-11 09:14:23 +00:00
{
2023-01-31 11:18:10 +00:00
private readonly IMediaSourceManager _mediaSourceManager;
2023-10-31 17:26:37 +00:00
private readonly ITranscodeManager _transcodeManager;
2023-01-31 11:18:10 +00:00
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>
2023-10-31 17:26:37 +00:00
/// <param name="transcodeManager">The <see cref="ITranscodeManager" /> singleton.</param>
public StreamState(IMediaSourceManager mediaSourceManager, TranscodingJobType transcodingType, ITranscodeManager transcodeManager)
2023-01-31 11:18:10 +00:00
: base(transcodingType)
{
_mediaSourceManager = mediaSourceManager;
2023-10-31 17:26:37 +00:00
_transcodeManager = transcodeManager;
2023-01-31 11:18:10 +00:00
}
/// <summary>
/// Gets or sets the requested url.
/// </summary>
public string? RequestedUrl { get; set; }
2020-07-12 09:14:38 +00:00
/// <summary>
2023-01-31 11:18:10 +00:00
/// Gets or sets the request.
2020-07-12 09:14:38 +00:00
/// </summary>
2023-01-31 11:18:10 +00:00
public StreamingRequestDto Request
2020-07-11 09:14:23 +00:00
{
2023-01-31 11:18:10 +00:00
get => (StreamingRequestDto)BaseRequest;
set
2020-07-11 09:14:23 +00:00
{
2023-01-31 11:18:10 +00:00
BaseRequest = value;
IsVideoRequest = VideoRequest is not null;
2020-07-11 09:14:23 +00:00
}
2023-01-31 11:18:10 +00:00
}
/// <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; }
2020-07-11 09:14:23 +00:00
2023-01-31 11:18:10 +00:00
/// <summary>
/// Gets a value indicating whether the request outputs video.
/// </summary>
public bool IsOutputVideo => Request is VideoRequestDto;
2020-07-12 09:14:38 +00:00
2023-01-31 11:18:10 +00:00
/// <summary>
/// Gets the segment length.
/// </summary>
public int SegmentLength
{
get
2020-07-22 14:57:06 +00:00
{
2023-01-31 11:18:10 +00:00
if (Request.SegmentLength.HasValue)
2020-07-22 14:57:06 +00:00
{
2023-01-31 11:18:10 +00:00
return Request.SegmentLength.Value;
2020-07-22 14:57:06 +00:00
}
2020-07-12 09:14:38 +00:00
2023-01-31 11:18:10 +00:00
if (EncodingHelper.IsCopyCodec(OutputVideoCodec))
2020-07-11 09:14:23 +00:00
{
2023-01-31 11:18:10 +00:00
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))
2020-07-11 09:14:23 +00:00
{
2023-01-31 11:18:10 +00:00
return 6;
2020-07-11 09:14:23 +00:00
}
2023-01-31 11:18:10 +00:00
if (IsSegmentedLiveStream)
2020-07-11 09:14:23 +00:00
{
2023-01-31 11:18:10 +00:00
return 3;
2020-07-11 09:14:23 +00:00
}
2023-01-31 11:18:10 +00:00
return 6;
2020-07-11 09:14:23 +00:00
}
2023-01-31 11:18:10 +00:00
return 3;
2020-07-11 09:14:23 +00:00
}
2023-01-31 11:18:10 +00:00
}
2020-07-11 09:14:23 +00:00
2023-01-31 11:18:10 +00:00
/// <summary>
/// Gets the minimum number of segments.
/// </summary>
public int MinSegments
{
get
2020-07-11 09:14:23 +00:00
{
2023-01-31 11:18:10 +00:00
if (Request.MinSegments.HasValue)
2020-07-11 09:14:23 +00:00
{
2023-01-31 11:18:10 +00:00
return Request.MinSegments.Value;
2020-07-11 09:14:23 +00:00
}
2023-01-31 11:18:10 +00:00
return SegmentLength >= 10 ? 2 : 3;
2020-07-11 09:14:23 +00:00
}
2023-01-31 11:18:10 +00:00
}
/// <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>
2023-10-31 15:31:09 +00:00
public TranscodingJob? TranscodingJob { get; set; }
2023-01-31 11:18:10 +00:00
/// <inheritdoc />
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <inheritdoc />
public override void ReportTranscodingProgress(TimeSpan? transcodingPosition, float? framerate, double? percentComplete, long? bytesTranscoded, int? bitRate)
{
2023-10-31 17:26:37 +00:00
_transcodeManager.ReportTranscodingProgress(TranscodingJob!, this, transcodingPosition, framerate, percentComplete, bytesTranscoded, bitRate);
2023-01-31 11:18:10 +00:00
}
/// <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)
2020-07-11 09:14:23 +00:00
{
2023-01-31 11:18:10 +00:00
return;
2020-07-11 09:14:23 +00:00
}
2023-01-31 11:18:10 +00:00
if (disposing)
2020-07-11 09:14:23 +00:00
{
2023-01-31 11:18:10 +00:00
// REVIEW: Is this the right place for this?
if (MediaSource.RequiresClosing
&& string.IsNullOrWhiteSpace(Request.LiveStreamId)
&& !string.IsNullOrWhiteSpace(MediaSource.LiveStreamId))
2020-07-11 09:14:23 +00:00
{
2023-01-31 11:18:10 +00:00
_mediaSourceManager.CloseLiveStream(MediaSource.LiveStreamId).GetAwaiter().GetResult();
2020-07-11 09:14:23 +00:00
}
2023-01-31 11:18:10 +00:00
}
2020-07-11 09:14:23 +00:00
2023-01-31 11:18:10 +00:00
TranscodingJob = null;
2020-07-11 09:14:23 +00:00
2023-01-31 11:18:10 +00:00
_disposed = true;
2020-07-11 09:14:23 +00:00
}
}