< Summary - Jellyfin

Information
Class: Jellyfin.Server.Implementations.Item.MediaStreamRepository
Assembly: Jellyfin.Server.Implementations
File(s): /srv/git/jellyfin/Jellyfin.Server.Implementations/Item/MediaStreamRepository.cs
Line coverage
2%
Covered lines: 4
Uncovered lines: 132
Coverable lines: 136
Total lines: 227
Line coverage: 2.9%
Branch coverage
0%
Covered branches: 0
Total branches: 26
Branch coverage: 0%
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%11100%
SaveMediaStreams(...)100%210%
GetMediaStreams(...)100%210%
GetPathToSave(...)0%620%
RestorePath(...)0%620%
TranslateQuery(...)0%2040%
Map(...)0%210140%
Map(...)0%2040%

File(s)

/srv/git/jellyfin/Jellyfin.Server.Implementations/Item/MediaStreamRepository.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Collections.Immutable;
 4using System.Linq;
 5using System.Threading;
 6using Jellyfin.Database.Implementations;
 7using Jellyfin.Database.Implementations.Entities;
 8using MediaBrowser.Controller;
 9using MediaBrowser.Controller.Persistence;
 10using MediaBrowser.Model.Entities;
 11using MediaBrowser.Model.Globalization;
 12using Microsoft.EntityFrameworkCore;
 13
 14namespace Jellyfin.Server.Implementations.Item;
 15
 16/// <summary>
 17/// Repository for obtaining MediaStreams.
 18/// </summary>
 19public class MediaStreamRepository : IMediaStreamRepository
 20{
 21    private readonly IDbContextFactory<JellyfinDbContext> _dbProvider;
 22    private readonly IServerApplicationHost _serverApplicationHost;
 23    private readonly ILocalizationManager _localization;
 24
 25    /// <summary>
 26    /// Initializes a new instance of the <see cref="MediaStreamRepository"/> class.
 27    /// </summary>
 28    /// <param name="dbProvider">The EFCore db factory.</param>
 29    /// <param name="serverApplicationHost">The Application host.</param>
 30    /// <param name="localization">The Localisation Provider.</param>
 31    public MediaStreamRepository(IDbContextFactory<JellyfinDbContext> dbProvider, IServerApplicationHost serverApplicati
 32    {
 2133        _dbProvider = dbProvider;
 2134        _serverApplicationHost = serverApplicationHost;
 2135        _localization = localization;
 2136    }
 37
 38    /// <inheritdoc />
 39    public void SaveMediaStreams(Guid id, IReadOnlyList<MediaStream> streams, CancellationToken cancellationToken)
 40    {
 041        using var context = _dbProvider.CreateDbContext();
 042        using var transaction = context.Database.BeginTransaction();
 43
 044        context.MediaStreamInfos.Where(e => e.ItemId.Equals(id)).ExecuteDelete();
 045        context.MediaStreamInfos.AddRange(streams.Select(f => Map(f, id)));
 046        context.SaveChanges();
 47
 048        transaction.Commit();
 049    }
 50
 51    /// <inheritdoc />
 52    public IReadOnlyList<MediaStream> GetMediaStreams(MediaStreamQuery filter)
 53    {
 054        using var context = _dbProvider.CreateDbContext();
 055        return TranslateQuery(context.MediaStreamInfos.AsNoTracking(), filter).AsEnumerable().Select(Map).ToArray();
 056    }
 57
 58    private string? GetPathToSave(string? path)
 59    {
 060        if (path is null)
 61        {
 062            return null;
 63        }
 64
 065        return _serverApplicationHost.ReverseVirtualPath(path);
 66    }
 67
 68    private string? RestorePath(string? path)
 69    {
 070        if (path is null)
 71        {
 072            return null;
 73        }
 74
 075        return _serverApplicationHost.ExpandVirtualPath(path);
 76    }
 77
 78    private IQueryable<MediaStreamInfo> TranslateQuery(IQueryable<MediaStreamInfo> query, MediaStreamQuery filter)
 79    {
 080        query = query.Where(e => e.ItemId.Equals(filter.ItemId));
 081        if (filter.Index.HasValue)
 82        {
 083            query = query.Where(e => e.StreamIndex == filter.Index);
 84        }
 85
 086        if (filter.Type.HasValue)
 87        {
 088            var typeValue = (MediaStreamTypeEntity)filter.Type.Value;
 089            query = query.Where(e => e.StreamType == typeValue);
 90        }
 91
 092        return query.OrderBy(e => e.StreamIndex);
 93    }
 94
 95    private MediaStream Map(MediaStreamInfo entity)
 96    {
 097        var dto = new MediaStream();
 098        dto.Index = entity.StreamIndex;
 099        dto.Type = (MediaStreamType)entity.StreamType;
 100
 0101        dto.IsAVC = entity.IsAvc;
 0102        dto.Codec = entity.Codec;
 103
 0104        var language = entity.Language;
 105
 106        // Check if the language has multiple three letter ISO codes
 107        // if yes choose the first as that is the ISO 639-2/T code we're needing
 0108        if (language != null && _localization.TryGetISO6392TFromB(language, out string? isoT))
 109        {
 0110            language = isoT;
 111        }
 112
 0113        dto.Language = language;
 114
 0115        dto.ChannelLayout = entity.ChannelLayout;
 0116        dto.Profile = entity.Profile;
 0117        dto.AspectRatio = entity.AspectRatio;
 0118        dto.Path = RestorePath(entity.Path);
 0119        dto.IsInterlaced = entity.IsInterlaced.GetValueOrDefault();
 0120        dto.BitRate = entity.BitRate;
 0121        dto.Channels = entity.Channels;
 0122        dto.SampleRate = entity.SampleRate;
 0123        dto.IsDefault = entity.IsDefault;
 0124        dto.IsForced = entity.IsForced;
 0125        dto.IsExternal = entity.IsExternal;
 0126        dto.Height = entity.Height;
 0127        dto.Width = entity.Width;
 0128        dto.AverageFrameRate = entity.AverageFrameRate;
 0129        dto.RealFrameRate = entity.RealFrameRate;
 0130        dto.Level = entity.Level;
 0131        dto.PixelFormat = entity.PixelFormat;
 0132        dto.BitDepth = entity.BitDepth;
 0133        dto.IsAnamorphic = entity.IsAnamorphic;
 0134        dto.RefFrames = entity.RefFrames;
 0135        dto.CodecTag = entity.CodecTag;
 0136        dto.Comment = entity.Comment;
 0137        dto.NalLengthSize = entity.NalLengthSize;
 0138        dto.Title = entity.Title;
 0139        dto.TimeBase = entity.TimeBase;
 0140        dto.CodecTimeBase = entity.CodecTimeBase;
 0141        dto.ColorPrimaries = entity.ColorPrimaries;
 0142        dto.ColorSpace = entity.ColorSpace;
 0143        dto.ColorTransfer = entity.ColorTransfer;
 0144        dto.DvVersionMajor = entity.DvVersionMajor;
 0145        dto.DvVersionMinor = entity.DvVersionMinor;
 0146        dto.DvProfile = entity.DvProfile;
 0147        dto.DvLevel = entity.DvLevel;
 0148        dto.RpuPresentFlag = entity.RpuPresentFlag;
 0149        dto.ElPresentFlag = entity.ElPresentFlag;
 0150        dto.BlPresentFlag = entity.BlPresentFlag;
 0151        dto.DvBlSignalCompatibilityId = entity.DvBlSignalCompatibilityId;
 0152        dto.IsHearingImpaired = entity.IsHearingImpaired.GetValueOrDefault();
 0153        dto.Rotation = entity.Rotation;
 0154        dto.Hdr10PlusPresentFlag = entity.Hdr10PlusPresentFlag;
 155
 0156        if (dto.Type is MediaStreamType.Audio or MediaStreamType.Subtitle)
 157        {
 0158            dto.LocalizedDefault = _localization.GetLocalizedString("Default");
 0159            dto.LocalizedExternal = _localization.GetLocalizedString("External");
 160
 0161            if (dto.Type is MediaStreamType.Subtitle)
 162            {
 0163                dto.LocalizedUndefined = _localization.GetLocalizedString("Undefined");
 0164                dto.LocalizedForced = _localization.GetLocalizedString("Forced");
 0165                dto.LocalizedHearingImpaired = _localization.GetLocalizedString("HearingImpaired");
 166            }
 167        }
 168
 0169        return dto;
 170    }
 171
 172    private MediaStreamInfo Map(MediaStream dto, Guid itemId)
 173    {
 0174        var entity = new MediaStreamInfo
 0175        {
 0176            Item = null!,
 0177            ItemId = itemId,
 0178            StreamIndex = dto.Index,
 0179            StreamType = (MediaStreamTypeEntity)dto.Type,
 0180            IsAvc = dto.IsAVC,
 0181
 0182            Codec = dto.Codec,
 0183            Language = dto.Language,
 0184            ChannelLayout = dto.ChannelLayout,
 0185            Profile = dto.Profile,
 0186            AspectRatio = dto.AspectRatio,
 0187            Path = GetPathToSave(dto.Path) ?? dto.Path,
 0188            IsInterlaced = dto.IsInterlaced,
 0189            BitRate = dto.BitRate,
 0190            Channels = dto.Channels,
 0191            SampleRate = dto.SampleRate,
 0192            IsDefault = dto.IsDefault,
 0193            IsForced = dto.IsForced,
 0194            IsExternal = dto.IsExternal,
 0195            Height = dto.Height,
 0196            Width = dto.Width,
 0197            AverageFrameRate = dto.AverageFrameRate,
 0198            RealFrameRate = dto.RealFrameRate,
 0199            Level = dto.Level.HasValue ? (float)dto.Level : null,
 0200            PixelFormat = dto.PixelFormat,
 0201            BitDepth = dto.BitDepth,
 0202            IsAnamorphic = dto.IsAnamorphic,
 0203            RefFrames = dto.RefFrames,
 0204            CodecTag = dto.CodecTag,
 0205            Comment = dto.Comment,
 0206            NalLengthSize = dto.NalLengthSize,
 0207            Title = dto.Title,
 0208            TimeBase = dto.TimeBase,
 0209            CodecTimeBase = dto.CodecTimeBase,
 0210            ColorPrimaries = dto.ColorPrimaries,
 0211            ColorSpace = dto.ColorSpace,
 0212            ColorTransfer = dto.ColorTransfer,
 0213            DvVersionMajor = dto.DvVersionMajor,
 0214            DvVersionMinor = dto.DvVersionMinor,
 0215            DvProfile = dto.DvProfile,
 0216            DvLevel = dto.DvLevel,
 0217            RpuPresentFlag = dto.RpuPresentFlag,
 0218            ElPresentFlag = dto.ElPresentFlag,
 0219            BlPresentFlag = dto.BlPresentFlag,
 0220            DvBlSignalCompatibilityId = dto.DvBlSignalCompatibilityId,
 0221            IsHearingImpaired = dto.IsHearingImpaired,
 0222            Rotation = dto.Rotation,
 0223            Hdr10PlusPresentFlag = dto.Hdr10PlusPresentFlag,
 0224        };
 0225        return entity;
 226    }
 227}