< Summary - Jellyfin

Information
Class: Emby.Server.Implementations.Library.LiveStreamHelper
Assembly: Emby.Server.Implementations
File(s): /srv/git/jellyfin/Emby.Server.Implementations/Library/LiveStreamHelper.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 6
Coverable lines: 6
Total lines: 191
Line coverage: 0%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
AddMediaInfoWithProbe(...)100%210%

File(s)

/srv/git/jellyfin/Emby.Server.Implementations/Library/LiveStreamHelper.cs

#LineLine coverage
 1#pragma warning disable CS1591
 2
 3using System;
 4using System.Collections.Generic;
 5using System.Globalization;
 6using System.IO;
 7using System.Linq;
 8using System.Text.Json;
 9using System.Threading;
 10using System.Threading.Tasks;
 11using Jellyfin.Extensions.Json;
 12using MediaBrowser.Common.Configuration;
 13using MediaBrowser.Common.Extensions;
 14using MediaBrowser.Controller.MediaEncoding;
 15using MediaBrowser.Model.Dlna;
 16using MediaBrowser.Model.Dto;
 17using MediaBrowser.Model.Entities;
 18using MediaBrowser.Model.IO;
 19using MediaBrowser.Model.MediaInfo;
 20using Microsoft.Extensions.Logging;
 21
 22namespace Emby.Server.Implementations.Library
 23{
 24    public class LiveStreamHelper
 25    {
 26        private readonly IMediaEncoder _mediaEncoder;
 27        private readonly ILogger _logger;
 28        private readonly IApplicationPaths _appPaths;
 029        private readonly JsonSerializerOptions _jsonOptions = JsonDefaults.Options;
 30
 31        public LiveStreamHelper(IMediaEncoder mediaEncoder, ILogger logger, IApplicationPaths appPaths)
 32        {
 033            _mediaEncoder = mediaEncoder;
 034            _logger = logger;
 035            _appPaths = appPaths;
 036        }
 37
 38        public async Task AddMediaInfoWithProbe(MediaSourceInfo mediaSource, bool isAudio, string? cacheKey, bool addPro
 39        {
 40            var originalRuntime = mediaSource.RunTimeTicks;
 41
 42            var now = DateTime.UtcNow;
 43
 44            MediaInfo? mediaInfo = null;
 45            var cacheFilePath = string.IsNullOrEmpty(cacheKey) ? null : Path.Combine(_appPaths.CachePath, "mediainfo", c
 46
 47            if (cacheFilePath is not null)
 48            {
 49                try
 50                {
 51                    FileStream jsonStream = AsyncFile.OpenRead(cacheFilePath);
 52
 53                    await using (jsonStream.ConfigureAwait(false))
 54                    {
 55                        mediaInfo = await JsonSerializer.DeserializeAsync<MediaInfo>(jsonStream, _jsonOptions, cancellat
 56                        // _logger.LogDebug("Found cached media info");
 57                    }
 58                }
 59                catch (IOException ex)
 60                {
 61                    _logger.LogDebug(ex, "Could not open cached media info");
 62                }
 63                catch (Exception ex)
 64                {
 65                    _logger.LogError(ex, "Error opening cached media info");
 66                }
 67            }
 68
 69            if (mediaInfo is null)
 70            {
 71                if (addProbeDelay)
 72                {
 73                    var delayMs = mediaSource.AnalyzeDurationMs ?? 0;
 74                    delayMs = Math.Max(3000, delayMs);
 75                    _logger.LogInformation("Waiting {0}ms before probing the live stream", delayMs);
 76                    await Task.Delay(delayMs, cancellationToken).ConfigureAwait(false);
 77                }
 78
 79                mediaSource.AnalyzeDurationMs = 3000;
 80
 81                mediaInfo = await _mediaEncoder.GetMediaInfo(
 82                    new MediaInfoRequest
 83                    {
 84                        MediaSource = mediaSource,
 85                        MediaType = isAudio ? DlnaProfileType.Audio : DlnaProfileType.Video,
 86                        ExtractChapters = false
 87                    },
 88                    cancellationToken).ConfigureAwait(false);
 89
 90                if (cacheFilePath is not null)
 91                {
 92                    Directory.CreateDirectory(Path.GetDirectoryName(cacheFilePath) ?? throw new InvalidOperationExceptio
 93                    FileStream createStream = AsyncFile.OpenWrite(cacheFilePath);
 94                    await using (createStream.ConfigureAwait(false))
 95                    {
 96                        await JsonSerializer.SerializeAsync(createStream, mediaInfo, _jsonOptions, cancellationToken).Co
 97                    }
 98
 99                    _logger.LogDebug("Saved media info to {0}", cacheFilePath);
 100                }
 101            }
 102
 103            var mediaStreams = mediaInfo.MediaStreams;
 104
 105            if (!string.IsNullOrEmpty(cacheKey))
 106            {
 107                var newList = new List<MediaStream>();
 108                newList.AddRange(mediaStreams.Where(i => i.Type == MediaStreamType.Video).Take(1));
 109                newList.AddRange(mediaStreams.Where(i => i.Type == MediaStreamType.Audio).Take(1));
 110
 111                foreach (var stream in newList)
 112                {
 113                    stream.Index = -1;
 114                    stream.Language = null;
 115                }
 116
 117                mediaStreams = newList;
 118            }
 119
 120            _logger.LogInformation("Live tv media info probe took {0} seconds", (DateTime.UtcNow - now).TotalSeconds.ToS
 121
 122            mediaSource.Bitrate = mediaInfo.Bitrate;
 123            mediaSource.Container = mediaInfo.Container;
 124            mediaSource.Formats = mediaInfo.Formats;
 125            mediaSource.MediaStreams = mediaStreams;
 126            mediaSource.RunTimeTicks = mediaInfo.RunTimeTicks;
 127            mediaSource.Size = mediaInfo.Size;
 128            mediaSource.Timestamp = mediaInfo.Timestamp;
 129            mediaSource.Video3DFormat = mediaInfo.Video3DFormat;
 130            mediaSource.VideoType = mediaInfo.VideoType;
 131
 132            mediaSource.DefaultSubtitleStreamIndex = null;
 133
 134            // Null this out so that it will be treated like a live stream
 135            if (!originalRuntime.HasValue)
 136            {
 137                mediaSource.RunTimeTicks = null;
 138            }
 139
 140            var audioStream = mediaStreams.FirstOrDefault(i => i.Type == MediaStreamType.Audio);
 141
 142            if (audioStream is null || audioStream.Index == -1)
 143            {
 144                mediaSource.DefaultAudioStreamIndex = null;
 145            }
 146            else
 147            {
 148                mediaSource.DefaultAudioStreamIndex = audioStream.Index;
 149            }
 150
 151            var videoStream = mediaStreams.FirstOrDefault(i => i.Type == MediaStreamType.Video);
 152            if (videoStream is not null)
 153            {
 154                if (!videoStream.BitRate.HasValue)
 155                {
 156                    var width = videoStream.Width ?? 1920;
 157
 158                    if (width >= 3000)
 159                    {
 160                        videoStream.BitRate = 30000000;
 161                    }
 162                    else if (width >= 1900)
 163                    {
 164                        videoStream.BitRate = 20000000;
 165                    }
 166                    else if (width >= 1200)
 167                    {
 168                        videoStream.BitRate = 8000000;
 169                    }
 170                    else if (width >= 700)
 171                    {
 172                        videoStream.BitRate = 2000000;
 173                    }
 174                }
 175
 176                // This is coming up false and preventing stream copy
 177                videoStream.IsAVC = null;
 178            }
 179
 180            mediaSource.AnalyzeDurationMs = 3000;
 181
 182            // Try to estimate this
 183            mediaSource.InferTotalBitrate(true);
 184        }
 185
 186        public Task AddMediaInfoWithProbe(MediaSourceInfo mediaSource, bool isAudio, bool addProbeDelay, CancellationTok
 187        {
 0188            return AddMediaInfoWithProbe(mediaSource, isAudio, null, addProbeDelay, cancellationToken);
 189        }
 190    }
 191}