From dbd7be091d802a788e520809fe55c9063a9cdaf4 Mon Sep 17 00:00:00 2001 From: Bond_009 Date: Mon, 14 Feb 2022 15:03:08 +0100 Subject: [PATCH] Fix MediaStreamSelector --- .../Library/MediaStreamSelector.cs | 7 ++++- .../Library/MediaStreamSelectorTests.cs | 30 +++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 tests/Jellyfin.Server.Implementations.Tests/Library/MediaStreamSelectorTests.cs diff --git a/Emby.Server.Implementations/Library/MediaStreamSelector.cs b/Emby.Server.Implementations/Library/MediaStreamSelector.cs index 13f0999b35..6ba9b29fa3 100644 --- a/Emby.Server.Implementations/Library/MediaStreamSelector.cs +++ b/Emby.Server.Implementations/Library/MediaStreamSelector.cs @@ -17,7 +17,12 @@ namespace Emby.Server.Implementations.Library if (preferDefaultTrack) { - return sortedStreams.FirstOrDefault(i => i.IsDefault)?.Index; + var defaultStream = streams.FirstOrDefault(i => i.IsDefault); + + if (defaultStream != null) + { + return defaultStream.Index; + } } return sortedStreams.FirstOrDefault()?.Index; diff --git a/tests/Jellyfin.Server.Implementations.Tests/Library/MediaStreamSelectorTests.cs b/tests/Jellyfin.Server.Implementations.Tests/Library/MediaStreamSelectorTests.cs new file mode 100644 index 0000000000..d59f2f4e51 --- /dev/null +++ b/tests/Jellyfin.Server.Implementations.Tests/Library/MediaStreamSelectorTests.cs @@ -0,0 +1,30 @@ +using System; +using Emby.Server.Implementations.Library; +using MediaBrowser.Model.Entities; +using Xunit; + +namespace Jellyfin.Server.Implementations.Tests.Library; + +public class MediaStreamSelectorTests +{ + [Theory] + [InlineData(true)] + [InlineData(false)] + public void GetDefaultAudioStreamIndex_EmptyStreams_Null(bool preferDefaultTrack) + { + Assert.Null(MediaStreamSelector.GetDefaultAudioStreamIndex(Array.Empty(), Array.Empty(), preferDefaultTrack)); + } + + [Theory] + [InlineData(true)] + [InlineData(false)] + public void GetDefaultAudioStreamIndex_WithoutDefault_NotNull(bool preferDefaultTrack) + { + var streams = new[] + { + new MediaStream() + }; + + Assert.NotNull(MediaStreamSelector.GetDefaultAudioStreamIndex(streams, Array.Empty(), preferDefaultTrack)); + } +}