jellyfin/Jellyfin.Api/Models/StreamingDtos/StreamState.cs

238 lines
7.5 KiB
C#
Raw Normal View History

2020-07-11 05:14:23 -04:00
using System;
using Jellyfin.Api.Helpers;
using Jellyfin.Api.Models.PlaybackDtos;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.MediaEncoding;
using MediaBrowser.Model.Dlna;
2020-07-12 05:14:38 -04:00
namespace Jellyfin.Api.Models.StreamingDtos
2020-07-11 05:14:23 -04:00
{
2020-07-12 05:14:38 -04:00
/// <summary>
/// The stream state dto.
/// </summary>
2020-07-11 05:14:23 -04:00
public class StreamState : EncodingJobInfo, IDisposable
{
private readonly IMediaSourceManager _mediaSourceManager;
2020-07-12 05:14:38 -04:00
private readonly TranscodingJobHelper _transcodingJobHelper;
private bool _disposed;
/// <summary>
/// Initializes a new instance of the <see cref="StreamState" /> class.
/// </summary>
/// <param name="mediaSourceManager">Instance of the <see cref="mediaSourceManager" /> interface.</param>
/// <param name="transcodingType">The <see cref="TranscodingJobType" />.</param>
/// <param name="transcodingJobHelper">The <see cref="TranscodingJobHelper" /> singleton.</param>
public StreamState(IMediaSourceManager mediaSourceManager, TranscodingJobType transcodingType, TranscodingJobHelper transcodingJobHelper)
: base(transcodingType)
2020-07-11 05:14:23 -04:00
{
2020-07-12 05:14:38 -04:00
_mediaSourceManager = mediaSourceManager;
_transcodingJobHelper = transcodingJobHelper;
2020-07-11 05:14:23 -04:00
}
2020-07-12 05:14:38 -04:00
/// <summary>
/// Gets or sets the requested url.
/// </summary>
public string? RequestedUrl { get; set; }
// /// <summary>
// /// Gets or sets the request.
// /// </summary>
// public StreamRequest Request
// {
// get => (StreamRequest)BaseRequest;
// set
// {
// BaseRequest = value;
//
// IsVideoRequest = VideoRequest != null;
// }
// }
/// <summary>
/// Gets or sets the transcoding throttler.
/// </summary>
public TranscodingThrottler? TranscodingThrottler { get; set; }
/*/// <summary>
2020-07-12 05:14:38 -04:00
/// Gets the video request.
/// </summary>
public VideoStreamRequest VideoRequest => Request as VideoStreamRequest;*/
2020-07-11 05:14:23 -04:00
2020-07-12 05:14:38 -04:00
/// <summary>
/// Gets or sets the direct stream provicer.
/// </summary>
public IDirectStreamProvider? DirectStreamProvider { get; set; }
2020-07-11 05:14:23 -04:00
2020-07-12 05:14:38 -04:00
/// <summary>
/// Gets or sets the path to wait for.
/// </summary>
public string? WaitForPath { get; set; }
2020-07-11 05:14:23 -04:00
/*/// <summary>
2020-07-12 05:14:38 -04:00
/// Gets a value indicating whether the request outputs video.
/// </summary>
public bool IsOutputVideo => Request is VideoStreamRequest;*/
2020-07-11 05:14:23 -04:00
2020-07-12 05:14:38 -04:00
/// <summary>
/// Gets the segment length.
/// </summary>
2020-07-11 05:14:23 -04:00
public int SegmentLength
{
get
{
if (Request.SegmentLength.HasValue)
{
return Request.SegmentLength.Value;
}
if (EncodingHelper.IsCopyCodec(OutputVideoCodec))
{
var userAgent = UserAgent ?? string.Empty;
if (userAgent.IndexOf("AppleTV", StringComparison.OrdinalIgnoreCase) != -1 ||
userAgent.IndexOf("cfnetwork", StringComparison.OrdinalIgnoreCase) != -1 ||
userAgent.IndexOf("ipad", StringComparison.OrdinalIgnoreCase) != -1 ||
userAgent.IndexOf("iphone", StringComparison.OrdinalIgnoreCase) != -1 ||
userAgent.IndexOf("ipod", StringComparison.OrdinalIgnoreCase) != -1)
{
if (IsSegmentedLiveStream)
{
return 6;
}
return 6;
}
if (IsSegmentedLiveStream)
{
return 3;
}
return 6;
}
return 3;
}
}
2020-07-12 05:14:38 -04:00
/// <summary>
/// Gets the minimum number of segments.
/// </summary>
2020-07-11 05:14:23 -04:00
public int MinSegments
{
get
{
if (Request.MinSegments.HasValue)
{
return Request.MinSegments.Value;
}
return SegmentLength >= 10 ? 2 : 3;
}
}
2020-07-12 05:14:38 -04:00
/// <summary>
/// Gets or sets the user agent.
/// </summary>
public string? UserAgent { get; set; }
2020-07-11 05:14:23 -04:00
2020-07-12 05:14:38 -04:00
/// <summary>
/// Gets or sets a value indicating whether to estimate the content length.
/// </summary>
2020-07-11 05:14:23 -04:00
public bool EstimateContentLength { get; set; }
2020-07-12 05:14:38 -04:00
/// <summary>
/// Gets or sets the transcode seek info.
/// </summary>
2020-07-11 05:14:23 -04:00
public TranscodeSeekInfo TranscodeSeekInfo { get; set; }
2020-07-12 05:14:38 -04:00
/// <summary>
/// Gets or sets a value indicating whether to enable dlna headers.
/// </summary>
2020-07-11 05:14:23 -04:00
public bool EnableDlnaHeaders { get; set; }
2020-07-12 05:14:38 -04:00
/// <summary>
/// Gets or sets the device profile.
/// </summary>
public DeviceProfile? DeviceProfile { get; set; }
2020-07-11 05:14:23 -04:00
2020-07-12 05:14:38 -04:00
/// <summary>
/// Gets or sets the transcoding job.
/// </summary>
public TranscodingJobDto? TranscodingJob { get; set; }
2020-07-11 05:14:23 -04:00
/// <summary>
/// Gets or sets the device id.
/// </summary>
public string? DeviceId { get; set; }
/// <summary>
/// Gets or sets the play session id.
/// </summary>
public string? PlaySessionId { get; set; }
/// <summary>
/// Gets or sets the live stream id.
/// </summary>
public string? LiveStreamId { get; set; }
/// <summary>
/// Gets or sets the video coded.
/// </summary>
public string? VideoCodec { get; set; }
/// <summary>
/// Gets or sets the audio codec.
/// </summary>
public string? AudioCodec { get; set; }
/// <summary>
/// Gets or sets the subtitle codec.
/// </summary>
public string? SubtitleCodec { get; set; }
2020-07-12 05:14:38 -04:00
/// <inheritdoc />
public void Dispose()
2020-07-11 05:14:23 -04:00
{
2020-07-12 05:14:38 -04:00
Dispose(true);
GC.SuppressFinalize(this);
2020-07-11 05:14:23 -04:00
}
2020-07-12 05:14:38 -04:00
/// <inheritdoc />
2020-07-11 05:14:23 -04:00
public override void ReportTranscodingProgress(TimeSpan? transcodingPosition, float? framerate, double? percentComplete, long? bytesTranscoded, int? bitRate)
{
2020-07-12 05:14:38 -04:00
_transcodingJobHelper.ReportTranscodingProgress(TranscodingJob!, this, transcodingPosition, framerate, percentComplete, bytesTranscoded, bitRate);
2020-07-11 05:14:23 -04:00
}
2020-07-12 05:14:38 -04:00
/// <summary>
/// Disposes the stream state.
/// </summary>
/// <param name="disposing">Whether the object is currently beeing disposed.</param>
2020-07-11 05:14:23 -04:00
protected virtual void Dispose(bool disposing)
{
if (_disposed)
{
return;
}
if (disposing)
{
// REVIEW: Is this the right place for this?
if (MediaSource.RequiresClosing
&& string.IsNullOrWhiteSpace(LiveStreamId)
2020-07-11 05:14:23 -04:00
&& !string.IsNullOrWhiteSpace(MediaSource.LiveStreamId))
{
_mediaSourceManager.CloseLiveStream(MediaSource.LiveStreamId).GetAwaiter().GetResult();
}
TranscodingThrottler?.Dispose();
}
TranscodingThrottler = null;
TranscodingJob = null;
_disposed = true;
}
}
}