2016-10-11 06:46:59 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using MediaBrowser.Controller.MediaEncoding;
|
|
|
|
|
using MediaBrowser.Model.Dlna;
|
|
|
|
|
using MediaBrowser.Model.Dto;
|
2017-03-29 19:16:43 +00:00
|
|
|
|
using MediaBrowser.Model.Entities;
|
2016-10-11 06:46:59 +00:00
|
|
|
|
using MediaBrowser.Model.Logging;
|
|
|
|
|
|
2016-11-03 23:35:19 +00:00
|
|
|
|
namespace Emby.Server.Implementations.LiveTv
|
2016-10-11 06:46:59 +00:00
|
|
|
|
{
|
|
|
|
|
public class LiveStreamHelper
|
|
|
|
|
{
|
|
|
|
|
private readonly IMediaEncoder _mediaEncoder;
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
|
2017-06-03 07:36:32 +00:00
|
|
|
|
const int AnalyzeDurationMs = 1000;
|
2017-03-05 15:38:36 +00:00
|
|
|
|
|
2016-10-11 06:46:59 +00:00
|
|
|
|
public LiveStreamHelper(IMediaEncoder mediaEncoder, ILogger logger)
|
|
|
|
|
{
|
|
|
|
|
_mediaEncoder = mediaEncoder;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-17 21:11:13 +00:00
|
|
|
|
public async Task AddMediaInfoWithProbe(MediaSourceInfo mediaSource, bool isAudio, CancellationToken cancellationToken)
|
2016-10-11 06:46:59 +00:00
|
|
|
|
{
|
|
|
|
|
var originalRuntime = mediaSource.RunTimeTicks;
|
|
|
|
|
|
|
|
|
|
var now = DateTime.UtcNow;
|
|
|
|
|
|
|
|
|
|
var info = await _mediaEncoder.GetMediaInfo(new MediaInfoRequest
|
|
|
|
|
{
|
|
|
|
|
InputPath = mediaSource.Path,
|
|
|
|
|
Protocol = mediaSource.Protocol,
|
|
|
|
|
MediaType = isAudio ? DlnaProfileType.Audio : DlnaProfileType.Video,
|
|
|
|
|
ExtractChapters = false,
|
2017-03-05 15:38:36 +00:00
|
|
|
|
AnalyzeDurationMs = AnalyzeDurationMs
|
2016-10-11 06:46:59 +00:00
|
|
|
|
|
|
|
|
|
}, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
_logger.Info("Live tv media info probe took {0} seconds", (DateTime.UtcNow - now).TotalSeconds.ToString(CultureInfo.InvariantCulture));
|
|
|
|
|
|
|
|
|
|
mediaSource.Bitrate = info.Bitrate;
|
|
|
|
|
mediaSource.Container = info.Container;
|
|
|
|
|
mediaSource.Formats = info.Formats;
|
|
|
|
|
mediaSource.MediaStreams = info.MediaStreams;
|
|
|
|
|
mediaSource.RunTimeTicks = info.RunTimeTicks;
|
|
|
|
|
mediaSource.Size = info.Size;
|
|
|
|
|
mediaSource.Timestamp = info.Timestamp;
|
|
|
|
|
mediaSource.Video3DFormat = info.Video3DFormat;
|
|
|
|
|
mediaSource.VideoType = info.VideoType;
|
|
|
|
|
|
|
|
|
|
mediaSource.DefaultSubtitleStreamIndex = null;
|
|
|
|
|
|
|
|
|
|
// Null this out so that it will be treated like a live stream
|
|
|
|
|
if (!originalRuntime.HasValue)
|
|
|
|
|
{
|
|
|
|
|
mediaSource.RunTimeTicks = null;
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-03 23:35:19 +00:00
|
|
|
|
var audioStream = mediaSource.MediaStreams.FirstOrDefault(i => i.Type == MediaBrowser.Model.Entities.MediaStreamType.Audio);
|
2016-10-11 06:46:59 +00:00
|
|
|
|
|
|
|
|
|
if (audioStream == null || audioStream.Index == -1)
|
|
|
|
|
{
|
|
|
|
|
mediaSource.DefaultAudioStreamIndex = null;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
mediaSource.DefaultAudioStreamIndex = audioStream.Index;
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-03 23:35:19 +00:00
|
|
|
|
var videoStream = mediaSource.MediaStreams.FirstOrDefault(i => i.Type == MediaBrowser.Model.Entities.MediaStreamType.Video);
|
2016-10-11 06:46:59 +00:00
|
|
|
|
if (videoStream != null)
|
|
|
|
|
{
|
|
|
|
|
if (!videoStream.BitRate.HasValue)
|
|
|
|
|
{
|
|
|
|
|
var width = videoStream.Width ?? 1920;
|
|
|
|
|
|
2017-04-19 16:57:06 +00:00
|
|
|
|
if (width >= 3000)
|
|
|
|
|
{
|
2017-04-21 03:47:17 +00:00
|
|
|
|
videoStream.BitRate = 30000000;
|
2017-04-19 16:57:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else if (width >= 1900)
|
2016-10-11 06:46:59 +00:00
|
|
|
|
{
|
2017-04-21 03:47:17 +00:00
|
|
|
|
videoStream.BitRate = 20000000;
|
2016-10-11 06:46:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-04-19 16:57:06 +00:00
|
|
|
|
else if (width >= 1200)
|
2016-10-11 06:46:59 +00:00
|
|
|
|
{
|
2017-04-21 03:47:17 +00:00
|
|
|
|
videoStream.BitRate = 8000000;
|
2016-10-11 06:46:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else if (width >= 700)
|
|
|
|
|
{
|
2017-04-21 03:47:17 +00:00
|
|
|
|
videoStream.BitRate = 2000000;
|
2016-10-11 06:46:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This is coming up false and preventing stream copy
|
|
|
|
|
videoStream.IsAVC = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Try to estimate this
|
2017-01-26 06:26:58 +00:00
|
|
|
|
mediaSource.InferTotalBitrate(true);
|
2017-03-05 15:38:36 +00:00
|
|
|
|
|
|
|
|
|
mediaSource.AnalyzeDurationMs = AnalyzeDurationMs;
|
2016-10-11 06:46:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|