From 4d0e675aa362358944be3c362de1aeeaff8b7543 Mon Sep 17 00:00:00 2001 From: dirtydishes Date: Tue, 26 May 2026 08:10:39 -0400 Subject: [PATCH] let mkv streams use the range cache --- .beads/interactions.jsonl | 1 + .beads/issues.jsonl | 1 + Dreamio/ProgressiveHTTPRangeCache.swift | 8 - Tests/StreamResolverTests.swift | 21 +- .../2026-05-26-fix-vlc-range-cache-mkv.html | 251 ++++++++++++++++++ 5 files changed, 266 insertions(+), 16 deletions(-) create mode 100644 docs/turns/2026-05-26-fix-vlc-range-cache-mkv.html diff --git a/.beads/interactions.jsonl b/.beads/interactions.jsonl index 6ce3541..0d70e4d 100644 --- a/.beads/interactions.jsonl +++ b/.beads/interactions.jsonl @@ -48,3 +48,4 @@ {"id":"int-91b3db21","kind":"field_change","created_at":"2026-05-26T04:40:10.299245Z","actor":"dirtydishes","issue_id":"dreamio-mi1","extra":{"field":"status","new_value":"closed","old_value":"in_progress","reason":"Used actual foreground VLC reads as prefetch follow signals on hits and changed foreground misses to fetch aligned chunks; added regression tests and updated the turn document."}} {"id":"int-ff0aeb09","kind":"field_change","created_at":"2026-05-26T04:47:44.48931Z","actor":"dirtydishes","issue_id":"dreamio-2hw","extra":{"field":"status","new_value":"closed","old_value":"in_progress","reason":"Fixed stale local range-cache prefetch state after cached seek reads and documented the validation."}} {"id":"int-204223f5","kind":"field_change","created_at":"2026-05-26T04:56:13.920284Z","actor":"dirtydishes","issue_id":"dreamio-816","extra":{"field":"status","new_value":"closed","old_value":"in_progress","reason":"Bypassed the local range cache for Matroska-family tail-index containers and added a regression test confirming MKV probes fall back to direct VLC playback without issuing cache probe requests."}} +{"id":"int-b6f641ed","kind":"field_change","created_at":"2026-05-26T12:10:16.392655Z","actor":"dirtydishes","issue_id":"dreamio-3sw","extra":{"field":"status","new_value":"closed","old_value":"in_progress","reason":"Removed the Matroska/WebM extension-level range-cache bypass and added a regression test proving MKV URLs use the cache when the origin advertises byte-range support."}} diff --git a/.beads/issues.jsonl b/.beads/issues.jsonl index 8291585..4481f84 100644 --- a/.beads/issues.jsonl +++ b/.beads/issues.jsonl @@ -1,5 +1,6 @@ {"_type":"issue","id":"dreamio-mun","title":"fix vlc cache loopback port startup","description":"Device logs showed local-cache playback opening http://127.0.0.1:0, because the NWListener ephemeral port was read before the listener reached ready. Wait for the real assigned port before returning the local cache URL.","status":"closed","priority":0,"issue_type":"bug","assignee":"dirtydishes","owner":"dishes@dpdrm.com","created_at":"2026-05-25T22:32:41Z","created_by":"dirtydishes","updated_at":"2026-05-25T22:33:15Z","started_at":"2026-05-25T22:33:14Z","closed_at":"2026-05-25T22:33:15Z","close_reason":"Wait for NWListener ready state before returning the local cache URL; verified tests and simulator build.","dependency_count":0,"dependent_count":0,"comment_count":0} {"_type":"issue","id":"dreamio-8cz","title":"fix stremio external subtitle loading regression","description":"After adding late subtitle forwarding for native playback, Stremio external subtitle loading is failing. Investigate the injected bridge and native subtitle forwarding path, then adjust behavior so Stremio can still load external subtitles while native playback receives late candidates.","status":"closed","priority":0,"issue_type":"bug","assignee":"dirtydishes","owner":"dishes@dpdrm.com","created_at":"2026-05-25T11:05:42Z","created_by":"dirtydishes","updated_at":"2026-05-25T11:07:35Z","started_at":"2026-05-25T11:05:55Z","closed_at":"2026-05-25T11:07:35Z","close_reason":"Hardened subtitle bridge network observers so non-text Stremio subtitle loads are not touched, and made parser traversal deterministic for metadata preservation.","dependency_count":0,"dependent_count":0,"comment_count":0} +{"_type":"issue","id":"dreamio-3sw","title":"Fix VLC range cache fallback for tail-index MKV streams","description":"Video range caching currently refuses streams classified as tail-index containers, causing VLC playback to use direct mode and lose seek prefetch behavior. Investigate the probe logic and enable safe local range caching for these streams without breaking playback startup.","status":"closed","priority":1,"issue_type":"bug","assignee":"dirtydishes","owner":"dishes@dpdrm.com","created_at":"2026-05-26T12:05:20Z","created_by":"dirtydishes","updated_at":"2026-05-26T12:10:16Z","started_at":"2026-05-26T12:05:38Z","closed_at":"2026-05-26T12:10:16Z","close_reason":"Removed the Matroska/WebM extension-level range-cache bypass and added a regression test proving MKV URLs use the cache when the origin advertises byte-range support.","dependency_count":0,"dependent_count":0,"comment_count":0} {"_type":"issue","id":"dreamio-816","title":"Fix local range cache playback buffering","description":"Normal VLC playback can stay in buffering after the local progressive HTTP range cache is enabled. Logs show VLC repeatedly probes header/tail MKV ranges through the loopback server while the cache foreground fetch path serializes 1 MB remote requests. Investigate and adjust the cache path so normal direct-file playback can start reliably.","status":"closed","priority":1,"issue_type":"bug","assignee":"dirtydishes","owner":"dishes@dpdrm.com","created_at":"2026-05-26T04:54:13Z","created_by":"dirtydishes","updated_at":"2026-05-26T04:56:14Z","started_at":"2026-05-26T04:54:17Z","closed_at":"2026-05-26T04:56:14Z","close_reason":"Bypassed the local range cache for Matroska-family tail-index containers and added a regression test confirming MKV probes fall back to direct VLC playback without issuing cache probe requests.","dependency_count":0,"dependent_count":0,"comment_count":0} {"_type":"issue","id":"dreamio-2hw","title":"Fix range cache prefetch cursor after cached seek reads","description":"Skipping after the local range cache has warmed can leave prefetch following an older foreground cursor instead of the post-seek cached read position. Update the cache so cached foreground reads can reset the follow cursor and add regression coverage.","status":"closed","priority":1,"issue_type":"bug","assignee":"dirtydishes","owner":"dishes@dpdrm.com","created_at":"2026-05-26T04:45:44Z","created_by":"dirtydishes","updated_at":"2026-05-26T04:47:44Z","started_at":"2026-05-26T04:46:36Z","closed_at":"2026-05-26T04:47:44Z","close_reason":"Fixed stale local range-cache prefetch state after cached seek reads and documented the validation.","dependency_count":0,"dependent_count":0,"comment_count":0} {"_type":"issue","id":"dreamio-mi1","title":"adapt vlc prefetch to actual post-seek reads","description":"Use real foreground VLC reads after a seek as a prefetch signal even when they are cache hits, and fetch aligned chunks for partial foreground misses so the cache warms ahead before VLC reaches the edge of retained data.","status":"closed","priority":1,"issue_type":"bug","assignee":"dirtydishes","owner":"dishes@dpdrm.com","created_at":"2026-05-26T04:38:14Z","created_by":"dirtydishes","updated_at":"2026-05-26T04:40:10Z","started_at":"2026-05-26T04:38:16Z","closed_at":"2026-05-26T04:40:10Z","close_reason":"Used actual foreground VLC reads as prefetch follow signals on hits and changed foreground misses to fetch aligned chunks; added regression tests and updated the turn document.","dependency_count":0,"dependent_count":0,"comment_count":0} diff --git a/Dreamio/ProgressiveHTTPRangeCache.swift b/Dreamio/ProgressiveHTTPRangeCache.swift index 776789c..d2eb203 100644 --- a/Dreamio/ProgressiveHTTPRangeCache.swift +++ b/Dreamio/ProgressiveHTTPRangeCache.swift @@ -272,10 +272,6 @@ final class HTTPRangeRemoteFetcher { guard !url.path.lowercased().hasSuffix(".m3u8") else { return HTTPRangeProbeResult(isCacheable: false, contentLength: nil, fallbackReason: "hls-playlist") } - guard !Self.shouldBypassCache(for: url) else { - return HTTPRangeProbeResult(isCacheable: false, contentLength: nil, fallbackReason: "tail-index-container") - } - if let head = try? await response(for: request(method: "HEAD")), (200..<400).contains(head.statusCode) { let acceptsRanges = header("Accept-Ranges", in: head)?.lowercased().contains("bytes") == true @@ -334,10 +330,6 @@ final class HTTPRangeRemoteFetcher { response.value(forHTTPHeaderField: name) } - private static func shouldBypassCache(for url: URL) -> Bool { - let extensionName = url.pathExtension.lowercased() - return ["mkv", "mk3d", "mka", "mks", "webm"].contains(extensionName) - } } enum HTTPRangeCacheError: Error { diff --git a/Tests/StreamResolverTests.swift b/Tests/StreamResolverTests.swift index 8f3dab7..bb9d71c 100644 --- a/Tests/StreamResolverTests.swift +++ b/Tests/StreamResolverTests.swift @@ -43,7 +43,7 @@ struct StreamResolverTests { testRangeCacheForegroundMissFetchesAlignedChunks() await testRangeCacheForegroundMissReprioritizesPrefetch() await testRangeCacheHitFollowsActualPostSeekReadArea() - await testRangeProbeBypassesTailIndexContainers() + await testRangeProbeAllowsRangeCacheForMKVWhenServerSupportsRanges() await testRangeProbeFallsBackWhenServerIgnoresRange() await testRangeFetcherPreservesHeaders() print("StreamResolverTests passed") @@ -541,17 +541,21 @@ struct StreamResolverTests { try? await Task.sleep(nanoseconds: 50_000_000) } - private static func testRangeProbeBypassesTailIndexContainers() async { + private static func testRangeProbeAllowsRangeCacheForMKVWhenServerSupportsRanges() async { var requestCount = 0 MockURLProtocol.handler = { request in requestCount += 1 + assertEqual(request.httpMethod, "HEAD") let response = HTTPURLResponse( url: request.url!, - statusCode: 206, + statusCode: 200, httpVersion: nil, - headerFields: ["Content-Range": "bytes 0-0/20"] + headerFields: [ + "Accept-Ranges": "bytes", + "Content-Length": "20" + ] )! - return (Data([1]), response) + return (Data(), response) } let fetcher = HTTPRangeRemoteFetcher( @@ -561,9 +565,10 @@ struct StreamResolverTests { ) let probe = await fetcher.probe() - assertEqual(probe.isCacheable, false) - assertEqual(probe.fallbackReason, "tail-index-container") - assertEqual(requestCount, 0) + assertEqual(probe.isCacheable, true) + assertEqual(probe.contentLength, 20) + assertEqual(probe.fallbackReason, nil) + assertEqual(requestCount, 1) MockURLProtocol.handler = nil } diff --git a/docs/turns/2026-05-26-fix-vlc-range-cache-mkv.html b/docs/turns/2026-05-26-fix-vlc-range-cache-mkv.html new file mode 100644 index 0000000..2331755 --- /dev/null +++ b/docs/turns/2026-05-26-fix-vlc-range-cache-mkv.html @@ -0,0 +1,251 @@ + + + + + + Fix VLC Range Cache for MKV Streams + + + +
+
+
Dreamio turn documentation ยท May 26, 2026
+

Fix VLC Range Cache for MKV Streams

+

Removed the blanket Matroska/WebM cache bypass so direct-file MKV streams can use Dreamio's local range cache when the origin server confirms byte-range support.

+
+ +
+

Summary

+

Dreamio was refusing to range-cache MKV streams before checking the server. That made Torrentio and Real-Debrid MKV playback open in direct mode, so seek prefetch could not run. The cache probe now lets normal HTTP range capability decide whether the local cache should be used.

+
+ +
+

Changes Made

+
    +
  • Removed the hard-coded cache bypass for .mkv, .mk3d, .mka, .mks, and .webm URLs.
  • +
  • Kept the existing non-HTTP and HLS playlist fallbacks intact.
  • +
  • Updated the range probe regression test so MKV URLs are cacheable when the server returns Accept-Ranges: bytes and a valid Content-Length.
  • +
+
+ +
+

Context

+

The diagnostic logs showed [DreamioVLC] cache fallback reason=tail-index-container, followed by opening mode=direct and direct-mode seek logs. That fallback came from an extension check, not a failed HTTP range probe. Because many debrid MKV streams do support byte ranges, the app was leaving useful buffering behavior on the table.

+
+ +
+

Important Implementation Details

+
    +
  • The probe still requires either a HEAD response with byte-range support and content length, or a successful GET Range: bytes=0-0 response.
  • +
  • If an MKV origin ignores range requests, Dreamio still falls back to direct playback through the existing range-probe-status-... path.
  • +
  • The expected debug signal for a compatible MKV is now [DreamioVLC] opening mode=local-cache, and seeks should include byteOffset=....
  • +
+
+ +
+

Relevant Diff Snippets

+

Dreamio/ProgressiveHTTPRangeCache.swift

Dreamio/ProgressiveHTTPRangeCache.swift
-8
271 unmodified lines
272
273
274
275
276
277
278
279
280
281
52 unmodified lines
334
335
336
337
338
339
340
341
342
343
271 unmodified lines
guard !url.path.lowercased().hasSuffix(".m3u8") else {
return HTTPRangeProbeResult(isCacheable: false, contentLength: nil, fallbackReason: "hls-playlist")
}
guard !Self.shouldBypassCache(for: url) else {
return HTTPRangeProbeResult(isCacheable: false, contentLength: nil, fallbackReason: "tail-index-container")
}
+
if let head = try? await response(for: request(method: "HEAD")),
(200..<400).contains(head.statusCode) {
let acceptsRanges = header("Accept-Ranges", in: head)?.lowercased().contains("bytes") == true
52 unmodified lines
response.value(forHTTPHeaderField: name)
}
+
private static func shouldBypassCache(for url: URL) -> Bool {
let extensionName = url.pathExtension.lowercased()
return ["mkv", "mk3d", "mka", "mks", "webm"].contains(extensionName)
}
}
+
enum HTTPRangeCacheError: Error {
271 unmodified lines
272
273
274
275
276
277
52 unmodified lines
330
331
332
333
334
335
271 unmodified lines
guard !url.path.lowercased().hasSuffix(".m3u8") else {
return HTTPRangeProbeResult(isCacheable: false, contentLength: nil, fallbackReason: "hls-playlist")
}
if let head = try? await response(for: request(method: "HEAD")),
(200..<400).contains(head.statusCode) {
let acceptsRanges = header("Accept-Ranges", in: head)?.lowercased().contains("bytes") == true
52 unmodified lines
response.value(forHTTPHeaderField: name)
}
+
}
+
enum HTTPRangeCacheError: Error {

Tests/StreamResolverTests.swift

Tests/StreamResolverTests.swift
-8+13
42 unmodified lines
43
44
45
46
47
48
49
491 unmodified lines
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
3 unmodified lines
561
562
563
564
565
566
567
568
569
42 unmodified lines
testRangeCacheForegroundMissFetchesAlignedChunks()
await testRangeCacheForegroundMissReprioritizesPrefetch()
await testRangeCacheHitFollowsActualPostSeekReadArea()
await testRangeProbeBypassesTailIndexContainers()
await testRangeProbeFallsBackWhenServerIgnoresRange()
await testRangeFetcherPreservesHeaders()
print("StreamResolverTests passed")
491 unmodified lines
try? await Task.sleep(nanoseconds: 50_000_000)
}
+
private static func testRangeProbeBypassesTailIndexContainers() async {
var requestCount = 0
MockURLProtocol.handler = { request in
requestCount += 1
let response = HTTPURLResponse(
url: request.url!,
statusCode: 206,
httpVersion: nil,
headerFields: ["Content-Range": "bytes 0-0/20"]
)!
return (Data([1]), response)
}
+
let fetcher = HTTPRangeRemoteFetcher(
3 unmodified lines
)
let probe = await fetcher.probe()
+
assertEqual(probe.isCacheable, false)
assertEqual(probe.fallbackReason, "tail-index-container")
assertEqual(requestCount, 0)
MockURLProtocol.handler = nil
}
+
42 unmodified lines
43
44
45
46
47
48
49
491 unmodified lines
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
3 unmodified lines
565
566
567
568
569
570
571
572
573
574
42 unmodified lines
testRangeCacheForegroundMissFetchesAlignedChunks()
await testRangeCacheForegroundMissReprioritizesPrefetch()
await testRangeCacheHitFollowsActualPostSeekReadArea()
await testRangeProbeAllowsRangeCacheForMKVWhenServerSupportsRanges()
await testRangeProbeFallsBackWhenServerIgnoresRange()
await testRangeFetcherPreservesHeaders()
print("StreamResolverTests passed")
491 unmodified lines
try? await Task.sleep(nanoseconds: 50_000_000)
}
+
private static func testRangeProbeAllowsRangeCacheForMKVWhenServerSupportsRanges() async {
var requestCount = 0
MockURLProtocol.handler = { request in
requestCount += 1
assertEqual(request.httpMethod, "HEAD")
let response = HTTPURLResponse(
url: request.url!,
statusCode: 200,
httpVersion: nil,
headerFields: [
"Accept-Ranges": "bytes",
"Content-Length": "20"
]
)!
return (Data(), response)
}
+
let fetcher = HTTPRangeRemoteFetcher(
3 unmodified lines
)
let probe = await fetcher.probe()
+
assertEqual(probe.isCacheable, true)
assertEqual(probe.contentLength, 20)
assertEqual(probe.fallbackReason, nil)
assertEqual(requestCount, 1)
MockURLProtocol.handler = nil
}
+
+
+ +
+

Expected Impact for End-Users

+

Compatible MKV direct-file streams should start through Dreamio's local range cache instead of direct VLC mode. Backward and forward skips can now prime nearby bytes, which should reduce stalls after seeking on supported servers.

+
+ +
+

Validation

+
    +
  • Passed swiftc Dreamio/StreamCandidate.swift Dreamio/StreamResolver.swift Dreamio/ProgressiveHTTPRangeCache.swift Dreamio/ExternalSubtitleTrackParser.swift Tests/StreamResolverTests.swift -o /tmp/StreamResolverTests && /tmp/StreamResolverTests
  • +
  • Passed DEVELOPER_DIR=/Applications/Xcode.app/Contents/Developer xcodebuild -workspace Dreamio.xcworkspace -scheme Dreamio -configuration Debug -sdk iphonesimulator build
  • +
+
+ +
+

Issues, Limitations, and Mitigations

+
This does not guarantee every MKV will use the cache. Servers that lack byte-range support, omit content length, reject range requests, or fail the local cache server setup still fall back to direct VLC playback. That is intentional so playback continues instead of failing hard.
+
+ +
+

Follow-up Work

+
    +
  • Test the original Torrentio/Real-Debrid South Park stream on device and confirm logs show opening mode=local-cache.
  • +
  • If startup is slower on some MKV sources, consider measuring HEAD latency and falling back to the tiny range probe sooner.
  • +
  • Improve external subtitle auto-selection so English does not lose to the first parsed subtitle track.
  • +
+
+
+ +