2014-04-25 17:30:41 +00:00
|
|
|
|
using MediaBrowser.Common.Net;
|
2014-04-05 15:02:50 +00:00
|
|
|
|
using MediaBrowser.Controller.LiveTv;
|
2014-04-01 22:23:07 +00:00
|
|
|
|
using MediaBrowser.Model.Dlna;
|
2014-04-23 00:50:47 +00:00
|
|
|
|
using MediaBrowser.Model.Drawing;
|
2014-03-12 19:56:12 +00:00
|
|
|
|
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;
|
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;
|
2014-01-12 21:32:13 +00:00
|
|
|
|
using System.Threading;
|
2013-02-27 04:19:05 +00:00
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Api.Playback
|
|
|
|
|
{
|
2014-04-05 15:02:50 +00:00
|
|
|
|
public class StreamState : IDisposable
|
2013-02-27 04:19:05 +00:00
|
|
|
|
{
|
2014-04-05 15:02:50 +00:00
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
private readonly ILiveTvManager _liveTvManager;
|
|
|
|
|
|
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; set; }
|
|
|
|
|
|
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
|
|
|
|
}
|
2014-04-25 02:00:19 +00:00
|
|
|
|
|
2014-06-02 19:32:41 +00:00
|
|
|
|
public Dictionary<string, string> RemoteHttpHeaders { get; set; }
|
|
|
|
|
|
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; }
|
|
|
|
|
|
2014-04-27 18:09:16 +00:00
|
|
|
|
public string InputContainer { get; set; }
|
2014-06-02 19:32:41 +00:00
|
|
|
|
|
2014-08-06 01:09:03 +00:00
|
|
|
|
public List<MediaStream> AllMediaStreams { get; set; }
|
|
|
|
|
|
2013-02-27 04:19:05 +00:00
|
|
|
|
public MediaStream AudioStream { get; set; }
|
|
|
|
|
public MediaStream VideoStream { get; set; }
|
|
|
|
|
public MediaStream SubtitleStream { get; set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the iso mount.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The iso mount.</value>
|
|
|
|
|
public IIsoMount IsoMount { get; set; }
|
2013-12-19 21:51:32 +00:00
|
|
|
|
|
|
|
|
|
public string MediaPath { get; set; }
|
|
|
|
|
|
2014-06-17 01:56:23 +00:00
|
|
|
|
public MediaProtocol InputProtocol { get; set; }
|
2013-12-19 21:51:32 +00:00
|
|
|
|
|
|
|
|
|
public bool IsInputVideo { get; set; }
|
|
|
|
|
|
|
|
|
|
public VideoType VideoType { get; set; }
|
|
|
|
|
public IsoType? IsoType { get; set; }
|
|
|
|
|
|
|
|
|
|
public List<string> PlayableStreamFileNames { get; set; }
|
2013-12-21 18:37:34 +00:00
|
|
|
|
|
2014-01-05 06:50:48 +00:00
|
|
|
|
public string LiveTvStreamId { get; set; }
|
2014-01-12 21:32:13 +00:00
|
|
|
|
|
|
|
|
|
public int SegmentLength = 10;
|
2014-03-04 04:58:19 +00:00
|
|
|
|
public int HlsListSize;
|
2014-01-12 21:32:13 +00:00
|
|
|
|
|
|
|
|
|
public long? RunTimeTicks;
|
2014-01-15 05:05:19 +00:00
|
|
|
|
|
2014-04-23 00:50:47 +00:00
|
|
|
|
public string OutputAudioSync = "1";
|
|
|
|
|
public string OutputVideoSync = "vfr";
|
2014-01-20 15:04:50 +00:00
|
|
|
|
|
2014-04-10 15:06:54 +00:00
|
|
|
|
public List<string> SupportedAudioCodecs { get; set; }
|
|
|
|
|
|
2014-04-05 15:02:50 +00:00
|
|
|
|
public StreamState(ILiveTvManager liveTvManager, ILogger logger)
|
|
|
|
|
{
|
|
|
|
|
_liveTvManager = liveTvManager;
|
|
|
|
|
_logger = logger;
|
2014-04-10 15:06:54 +00:00
|
|
|
|
SupportedAudioCodecs = new List<string>();
|
2014-06-04 19:40:05 +00:00
|
|
|
|
PlayableStreamFileNames = new List<string>();
|
|
|
|
|
RemoteHttpHeaders = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
|
2014-08-06 01:09:03 +00:00
|
|
|
|
AllMediaStreams = new List<MediaStream>();
|
2014-04-05 15:02:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-03-21 04:52:28 +00:00
|
|
|
|
public string InputAudioSync { get; set; }
|
|
|
|
|
public string InputVideoSync { get; set; }
|
2014-04-25 02:00:19 +00:00
|
|
|
|
|
2014-01-20 15:04:50 +00:00
|
|
|
|
public bool DeInterlace { get; set; }
|
2014-01-22 01:37:01 +00:00
|
|
|
|
public bool ReadInputAtNativeFramerate { get; set; }
|
2014-03-12 19:56:12 +00:00
|
|
|
|
|
2014-04-25 02:00:19 +00:00
|
|
|
|
public TransportStreamTimestamp InputTimestamp { get; set; }
|
|
|
|
|
|
2014-03-25 05:25:03 +00:00
|
|
|
|
public string MimeType { get; set; }
|
|
|
|
|
|
|
|
|
|
public bool EstimateContentLength { get; set; }
|
|
|
|
|
public bool EnableMpegtsM2TsMode { get; set; }
|
|
|
|
|
public TranscodeSeekInfo TranscodeSeekInfo { get; set; }
|
2014-04-25 02:00:19 +00:00
|
|
|
|
|
2014-06-28 19:35:30 +00:00
|
|
|
|
public long? EncodingDurationTicks { get; set; }
|
|
|
|
|
|
|
|
|
|
public string ItemType { get; set; }
|
|
|
|
|
|
2014-03-12 19:56:12 +00:00
|
|
|
|
public string GetMimeType(string outputPath)
|
|
|
|
|
{
|
2014-03-25 05:25:03 +00:00
|
|
|
|
if (!string.IsNullOrEmpty(MimeType))
|
|
|
|
|
{
|
|
|
|
|
return MimeType;
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-12 19:56:12 +00:00
|
|
|
|
return MimeTypes.GetMimeType(outputPath);
|
|
|
|
|
}
|
2014-04-05 15:02:50 +00:00
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
DisposeLiveStream();
|
|
|
|
|
DisposeLogStream();
|
|
|
|
|
DisposeIsoMount();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void DisposeIsoMount()
|
|
|
|
|
{
|
|
|
|
|
if (IsoMount != null)
|
|
|
|
|
{
|
2014-04-06 17:53:23 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
IsoMount.Dispose();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error disposing iso mount", ex);
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-05 15:02:50 +00:00
|
|
|
|
IsoMount = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async void DisposeLiveStream()
|
|
|
|
|
{
|
|
|
|
|
if (!string.IsNullOrEmpty(LiveTvStreamId))
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await _liveTvManager.CloseLiveStream(LiveTvStreamId, CancellationToken.None).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error closing live tv stream", ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-04-23 00:50:47 +00:00
|
|
|
|
|
2014-06-12 02:38:40 +00:00
|
|
|
|
public int InternalSubtitleStreamOffset { get; set; }
|
|
|
|
|
|
2014-06-06 00:39:02 +00:00
|
|
|
|
public string OutputFilePath { get; set; }
|
|
|
|
|
public string OutputVideoCodec { get; set; }
|
|
|
|
|
public string OutputAudioCodec { get; set; }
|
2014-04-23 00:50:47 +00:00
|
|
|
|
public int? OutputAudioChannels;
|
|
|
|
|
public int? OutputAudioSampleRate;
|
|
|
|
|
public int? OutputAudioBitrate;
|
|
|
|
|
public int? OutputVideoBitrate;
|
|
|
|
|
|
|
|
|
|
public string OutputContainer { get; set; }
|
|
|
|
|
|
2014-04-23 02:47:46 +00:00
|
|
|
|
public DeviceProfile DeviceProfile { get; set; }
|
|
|
|
|
|
2014-04-23 00:50:47 +00:00
|
|
|
|
public int? TotalOutputBitrate
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return (OutputAudioBitrate ?? 0) + (OutputVideoBitrate ?? 0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int? OutputWidth
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (VideoStream != null && VideoStream.Width.HasValue && VideoStream.Height.HasValue)
|
|
|
|
|
{
|
|
|
|
|
var size = new ImageSize
|
|
|
|
|
{
|
|
|
|
|
Width = VideoStream.Width.Value,
|
|
|
|
|
Height = VideoStream.Height.Value
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var newSize = DrawingUtils.Resize(size,
|
|
|
|
|
VideoRequest.Width,
|
|
|
|
|
VideoRequest.Height,
|
|
|
|
|
VideoRequest.MaxWidth,
|
|
|
|
|
VideoRequest.MaxHeight);
|
|
|
|
|
|
|
|
|
|
return Convert.ToInt32(newSize.Width);
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-06 17:14:02 +00:00
|
|
|
|
if (VideoRequest == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-23 00:50:47 +00:00
|
|
|
|
return VideoRequest.MaxWidth ?? VideoRequest.Width;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int? OutputHeight
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (VideoStream != null && VideoStream.Width.HasValue && VideoStream.Height.HasValue)
|
|
|
|
|
{
|
|
|
|
|
var size = new ImageSize
|
|
|
|
|
{
|
|
|
|
|
Width = VideoStream.Width.Value,
|
|
|
|
|
Height = VideoStream.Height.Value
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var newSize = DrawingUtils.Resize(size,
|
|
|
|
|
VideoRequest.Width,
|
|
|
|
|
VideoRequest.Height,
|
|
|
|
|
VideoRequest.MaxWidth,
|
|
|
|
|
VideoRequest.MaxHeight);
|
|
|
|
|
|
|
|
|
|
return Convert.ToInt32(newSize.Height);
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-06 17:14:02 +00:00
|
|
|
|
if (VideoRequest == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-23 00:50:47 +00:00
|
|
|
|
return VideoRequest.MaxHeight ?? VideoRequest.Height;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-04-24 05:08:10 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Predicts the audio sample rate that will be in the output stream
|
|
|
|
|
/// </summary>
|
|
|
|
|
public int? TargetVideoBitDepth
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
var stream = VideoStream;
|
|
|
|
|
return stream == null || !Request.Static ? null : stream.BitDepth;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Predicts the audio sample rate that will be in the output stream
|
|
|
|
|
/// </summary>
|
2014-06-23 16:05:19 +00:00
|
|
|
|
public float? TargetFramerate
|
2014-04-24 05:08:10 +00:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
var stream = VideoStream;
|
|
|
|
|
var requestedFramerate = VideoRequest.MaxFramerate ?? VideoRequest.Framerate;
|
|
|
|
|
|
|
|
|
|
return requestedFramerate.HasValue && !Request.Static
|
|
|
|
|
? requestedFramerate
|
|
|
|
|
: stream == null ? null : stream.AverageFrameRate ?? stream.RealFrameRate;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Predicts the audio sample rate that will be in the output stream
|
|
|
|
|
/// </summary>
|
|
|
|
|
public double? TargetVideoLevel
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
var stream = VideoStream;
|
|
|
|
|
return !string.IsNullOrEmpty(VideoRequest.Level) && !Request.Static
|
|
|
|
|
? double.Parse(VideoRequest.Level, CultureInfo.InvariantCulture)
|
|
|
|
|
: stream == null ? null : stream.Level;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public TransportStreamTimestamp TargetTimestamp
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2014-06-02 19:32:41 +00:00
|
|
|
|
var defaultValue = string.Equals(OutputContainer, "m2ts", StringComparison.OrdinalIgnoreCase) ?
|
|
|
|
|
TransportStreamTimestamp.Valid :
|
2014-04-25 02:45:06 +00:00
|
|
|
|
TransportStreamTimestamp.None;
|
2014-04-24 05:08:10 +00:00
|
|
|
|
|
|
|
|
|
return !Request.Static
|
2014-04-25 02:00:19 +00:00
|
|
|
|
? defaultValue
|
|
|
|
|
: InputTimestamp;
|
2014-04-24 05:08:10 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Predicts the audio sample rate that will be in the output stream
|
|
|
|
|
/// </summary>
|
|
|
|
|
public int? TargetPacketLength
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
var stream = VideoStream;
|
|
|
|
|
return !Request.Static
|
|
|
|
|
? null
|
|
|
|
|
: stream == null ? null : stream.PacketLength;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Predicts the audio sample rate that will be in the output stream
|
|
|
|
|
/// </summary>
|
|
|
|
|
public string TargetVideoProfile
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
var stream = VideoStream;
|
|
|
|
|
return !string.IsNullOrEmpty(VideoRequest.Profile) && !Request.Static
|
|
|
|
|
? VideoRequest.Profile
|
|
|
|
|
: stream == null ? null : stream.Profile;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-22 16:25:47 +00:00
|
|
|
|
public bool? IsTargetAnamorphic
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (Request.Static)
|
|
|
|
|
{
|
|
|
|
|
return VideoStream == null ? null : VideoStream.IsAnamorphic;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-02-27 04:19:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|