From 3e5cb8e04e11d05b4af54e88686e829429305870 Mon Sep 17 00:00:00 2001 From: cvium Date: Sat, 25 Sep 2021 11:47:44 +0200 Subject: [PATCH] Add tests for ComputeEqualLengthSegments and fix bug --- .../Playlist/DynamicHlsPlaylistGenerator.cs | 11 +++++-- .../DynamicHlsPlaylistGeneratorTests.cs | 32 ++++++++++++++++++- 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/src/Jellyfin.MediaEncoding.Hls/Playlist/DynamicHlsPlaylistGenerator.cs b/src/Jellyfin.MediaEncoding.Hls/Playlist/DynamicHlsPlaylistGenerator.cs index 9cb52e678..5c6809a87 100644 --- a/src/Jellyfin.MediaEncoding.Hls/Playlist/DynamicHlsPlaylistGenerator.cs +++ b/src/Jellyfin.MediaEncoding.Hls/Playlist/DynamicHlsPlaylistGenerator.cs @@ -221,7 +221,14 @@ namespace Jellyfin.MediaEncoding.Hls.Playlist internal static double[] ComputeEqualLengthSegments(long desiredSegmentLengthMs, long totalRuntimeTicks) { - var segmentLengthTicks = TimeSpan.FromMilliseconds(desiredSegmentLengthMs).Ticks; + if (desiredSegmentLengthMs == 0 || totalRuntimeTicks == 0) + { + throw new InvalidOperationException($"Invalid segment length ({desiredSegmentLengthMs}) or runtime ticks ({totalRuntimeTicks})"); + } + + var desiredSegmentLength = TimeSpan.FromMilliseconds(desiredSegmentLengthMs); + + var segmentLengthTicks = desiredSegmentLength.Ticks; var wholeSegments = totalRuntimeTicks / segmentLengthTicks; var remainingTicks = totalRuntimeTicks % segmentLengthTicks; @@ -229,7 +236,7 @@ namespace Jellyfin.MediaEncoding.Hls.Playlist var segments = new double[segmentsLen]; for (int i = 0; i < wholeSegments; i++) { - segments[i] = desiredSegmentLengthMs; + segments[i] = desiredSegmentLength.TotalSeconds; } if (remainingTicks != 0) diff --git a/tests/Jellyfin.MediaEncoding.Hls.Tests/Playlist/DynamicHlsPlaylistGeneratorTests.cs b/tests/Jellyfin.MediaEncoding.Hls.Tests/Playlist/DynamicHlsPlaylistGeneratorTests.cs index c55b7e304..4b2fce171 100644 --- a/tests/Jellyfin.MediaEncoding.Hls.Tests/Playlist/DynamicHlsPlaylistGeneratorTests.cs +++ b/tests/Jellyfin.MediaEncoding.Hls.Tests/Playlist/DynamicHlsPlaylistGeneratorTests.cs @@ -1,10 +1,26 @@ -using Jellyfin.MediaEncoding.Hls.Playlist; +using System; +using Jellyfin.MediaEncoding.Hls.Playlist; using Xunit; namespace Jellyfin.MediaEncoding.Hls.Tests.Playlist { public class DynamicHlsPlaylistGeneratorTests { + [Theory] + [MemberData(nameof(ComputeEqualLengthSegments_Valid_Success_Data))] + public void ComputeEqualLengthSegments_Valid_Success(long desiredSegmentLengthMs, long totalRuntimeTicks, double[] segments) + { + Assert.Equal(segments, DynamicHlsPlaylistGenerator.ComputeEqualLengthSegments(desiredSegmentLengthMs, totalRuntimeTicks)); + } + + [Theory] + [InlineData(0, 1000000)] + [InlineData(1000, 0)] + public void ComputeEqualLengthSegments_Invalid_ThrowsInvalidOperationException(long desiredSegmentLengthMs, long totalRuntimeTicks) + { + Assert.Throws(() => DynamicHlsPlaylistGenerator.ComputeEqualLengthSegments(desiredSegmentLengthMs, totalRuntimeTicks)); + } + [Theory] [InlineData("testfile.mkv", new string[0], false)] [InlineData("testfile.flv", new[] { "mp4", "mkv", "ts" }, false)] @@ -21,5 +37,19 @@ namespace Jellyfin.MediaEncoding.Hls.Tests.Playlist { Assert.False(DynamicHlsPlaylistGenerator.IsExtractionAllowedForFile(filePath, allowedExtensions)); } + + private static TheoryData ComputeEqualLengthSegments_Valid_Success_Data() + { + var data = new TheoryData + { + { 6000, TimeSpan.FromMilliseconds(13000).Ticks, new[] { 6.0, 6.0, 1.0 } }, + { 3000, TimeSpan.FromMilliseconds(15000).Ticks, new[] { 3.0, 3.0, 3.0, 3.0, 3.0 } }, + { 6000, TimeSpan.FromMilliseconds(25000).Ticks, new[] { 6.0, 6.0, 6.0, 6.0, 1.0 } }, + { 6000, TimeSpan.FromMilliseconds(20123).Ticks, new[] { 6.0, 6.0, 6.0, 2.123 } }, + { 6000, TimeSpan.FromMilliseconds(1234).Ticks, new[] { 1.234 } } + }; + + return data; + } } }