DynamicHlsPlaylistGeneratorTests.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using System;
  2. using Jellyfin.MediaEncoding.Hls.Playlist;
  3. using Jellyfin.MediaEncoding.Keyframes;
  4. using Xunit;
  5. namespace Jellyfin.MediaEncoding.Hls.Tests.Playlist
  6. {
  7. public class DynamicHlsPlaylistGeneratorTests
  8. {
  9. [Theory]
  10. [MemberData(nameof(ComputeSegments_Valid_Success_Data))]
  11. public void ComputeSegments_Valid_Success(KeyframeData keyframeData, int desiredSegmentLengthMs, double[] segments)
  12. {
  13. Assert.Equal(segments, DynamicHlsPlaylistGenerator.ComputeSegments(keyframeData, desiredSegmentLengthMs));
  14. }
  15. [Fact]
  16. public void ComputeSegments_InvalidDuration_ThrowsArgumentException()
  17. {
  18. var keyframeData = new KeyframeData(0, new[] { MsToTicks(10000) });
  19. Assert.Throws<ArgumentException>(() => DynamicHlsPlaylistGenerator.ComputeSegments(keyframeData, 6000));
  20. }
  21. [Theory]
  22. [MemberData(nameof(ComputeEqualLengthSegments_Valid_Success_Data))]
  23. public void ComputeEqualLengthSegments_Valid_Success(int desiredSegmentLengthMs, long totalRuntimeTicks, double[] segments)
  24. {
  25. Assert.Equal(segments, DynamicHlsPlaylistGenerator.ComputeEqualLengthSegments(desiredSegmentLengthMs, totalRuntimeTicks));
  26. }
  27. [Theory]
  28. [InlineData(0, 1000000)]
  29. [InlineData(1000, 0)]
  30. public void ComputeEqualLengthSegments_Invalid_ThrowsInvalidOperationException(int desiredSegmentLengthMs, long totalRuntimeTicks)
  31. {
  32. Assert.Throws<InvalidOperationException>(() => DynamicHlsPlaylistGenerator.ComputeEqualLengthSegments(desiredSegmentLengthMs, totalRuntimeTicks));
  33. }
  34. [Theory]
  35. [InlineData("testfile.mkv", new string[0], false)]
  36. [InlineData("testfile.flv", new[] { ".mp4", ".mkv", ".ts" }, false)]
  37. [InlineData("testfile.flv", new[] { ".mp4", ".mkv", ".ts", ".flv" }, true)]
  38. [InlineData("/some/arbitrarily/long/path/testfile.mkv", new[] { "mkv" }, true)]
  39. public void IsExtractionAllowedForFile_Valid_Success(string filePath, string[] allowedExtensions, bool isAllowed)
  40. {
  41. Assert.Equal(isAllowed, DynamicHlsPlaylistGenerator.IsExtractionAllowedForFile(filePath, allowedExtensions));
  42. }
  43. [Theory]
  44. [InlineData("testfile", new[] { ".mp4" })]
  45. public void IsExtractionAllowedForFile_Invalid_ReturnsFalse(string filePath, string[] allowedExtensions)
  46. {
  47. Assert.False(DynamicHlsPlaylistGenerator.IsExtractionAllowedForFile(filePath, allowedExtensions));
  48. }
  49. private static TheoryData<int, long, double[]> ComputeEqualLengthSegments_Valid_Success_Data()
  50. {
  51. var data = new TheoryData<int, long, double[]>
  52. {
  53. { 6000, MsToTicks(13000), new[] { 6.0, 6.0, 1.0 } },
  54. { 3000, MsToTicks(15000), new[] { 3.0, 3.0, 3.0, 3.0, 3.0 } },
  55. { 6000, MsToTicks(25000), new[] { 6.0, 6.0, 6.0, 6.0, 1.0 } },
  56. { 6000, MsToTicks(20123), new[] { 6.0, 6.0, 6.0, 2.123 } },
  57. { 6000, MsToTicks(1234), new[] { 1.234 } }
  58. };
  59. return data;
  60. }
  61. private static TheoryData<KeyframeData, int, double[]> ComputeSegments_Valid_Success_Data()
  62. {
  63. var data = new TheoryData<KeyframeData, int, double[]>
  64. {
  65. {
  66. new KeyframeData(MsToTicks(35000), new[] { 0, MsToTicks(10427), MsToTicks(20854), MsToTicks(31240) }),
  67. 6000,
  68. new[] { 10.427, 10.427, 10.386, 3.760 }
  69. },
  70. {
  71. new KeyframeData(MsToTicks(10000), new[] { 0, MsToTicks(1000), MsToTicks(2000), MsToTicks(3000), MsToTicks(4000), MsToTicks(5000) }),
  72. 2000,
  73. new[] { 2.0, 2.0, 6.0 }
  74. },
  75. {
  76. new KeyframeData(MsToTicks(10000), new[] { 0L }),
  77. 6000,
  78. new[] { 10.0 }
  79. },
  80. {
  81. new KeyframeData(MsToTicks(10000), Array.Empty<long>()),
  82. 6000,
  83. new[] { 10.0 }
  84. }
  85. };
  86. return data;
  87. }
  88. private static long MsToTicks(int value) => TimeSpan.FromMilliseconds(value).Ticks;
  89. }
  90. }