jellyfin/MediaBrowser.Api/Playback/StreamState.cs

260 lines
7.3 KiB
C#
Raw Normal View History

2015-03-28 20:22:27 +00:00
using MediaBrowser.Controller.Library;
2014-04-01 22:23:07 +00:00
using MediaBrowser.Model.Dlna;
using MediaBrowser.Model.Drawing;
2015-03-28 20:22:27 +00:00
using MediaBrowser.Model.Dto;
using MediaBrowser.Model.Entities;
2013-08-10 01:16:31 +00:00
using MediaBrowser.Model.IO;
2014-04-05 15:02:50 +00:00
using MediaBrowser.Model.Logging;
2014-04-25 17:30:41 +00:00
using MediaBrowser.Model.MediaInfo;
2015-02-28 18:47:05 +00:00
using MediaBrowser.Model.Net;
2014-04-05 15:02:50 +00:00
using System;
2013-12-19 21:51:32 +00:00
using System.Collections.Generic;
2014-04-25 17:30:41 +00:00
using System.Globalization;
2013-12-19 21:51:32 +00:00
using System.IO;
2016-12-13 17:04:37 +00:00
using System.Linq;
2014-01-12 21:32:13 +00:00
using System.Threading;
using MediaBrowser.Controller.MediaEncoding;
2013-02-27 04:19:05 +00:00
namespace MediaBrowser.Api.Playback
{
public class StreamState : EncodingJobInfo, IDisposable
2013-02-27 04:19:05 +00:00
{
2014-04-05 15:02:50 +00:00
private readonly ILogger _logger;
2015-03-28 20:22:27 +00:00
private readonly IMediaSourceManager _mediaSourceManager;
2014-04-05 15:02:50 +00:00
2013-12-19 21:51:32 +00:00
public string RequestedUrl { get; set; }
2013-02-27 04:19:05 +00:00
public StreamRequest Request
{
get { return (StreamRequest)BaseRequest; }
set
{
BaseRequest = value;
IsVideoRequest = VideoRequest != null;
}
}
2015-02-28 18:47:05 +00:00
public TranscodingThrottler TranscodingThrottler { get; set; }
2013-02-27 04:19:05 +00:00
2013-03-09 02:34:54 +00:00
public VideoStreamRequest VideoRequest
{
2014-01-22 17:05:32 +00:00
get { return Request as VideoStreamRequest; }
2013-03-09 02:34:54 +00:00
}
2013-02-27 04:19:05 +00:00
/// <summary>
/// Gets or sets the log file stream.
/// </summary>
/// <value>The log file stream.</value>
public Stream LogFileStream { get; set; }
2016-10-05 07:15:29 +00:00
public IDirectStreamProvider DirectStreamProvider { get; set; }
2013-02-27 04:19:05 +00:00
2015-03-18 06:02:22 +00:00
public string WaitForPath { get; set; }
2013-12-19 21:51:32 +00:00
2015-05-24 18:33:28 +00:00
public bool IsOutputVideo
{
get { return Request is VideoStreamRequest; }
}
2013-12-21 18:37:34 +00:00
2016-04-20 18:51:47 +00:00
public int SegmentLength
{
get
{
if (Request.SegmentLength.HasValue)
{
return Request.SegmentLength.Value;
}
2016-04-20 18:51:47 +00:00
if (string.Equals(OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase))
{
2016-06-30 21:26:43 +00:00
var userAgent = UserAgent ?? string.Empty;
2017-05-23 16:43:24 +00:00
if (userAgent.IndexOf("AppleTV", StringComparison.OrdinalIgnoreCase) != -1 ||
userAgent.IndexOf("cfnetwork", StringComparison.OrdinalIgnoreCase) != -1 ||
2016-08-05 05:12:25 +00:00
userAgent.IndexOf("ipad", StringComparison.OrdinalIgnoreCase) != -1 ||
userAgent.IndexOf("iphone", StringComparison.OrdinalIgnoreCase) != -1 ||
userAgent.IndexOf("ipod", StringComparison.OrdinalIgnoreCase) != -1)
2016-07-23 05:03:16 +00:00
{
2017-05-23 16:43:24 +00:00
if (IsSegmentedLiveStream)
{
return 6;
}
2017-07-19 19:53:47 +00:00
return 6;
2016-07-23 05:03:16 +00:00
}
2016-06-30 21:26:43 +00:00
2017-01-07 08:09:24 +00:00
if (IsSegmentedLiveStream)
2016-09-25 18:39:13 +00:00
{
return 3;
}
2017-01-07 08:09:24 +00:00
return 6;
2016-09-19 15:41:35 +00:00
}
2017-01-03 05:15:59 +00:00
2016-04-20 18:51:47 +00:00
return 3;
}
}
public int MinSegments
{
get
{
if (Request.MinSegments.HasValue)
{
return Request.MinSegments.Value;
}
return SegmentLength >= 10 ? 2 : 3;
}
}
2014-10-16 03:26:39 +00:00
public int HlsListSize
{
2014-11-11 03:41:55 +00:00
get
{
2015-09-09 17:49:44 +00:00
return 0;
2014-11-11 03:41:55 +00:00
}
2014-10-16 03:26:39 +00:00
}
2014-01-12 21:32:13 +00:00
2016-06-30 21:26:43 +00:00
public string UserAgent { get; set; }
2014-04-10 15:06:54 +00:00
public StreamState(IMediaSourceManager mediaSourceManager, ILogger logger, TranscodingJobType transcodingType)
2017-04-09 21:38:59 +00:00
: base(logger, transcodingType)
2014-04-05 15:02:50 +00:00
{
2015-03-28 20:22:27 +00:00
_mediaSourceManager = mediaSourceManager;
2014-04-05 15:02:50 +00:00
_logger = logger;
}
2014-03-25 05:25:03 +00:00
public string MimeType { get; set; }
public bool EstimateContentLength { get; set; }
public TranscodeSeekInfo TranscodeSeekInfo { get; set; }
2014-06-28 19:35:30 +00:00
public long? EncodingDurationTicks { get; set; }
2017-08-04 20:29:34 +00:00
public string GetMimeType(string outputPath, bool enableStreamDefault = true)
{
2014-03-25 05:25:03 +00:00
if (!string.IsNullOrEmpty(MimeType))
{
return MimeType;
}
2017-08-04 20:29:34 +00:00
return MimeTypes.GetMimeType(outputPath, enableStreamDefault);
}
2014-04-05 15:02:50 +00:00
2017-05-05 17:55:38 +00:00
public bool EnableDlnaHeaders { get; set; }
2014-04-05 15:02:50 +00:00
public void Dispose()
{
2015-02-28 18:47:05 +00:00
DisposeTranscodingThrottler();
2014-04-05 15:02:50 +00:00
DisposeLiveStream();
DisposeLogStream();
DisposeIsoMount();
2017-03-24 15:03:49 +00:00
TranscodingJob = null;
2014-04-05 15:02:50 +00:00
}
private void DisposeLogStream()
{
if (LogFileStream != null)
{
2014-04-06 17:53:23 +00:00
try
{
LogFileStream.Dispose();
}
catch (Exception ex)
{
_logger.ErrorException("Error disposing log stream", ex);
}
2014-04-05 15:02:50 +00:00
LogFileStream = null;
}
}
2015-02-28 18:47:05 +00:00
private void DisposeTranscodingThrottler()
{
if (TranscodingThrottler != null)
{
try
{
TranscodingThrottler.Dispose();
}
catch (Exception ex)
{
_logger.ErrorException("Error disposing TranscodingThrottler", ex);
}
TranscodingThrottler = null;
}
}
2014-04-05 15:02:50 +00:00
private async void DisposeLiveStream()
{
2016-07-28 06:29:14 +00:00
if (MediaSource.RequiresClosing && string.IsNullOrWhiteSpace(Request.LiveStreamId) && !string.IsNullOrWhiteSpace(MediaSource.LiveStreamId))
2014-04-05 15:02:50 +00:00
{
try
{
2016-09-29 12:55:49 +00:00
await _mediaSourceManager.CloseLiveStream(MediaSource.LiveStreamId).ConfigureAwait(false);
2014-04-05 15:02:50 +00:00
}
catch (Exception ex)
{
2015-03-28 20:22:27 +00:00
_logger.ErrorException("Error closing media source", ex);
2014-04-05 15:02:50 +00:00
}
}
}
public string OutputFilePath { get; set; }
2014-10-20 03:04:45 +00:00
public string ActualOutputVideoCodec
{
get
{
var codec = OutputVideoCodec;
if (string.Equals(codec, "copy", StringComparison.OrdinalIgnoreCase))
{
var stream = VideoStream;
if (stream != null)
{
return stream.Codec;
}
return null;
}
return codec;
}
}
public string ActualOutputAudioCodec
{
get
{
var codec = OutputAudioCodec;
if (string.Equals(codec, "copy", StringComparison.OrdinalIgnoreCase))
{
var stream = AudioStream;
if (stream != null)
{
return stream.Codec;
}
return null;
}
return codec;
}
}
2014-04-23 02:47:46 +00:00
public DeviceProfile DeviceProfile { get; set; }
2017-03-24 15:03:49 +00:00
public TranscodingJob TranscodingJob;
public override void ReportTranscodingProgress(TimeSpan? transcodingPosition, float? framerate, double? percentComplete, long? bytesTranscoded, int? bitRate)
{
ApiEntryPoint.Instance.ReportTranscodingProgress(TranscodingJob, this, transcodingPosition, framerate, percentComplete, bytesTranscoded, bitRate);
}
2013-02-27 04:19:05 +00:00
}
}