jellyfin-server/Emby.Server.Implementations/Library/LiveStreamHelper.cs

183 lines
6.7 KiB
C#
Raw Normal View History

#nullable disable
2019-11-01 17:38:54 +00:00
#pragma warning disable CS1591
using System;
using System.Collections.Generic;
2018-09-12 17:26:21 +00:00
using System.Globalization;
using System.IO;
2018-09-12 17:26:21 +00:00
using System.Linq;
using System.Text.Json;
2018-09-12 17:26:21 +00:00
using System.Threading;
using System.Threading.Tasks;
2021-09-25 18:32:53 +00:00
using Jellyfin.Extensions.Json;
using MediaBrowser.Common.Configuration;
using MediaBrowser.Common.Extensions;
2018-09-12 17:26:21 +00:00
using MediaBrowser.Controller.MediaEncoding;
using MediaBrowser.Model.Dlna;
using MediaBrowser.Model.Dto;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.IO;
2018-09-12 17:26:21 +00:00
using MediaBrowser.Model.MediaInfo;
using Microsoft.Extensions.Logging;
2018-09-12 17:26:21 +00:00
namespace Emby.Server.Implementations.Library
{
public class LiveStreamHelper
{
private readonly IMediaEncoder _mediaEncoder;
private readonly ILogger _logger;
2020-07-20 09:01:37 +00:00
private readonly IApplicationPaths _appPaths;
2021-03-09 04:57:38 +00:00
private readonly JsonSerializerOptions _jsonOptions = JsonDefaults.Options;
2018-09-12 17:26:21 +00:00
public LiveStreamHelper(IMediaEncoder mediaEncoder, ILogger logger, IApplicationPaths appPaths)
2018-09-12 17:26:21 +00:00
{
_mediaEncoder = mediaEncoder;
_logger = logger;
_appPaths = appPaths;
}
public async Task AddMediaInfoWithProbe(MediaSourceInfo mediaSource, bool isAudio, string cacheKey, bool addProbeDelay, CancellationToken cancellationToken)
{
var originalRuntime = mediaSource.RunTimeTicks;
var now = DateTime.UtcNow;
MediaInfo mediaInfo = null;
var cacheFilePath = string.IsNullOrEmpty(cacheKey) ? null : Path.Combine(_appPaths.CachePath, "mediainfo", cacheKey.GetMD5().ToString("N", CultureInfo.InvariantCulture) + ".json");
2018-09-12 17:26:21 +00:00
if (!string.IsNullOrEmpty(cacheKey))
{
try
{
await using FileStream jsonStream = AsyncFile.OpenRead(cacheFilePath);
mediaInfo = await JsonSerializer.DeserializeAsync<MediaInfo>(jsonStream, _jsonOptions, cancellationToken).ConfigureAwait(false);
2018-09-12 17:26:21 +00:00
2020-06-14 09:11:11 +00:00
// _logger.LogDebug("Found cached media info");
2018-09-12 17:26:21 +00:00
}
catch
{
}
}
if (mediaInfo == null)
{
if (addProbeDelay)
{
var delayMs = mediaSource.AnalyzeDurationMs ?? 0;
delayMs = Math.Max(3000, delayMs);
2021-12-15 17:25:36 +00:00
_logger.LogInformation("Waiting {0}ms before probing the live stream", delayMs);
await Task.Delay(delayMs, cancellationToken).ConfigureAwait(false);
2018-09-12 17:26:21 +00:00
}
mediaSource.AnalyzeDurationMs = 3000;
2020-07-20 09:01:37 +00:00
mediaInfo = await _mediaEncoder.GetMediaInfo(
new MediaInfoRequest
{
MediaSource = mediaSource,
MediaType = isAudio ? DlnaProfileType.Audio : DlnaProfileType.Video,
ExtractChapters = false
},
cancellationToken).ConfigureAwait(false);
2018-09-12 17:26:21 +00:00
if (cacheFilePath != null)
{
Directory.CreateDirectory(Path.GetDirectoryName(cacheFilePath));
await using FileStream createStream = AsyncFile.OpenWrite(cacheFilePath);
await JsonSerializer.SerializeAsync(createStream, mediaInfo, _jsonOptions, cancellationToken).ConfigureAwait(false);
2018-09-12 17:26:21 +00:00
2020-06-14 09:11:11 +00:00
// _logger.LogDebug("Saved media info to {0}", cacheFilePath);
2018-09-12 17:26:21 +00:00
}
}
var mediaStreams = mediaInfo.MediaStreams;
if (!string.IsNullOrEmpty(cacheKey))
{
var newList = new List<MediaStream>();
newList.AddRange(mediaStreams.Where(i => i.Type == MediaStreamType.Video).Take(1));
newList.AddRange(mediaStreams.Where(i => i.Type == MediaStreamType.Audio).Take(1));
foreach (var stream in newList)
{
stream.Index = -1;
stream.Language = null;
}
mediaStreams = newList;
}
_logger.LogInformation("Live tv media info probe took {0} seconds", (DateTime.UtcNow - now).TotalSeconds.ToString(CultureInfo.InvariantCulture));
2018-09-12 17:26:21 +00:00
mediaSource.Bitrate = mediaInfo.Bitrate;
mediaSource.Container = mediaInfo.Container;
mediaSource.Formats = mediaInfo.Formats;
mediaSource.MediaStreams = mediaStreams;
mediaSource.RunTimeTicks = mediaInfo.RunTimeTicks;
mediaSource.Size = mediaInfo.Size;
mediaSource.Timestamp = mediaInfo.Timestamp;
mediaSource.Video3DFormat = mediaInfo.Video3DFormat;
mediaSource.VideoType = mediaInfo.VideoType;
mediaSource.DefaultSubtitleStreamIndex = null;
// Null this out so that it will be treated like a live stream
if (!originalRuntime.HasValue)
{
mediaSource.RunTimeTicks = null;
}
2020-07-20 09:01:37 +00:00
var audioStream = mediaStreams.FirstOrDefault(i => i.Type == MediaStreamType.Audio);
2018-09-12 17:26:21 +00:00
if (audioStream == null || audioStream.Index == -1)
{
mediaSource.DefaultAudioStreamIndex = null;
}
else
{
mediaSource.DefaultAudioStreamIndex = audioStream.Index;
}
2020-07-20 09:01:37 +00:00
var videoStream = mediaStreams.FirstOrDefault(i => i.Type == MediaStreamType.Video);
2018-09-12 17:26:21 +00:00
if (videoStream != null)
{
if (!videoStream.BitRate.HasValue)
{
var width = videoStream.Width ?? 1920;
if (width >= 3000)
{
videoStream.BitRate = 30000000;
}
else if (width >= 1900)
{
videoStream.BitRate = 20000000;
}
else if (width >= 1200)
{
videoStream.BitRate = 8000000;
}
else if (width >= 700)
{
videoStream.BitRate = 2000000;
}
}
// This is coming up false and preventing stream copy
videoStream.IsAVC = null;
}
mediaSource.AnalyzeDurationMs = 3000;
// Try to estimate this
mediaSource.InferTotalBitrate(true);
}
public Task AddMediaInfoWithProbe(MediaSourceInfo mediaSource, bool isAudio, bool addProbeDelay, CancellationToken cancellationToken)
{
return AddMediaInfoWithProbe(mediaSource, isAudio, null, addProbeDelay, cancellationToken);
}
}
}