Browse Source

fix: continue serving old segments when unable to transcode in realtime (#2933)

pull/2934/head
N73311 1 month ago committed by GitHub
parent
commit
0b8c51464e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 40
      ErsatzTV.Core.Tests/FFmpeg/HlsPlaylistFilterTests.cs
  2. 8
      ErsatzTV.Core/FFmpeg/HlsPlaylistFilter.cs

40
ErsatzTV.Core.Tests/FFmpeg/HlsPlaylistFilterTests.cs

@ -996,6 +996,46 @@ live_1760874038_000056.m4s @@ -996,6 +996,46 @@ live_1760874038_000056.m4s
result.Playlist.ShouldBe(expectedPlaylist);
}
[Test]
public void HlsPlaylistFilter_ShouldKeepSegments_WhenNoneAreNewerThanFilterBefore()
{
// long session: the transcode has fallen behind and every segment is now older
// than filterBefore. trimming used to remove all of them and emit a header-only
// playlist with no segments, which leaves the channel unplayable.
var start = new DateTimeOffset(2021, 10, 9, 8, 0, 0, TimeSpan.FromHours(-5));
string[] input = NormalizeLineEndings(
@"#EXTM3U
#EXT-X-VERSION:7
#EXT-X-TARGETDURATION:4
#EXT-X-MEDIA-SEQUENCE:1137
#EXT-X-INDEPENDENT-SEGMENTS
#EXT-X-DISCONTINUITY
#EXT-X-MAP:URI=""1760874038_init.mp4""
#EXTINF:4.000000,
#EXT-X-PROGRAM-DATE-TIME:2021-10-08T08:34:49.320-0500
live_1760874038_001137.m4s
#EXTINF:4.000000,
#EXT-X-PROGRAM-DATE-TIME:2021-10-08T08:34:53.320-0500
live_1760874038_001138.m4s
#EXTINF:4.000000,
#EXT-X-PROGRAM-DATE-TIME:2021-10-08T08:34:57.320-0500
live_1760874038_001139.m4s").Split(Environment.NewLine);
TrimPlaylistResult result = _hlsPlaylistFilter.TrimPlaylistWithDiscontinuity(
new Dictionary<long, int>
{
[1760874038] = 1
},
OutputFormatKind.HlsMp4,
start,
start.AddSeconds(60),
new FakeHlsInitSegmentCache(),
input);
result.SegmentCount.ShouldBeGreaterThan(0);
result.Playlist.ShouldContain("#EXTINF");
}
private static string NormalizeLineEndings(string str) =>
str
.Replace("\r\n", "\n")

8
ErsatzTV.Core/FFmpeg/HlsPlaylistFilter.cs

@ -148,8 +148,10 @@ public class HlsPlaylistFilter(ITempFilePool tempFilePool, ILogger<HlsPlaylistFi @@ -148,8 +148,10 @@ public class HlsPlaylistFilter(ITempFilePool tempFilePool, ILogger<HlsPlaylistFi
var allSegments = items.OfType<PlaylistSegment>().ToList();
// still need to filter old content
if (maybeMaxSegments.IsNone)
// still need to filter old content, but never down to an empty playlist:
// if nothing is newer than filterBefore, keep the existing segments
// (the maxSegments branch below keeps a non-empty tail the same way)
if (maybeMaxSegments.IsNone && allSegments.Any(s => s.StartTime >= filterBefore))
{
allSegments.RemoveAll(s => s.StartTime < filterBefore);
}
@ -268,7 +270,7 @@ public class HlsPlaylistFilter(ITempFilePool tempFilePool, ILogger<HlsPlaylistFi @@ -268,7 +270,7 @@ public class HlsPlaylistFilter(ITempFilePool tempFilePool, ILogger<HlsPlaylistFi
playlist,
nextPlaylistStart,
startSequence,
items.OfType<PlaylistSegment>().Min(s => s.GeneratedAt),
items.OfType<PlaylistSegment>().Select(s => s.GeneratedAt).DefaultIfEmpty(generatedAt).Min(),
allSegments.Count);
}

Loading…
Cancel
Save