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.webmURLs. - 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: bytesand a validContent-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-0response. - 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 includebyteOffset=....
Relevant Diff Snippets
Dreamio/ProgressiveHTTPRangeCache.swift
271 unmodified lines27227327427527627727827928028152 unmodified lines334335336337338339340341342343271 unmodified linesguard !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") == true52 unmodified linesresponse.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 lines27227327427527627752 unmodified lines330331332333334335271 unmodified linesguard !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") == true52 unmodified linesresponse.value(forHTTPHeaderField: name)}}enum HTTPRangeCacheError: Error {
Tests/StreamResolverTests.swift
42 unmodified lines43444546474849491 unmodified lines5415425435445455465475485495505515525535545555565573 unmodified lines56156256356456556656756856942 unmodified linestestRangeCacheForegroundMissFetchesAlignedChunks()await testRangeCacheForegroundMissReprioritizesPrefetch()await testRangeCacheHitFollowsActualPostSeekReadArea()await testRangeProbeBypassesTailIndexContainers()await testRangeProbeFallsBackWhenServerIgnoresRange()await testRangeFetcherPreservesHeaders()print("StreamResolverTests passed")491 unmodified linestry? await Task.sleep(nanoseconds: 50_000_000)}private static func testRangeProbeBypassesTailIndexContainers() async {var requestCount = 0MockURLProtocol.handler = { request inrequestCount += 1let 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 lines43444546474849491 unmodified lines5415425435445455465475485495505515525535545555565575585595605613 unmodified lines56556656756856957057157257357442 unmodified linestestRangeCacheForegroundMissFetchesAlignedChunks()await testRangeCacheForegroundMissReprioritizesPrefetch()await testRangeCacheHitFollowsActualPostSeekReadArea()await testRangeProbeAllowsRangeCacheForMKVWhenServerSupportsRanges()await testRangeProbeFallsBackWhenServerIgnoresRange()await testRangeFetcherPreservesHeaders()print("StreamResolverTests passed")491 unmodified linestry? await Task.sleep(nanoseconds: 50_000_000)}private static func testRangeProbeAllowsRangeCacheForMKVWhenServerSupportsRanges() async {var requestCount = 0MockURLProtocol.handler = { request inrequestCount += 1assertEqual(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
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.