< 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: 146
Coverable lines: 150
Total lines: 251
Line coverage: 2.6%
Branch coverage
0%
Covered branches: 0
Total branches: 32
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100 2/13/2026 - 12:11:21 AM Line coverage: 2.8% (4/139) Branch coverage: 0% (0/30) Total lines: 2335/8/2026 - 12:15:13 AM Line coverage: 2.7% (4/143) Branch coverage: 0% (0/32) Total lines: 2405/16/2026 - 12:15:55 AM Line coverage: 2.6% (4/150) Branch coverage: 0% (0/32) Total lines: 251 2/13/2026 - 12:11:21 AM Line coverage: 2.8% (4/139) Branch coverage: 0% (0/30) Total lines: 2335/8/2026 - 12:15:13 AM Line coverage: 2.7% (4/143) Branch coverage: 0% (0/32) Total lines: 2405/16/2026 - 12:15:55 AM Line coverage: 2.6% (4/150) Branch coverage: 0% (0/32) Total lines: 251

Coverage delta

Coverage delta 1 -1

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
SaveMediaStreams(...)100%210%
GetMediaStreams(...)100%210%
GetMediaStreamLanguages(...)100%210%
GetPathToSave(...)0%620%
RestorePath(...)0%620%
TranslateQuery(...)0%2040%
Map(...)0%420200%
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    /// <inheritdoc />
 59    public IReadOnlyList<string> GetMediaStreamLanguages(MediaStreamType mediaStreamType)
 60    {
 061        using var context = _dbProvider.CreateDbContext();
 062        return context.MediaStreamInfos
 063            .Where(e => e.StreamType == (MediaStreamTypeEntity)mediaStreamType)
 064            .Select(s => string.IsNullOrEmpty(s.Language) ? "und" : s.Language) // und = undetermined
 065            .Distinct()
 066            .ToArray();
 067    }
 68
 69    private string? GetPathToSave(string? path)
 70    {
 071        if (path is null)
 72        {
 073            return null;
 74        }
 75
 076        return _serverApplicationHost.ReverseVirtualPath(path);
 77    }
 78
 79    private string? RestorePath(string? path)
 80    {
 081        if (path is null)
 82        {
 083            return null;
 84        }
 85
 086        return _serverApplicationHost.ExpandVirtualPath(path);
 87    }
 88
 89    private IQueryable<MediaStreamInfo> TranslateQuery(IQueryable<MediaStreamInfo> query, MediaStreamQuery filter)
 90    {
 091        query = query.Where(e => e.ItemId.Equals(filter.ItemId));
 092        if (filter.Index.HasValue)
 93        {
 094            query = query.Where(e => e.StreamIndex == filter.Index);
 95        }
 96
 097        if (filter.Type.HasValue)
 98        {
 099            var typeValue = (MediaStreamTypeEntity)filter.Type.Value;
 0100            query = query.Where(e => e.StreamType == typeValue);
 101        }
 102
 0103        return query.OrderBy(e => e.StreamIndex);
 104    }
 105
 106    private MediaStream Map(MediaStreamInfo entity)
 107    {
 0108        var dto = new MediaStream();
 0109        dto.Index = entity.StreamIndex;
 0110        dto.Type = (MediaStreamType)entity.StreamType;
 111
 0112        dto.IsAVC = entity.IsAvc;
 0113        dto.Codec = entity.Codec;
 114
 0115        var language = entity.Language;
 116
 117        // Check if the language has multiple three letter ISO codes
 118        // if yes choose the first as that is the ISO 639-2/T code we're needing
 0119        if (language != null && _localization.TryGetISO6392TFromB(language, out string? isoT))
 120        {
 0121            language = isoT;
 122        }
 123
 0124        dto.Language = language;
 125
 0126        dto.ChannelLayout = entity.ChannelLayout;
 0127        dto.Profile = entity.Profile;
 0128        dto.AspectRatio = entity.AspectRatio;
 0129        dto.Path = RestorePath(entity.Path);
 0130        dto.IsInterlaced = entity.IsInterlaced.GetValueOrDefault();
 0131        dto.BitRate = entity.BitRate;
 0132        dto.Channels = entity.Channels;
 0133        dto.SampleRate = entity.SampleRate;
 0134        dto.IsDefault = entity.IsDefault;
 0135        dto.IsForced = entity.IsForced;
 0136        dto.IsExternal = entity.IsExternal;
 0137        dto.IsOriginal = entity.IsOriginal;
 0138        dto.Height = entity.Height;
 0139        dto.Width = entity.Width;
 0140        dto.AverageFrameRate = entity.AverageFrameRate;
 0141        dto.RealFrameRate = entity.RealFrameRate;
 0142        dto.Level = entity.Level;
 0143        dto.PixelFormat = entity.PixelFormat;
 0144        dto.BitDepth = entity.BitDepth;
 0145        dto.IsAnamorphic = entity.IsAnamorphic;
 0146        dto.RefFrames = entity.RefFrames;
 0147        dto.CodecTag = entity.CodecTag;
 0148        dto.Comment = entity.Comment;
 0149        dto.NalLengthSize = entity.NalLengthSize;
 0150        dto.Title = entity.Title;
 0151        dto.TimeBase = entity.TimeBase;
 0152        dto.CodecTimeBase = entity.CodecTimeBase;
 0153        dto.ColorPrimaries = entity.ColorPrimaries;
 0154        dto.ColorSpace = entity.ColorSpace;
 0155        dto.ColorTransfer = entity.ColorTransfer;
 0156        dto.DvVersionMajor = entity.DvVersionMajor;
 0157        dto.DvVersionMinor = entity.DvVersionMinor;
 0158        dto.DvProfile = entity.DvProfile;
 0159        dto.DvLevel = entity.DvLevel;
 0160        dto.RpuPresentFlag = entity.RpuPresentFlag;
 0161        dto.ElPresentFlag = entity.ElPresentFlag;
 0162        dto.BlPresentFlag = entity.BlPresentFlag;
 0163        dto.DvBlSignalCompatibilityId = entity.DvBlSignalCompatibilityId;
 0164        dto.IsHearingImpaired = entity.IsHearingImpaired.GetValueOrDefault();
 0165        dto.Rotation = entity.Rotation;
 0166        dto.Hdr10PlusPresentFlag = entity.Hdr10PlusPresentFlag;
 167
 0168        if (dto.Type is MediaStreamType.Audio or MediaStreamType.Subtitle)
 169        {
 0170            dto.LocalizedDefault = _localization.GetLocalizedString("Default");
 0171            dto.LocalizedExternal = _localization.GetLocalizedString("External");
 172
 0173            if (!string.IsNullOrEmpty(dto.Language))
 174            {
 0175                var culture = _localization.FindLanguageInfo(dto.Language);
 0176                dto.LocalizedLanguage = culture?.DisplayName;
 177            }
 178
 0179            if (dto.Type is MediaStreamType.Audio)
 180            {
 0181                dto.LocalizedOriginal = _localization.GetLocalizedString("Original");
 182            }
 183
 0184            if (dto.Type is MediaStreamType.Subtitle)
 185            {
 0186                dto.LocalizedUndefined = _localization.GetLocalizedString("Undefined");
 0187                dto.LocalizedForced = _localization.GetLocalizedString("Forced");
 0188                dto.LocalizedHearingImpaired = _localization.GetLocalizedString("HearingImpaired");
 189            }
 190        }
 191
 0192        return dto;
 193    }
 194
 195    private MediaStreamInfo Map(MediaStream dto, Guid itemId)
 196    {
 0197        var entity = new MediaStreamInfo
 0198        {
 0199            Item = null!,
 0200            ItemId = itemId,
 0201            StreamIndex = dto.Index,
 0202            StreamType = (MediaStreamTypeEntity)dto.Type,
 0203            IsAvc = dto.IsAVC,
 0204
 0205            Codec = dto.Codec,
 0206            Language = dto.Language,
 0207            ChannelLayout = dto.ChannelLayout,
 0208            Profile = dto.Profile,
 0209            AspectRatio = dto.AspectRatio,
 0210            Path = GetPathToSave(dto.Path) ?? dto.Path,
 0211            IsInterlaced = dto.IsInterlaced,
 0212            BitRate = dto.BitRate,
 0213            Channels = dto.Channels,
 0214            SampleRate = dto.SampleRate,
 0215            IsDefault = dto.IsDefault,
 0216            IsForced = dto.IsForced,
 0217            IsExternal = dto.IsExternal,
 0218            IsOriginal = dto.IsOriginal,
 0219            Height = dto.Height,
 0220            Width = dto.Width,
 0221            AverageFrameRate = dto.AverageFrameRate,
 0222            RealFrameRate = dto.RealFrameRate,
 0223            Level = dto.Level.HasValue ? (float)dto.Level : null,
 0224            PixelFormat = dto.PixelFormat,
 0225            BitDepth = dto.BitDepth,
 0226            IsAnamorphic = dto.IsAnamorphic,
 0227            RefFrames = dto.RefFrames,
 0228            CodecTag = dto.CodecTag,
 0229            Comment = dto.Comment,
 0230            NalLengthSize = dto.NalLengthSize,
 0231            Title = dto.Title,
 0232            TimeBase = dto.TimeBase,
 0233            CodecTimeBase = dto.CodecTimeBase,
 0234            ColorPrimaries = dto.ColorPrimaries,
 0235            ColorSpace = dto.ColorSpace,
 0236            ColorTransfer = dto.ColorTransfer,
 0237            DvVersionMajor = dto.DvVersionMajor,
 0238            DvVersionMinor = dto.DvVersionMinor,
 0239            DvProfile = dto.DvProfile,
 0240            DvLevel = dto.DvLevel,
 0241            RpuPresentFlag = dto.RpuPresentFlag,
 0242            ElPresentFlag = dto.ElPresentFlag,
 0243            BlPresentFlag = dto.BlPresentFlag,
 0244            DvBlSignalCompatibilityId = dto.DvBlSignalCompatibilityId,
 0245            IsHearingImpaired = dto.IsHearingImpaired,
 0246            Rotation = dto.Rotation,
 0247            Hdr10PlusPresentFlag = dto.Hdr10PlusPresentFlag,
 0248        };
 0249        return entity;
 250    }
 251}