Dreamio turn document ยท May 25, 2026

Local Seek Buffer for VLC Playback

Added a local HTTP range proxy/cache for native VLC playback so direct-file streams can reuse recently fetched bytes for short rewinds and opportunistically warm nearby forward ranges.

Summary

Dreamio now starts a per-playback loopback proxy before handing a direct HTTP stream to VLC. VLC receives a localhost URL, while the proxy forwards authenticated byte-range requests upstream, stores bounded temporary chunks, serves complete range hits locally, and cleans up the cache when the native player dismisses.

Changes Made

Context

VLC was previously opened directly on debrid or direct-file URLs. That means small seeks depended entirely on VLC and the upstream server. The new proxy gives Dreamio a narrow local buffer for HTTP/HTTPS direct-file streams without changing Stremio interception or subtitle discovery contracts.

Important Implementation Details

Relevant Diff Snippets

Dreamio/NativePlayerViewController.swift

Dreamio/NativePlayerViewController.swift
-1+22
10 unmodified lines
11
12
13
14
15
16
120 unmodified lines
137
138
139
140
141
142
143
48 unmodified lines
192
193
194
195
196
197
198
199
200
10 unmodified lines
private var attachedSubtitleURLs: Set<URL>
private var audioMenuSignature: String?
private var captionsMenuSignature: String?
var onDismiss: (() -> Void)?
private let loadingView: UIActivityIndicatorView = {
120 unmodified lines
configureBackend()
configureLayout()
startStartupTimer()
backend.play(request: request)
addSubtitleCandidates(request.subtitleCandidates)
}
48 unmodified lines
controlsTimer?.invalidate()
progressTimer?.invalidate()
backend.stop()
onDismiss?()
}
private func resolveSubtitleCandidates(_ candidates: [SubtitleCandidate]) async -> [SubtitleCandidate] {
var resolved: [SubtitleCandidate] = []
for candidate in candidates {
10 unmodified lines
11
12
13
14
15
16
17
120 unmodified lines
138
139
140
141
142
143
144
145
48 unmodified lines
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
10 unmodified lines
private var attachedSubtitleURLs: Set<URL>
private var audioMenuSignature: String?
private var captionsMenuSignature: String?
private var streamCacheProxy: NativeStreamCacheProxy?
var onDismiss: (() -> Void)?
private let loadingView: UIActivityIndicatorView = {
120 unmodified lines
configureBackend()
configureLayout()
startStartupTimer()
let playbackRequest = startProxyPlaybackRequest(for: request)
backend.play(request: playbackRequest)
addSubtitleCandidates(request.subtitleCandidates)
}
48 unmodified lines
controlsTimer?.invalidate()
progressTimer?.invalidate()
backend.stop()
streamCacheProxy?.stop()
streamCacheProxy = nil
onDismiss?()
}
private func startProxyPlaybackRequest(for request: NativePlaybackRequest) -> NativePlaybackRequest {
guard request.playbackURL.scheme?.lowercased().hasPrefix("http") == true else {
return request
}
let proxy = NativeStreamCacheProxy(session: NativeStreamCacheProxy.Session(request: request))
do {
let proxyURL = try proxy.start()
streamCacheProxy = proxy
return request.withPlaybackURL(proxyURL)
} catch {
#if DEBUG
print("[DreamioStreamProxy] start-failed error=\(error.localizedDescription)")
#endif
return request
}
}
private func resolveSubtitleCandidates(_ candidates: [SubtitleCandidate]) async -> [SubtitleCandidate] {
var resolved: [SubtitleCandidate] = []
for candidate in candidates {

Dreamio/StreamCandidate.swift

Dreamio/StreamCandidate.swift
+14
26 unmodified lines
27
28
29
30
31
32
26 unmodified lines
let headers: [String: String]
let classification: StreamClassification
let subtitleCandidates: [SubtitleCandidate]
}
struct SubtitleCandidate: Equatable {
26 unmodified lines
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
26 unmodified lines
let headers: [String: String]
let classification: StreamClassification
let subtitleCandidates: [SubtitleCandidate]
func withPlaybackURL(_ playbackURL: URL) -> NativePlaybackRequest {
NativePlaybackRequest(
playbackURL: playbackURL,
observedURL: observedURL,
resolverURL: resolverURL,
pageURL: pageURL,
userAgent: userAgent,
referer: referer,
headers: headers,
classification: classification,
subtitleCandidates: subtitleCandidates
)
}
}
struct SubtitleCandidate: Equatable {

Tests/StreamResolverTests.swift

Tests/StreamResolverTests.swift
+85
23 unmodified lines
24
25
26
27
28
29
10 unmodified lines
40
41
42
43
44
45
23 unmodified lines
testSubtitleDisplayNameNormalization()
testSubtitleDisplayNameUsesPreservedNamesForGenericVLCTracks()
testSubtitleOptionMappingIncludesNone()
print("StreamResolverTests passed")
}
10 unmodified lines
assertEqual(request.headers["User-Agent"], "DreamioTest/1")
}
private static func testResolverSelectsUnsupportedDirectURLAndHeaders() {
let payload: [String: Any] = [
"streams": [
23 unmodified lines
24
25
26
27
28
29
30
31
32
33
34
35
10 unmodified lines
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
23 unmodified lines
testSubtitleDisplayNameNormalization()
testSubtitleDisplayNameUsesPreservedNamesForGenericVLCTracks()
testSubtitleOptionMappingIncludesNone()
testHTTPRangeParsing()
testContentRangeFormatting()
testCacheLookupAcrossChunkBoundaries()
testCacheEvictionOutsideByteBudget()
testProxyForwardsUpstreamHeaders()
testProxyPassThroughFallbackStatus()
print("StreamResolverTests passed")
}
10 unmodified lines
assertEqual(request.headers["User-Agent"], "DreamioTest/1")
}
private static func testHTTPRangeParsing() {
assertEqual(HTTPRange.parse("bytes=10-20"), HTTPRange(start: 10, end: 20))
assertEqual(HTTPRange.parse("bytes=10-"), HTTPRange(start: 10, end: nil))
assert(HTTPRange.parse("items=10-20") == nil, "Expected non-byte range to be rejected")
assert(HTTPRange.parse("bytes=-20") == nil, "Expected suffix ranges to be rejected for v1")
assert(HTTPRange.parse("bytes=20-10") == nil, "Expected inverted ranges to be rejected")
assert(HTTPRange.parse("bytes=1-2,3-4") == nil, "Expected multipart ranges to be rejected")
}
private static func testContentRangeFormatting() {
assertEqual(HTTPRange.contentRange(start: 10, end: 20, totalLength: 100), "bytes 10-20/100")
assertEqual(HTTPRange.contentRange(start: 10, end: 20, totalLength: nil), "bytes 10-20/*")
}
private static func testCacheLookupAcrossChunkBoundaries() {
let store = CachedRangeStore(sessionID: "test-\(UUID().uuidString)", byteBudget: 1024)
defer { store.removeAll() }
store.store(data: Data("abc".utf8), start: 0)
store.store(data: Data("def".utf8), start: 3)
let lookup = store.lookup(range: HTTPRange(start: 0, end: 5), maximumLength: 6)
assertEqual(String(data: lookup?.data ?? Data(), encoding: .utf8), "abcdef")
assertEqual(lookup?.isComplete, true)
}
private static func testCacheEvictionOutsideByteBudget() {
let store = CachedRangeStore(sessionID: "test-\(UUID().uuidString)", byteBudget: 6)
defer { store.removeAll() }
store.store(data: Data("abcdef".utf8), start: 0)
store.store(data: Data("ghijkl".utf8), start: 6)
let oldLookup = store.lookup(range: HTTPRange(start: 0, end: 5), maximumLength: 6)
let newLookup = store.lookup(range: HTTPRange(start: 6, end: 11), maximumLength: 6)
assert(oldLookup == nil, "Expected old chunk to be evicted outside the byte budget")
assertEqual(String(data: newLookup?.data ?? Data(), encoding: .utf8), "ghijkl")
}
private static func testProxyForwardsUpstreamHeaders() {
let proxy = NativeStreamCacheProxy(session: NativeStreamCacheProxy.Session(request: proxyTestRequest()))
let upstreamRequest = proxy.upstreamRequest(for: HTTPRange(start: 12, end: 34))
assertEqual(upstreamRequest.value(forHTTPHeaderField: "Range"), "bytes=12-34")
assertEqual(upstreamRequest.value(forHTTPHeaderField: "Referer"), "https://resolver.example.test/")
assertEqual(upstreamRequest.value(forHTTPHeaderField: "User-Agent"), "DreamioTest/1")
assertEqual(upstreamRequest.value(forHTTPHeaderField: "Authorization"), "Bearer secret")
}
private static func testProxyPassThroughFallbackStatus() {
assertEqual(NativeStreamCacheProxy.responseStatusForUpstreamStatus(206), 206)
assertEqual(NativeStreamCacheProxy.responseStatusForUpstreamStatus(200), 200)
}
private static func proxyTestRequest() -> NativePlaybackRequest {
NativePlaybackRequest(
playbackURL: URL(string: "https://cdn.example.test/movie.mkv")!,
observedURL: URL(string: "https://cdn.example.test/movie.mkv")!,
resolverURL: URL(string: "https://resolver.example.test/play")!,
pageURL: nil,
userAgent: "DreamioTest/1",
referer: "https://resolver.example.test/",
headers: [
"Referer": "https://resolver.example.test/",
"User-Agent": "DreamioTest/1",
"Authorization": "Bearer secret"
],
classification: StreamClassification(
sourceKind: .directFile,
containerGuess: .mkv,
reason: "test",
shouldIntercept: true,
sanitizedObservedURL: "https://cdn.example.test/movie.mkv",
sanitizedResolverURL: nil
),
subtitleCandidates: []
)
}
private static func testResolverSelectsUnsupportedDirectURLAndHeaders() {
let payload: [String: Any] = [
"streams": [

Dreamio.xcodeproj/project.pbxproj

Dreamio.xcodeproj/project.pbxproj
-1+6
14 unmodified lines
15
16
17
18
19
20
21
7 unmodified lines
29
30
31
32
33
34
4 unmodified lines
39
40
41
42
43
44
46 unmodified lines
91
92
93
94
95
96
140 unmodified lines
237
238
239
240
241
242
14 unmodified lines
6F2A2B442C00100100DREAMIO /* VLCNativePlaybackBackend.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6F2A2B482C00100100DREAMIO /* VLCNativePlaybackBackend.swift */; };
6F2A2B452C00100100DREAMIO /* NativePlayerViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6F2A2B492C00100100DREAMIO /* NativePlayerViewController.swift */; };
6F2A2B502C00100100DREAMIO /* StreamResolver.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6F2A2B512C00100100DREAMIO /* StreamResolver.swift */; };
BA013CEC876B829A86AE8DCB /* Pods_Dreamio.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 908FA15B08AB341C116BAB46 /* Pods_Dreamio.framework */; };
/* End PBXBuildFile section */
/* Begin PBXFileReference section */
7 unmodified lines
6F2A2B482C00100100DREAMIO /* VLCNativePlaybackBackend.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VLCNativePlaybackBackend.swift; sourceTree = "<group>"; };
6F2A2B492C00100100DREAMIO /* NativePlayerViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NativePlayerViewController.swift; sourceTree = "<group>"; };
6F2A2B512C00100100DREAMIO /* StreamResolver.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StreamResolver.swift; sourceTree = "<group>"; };
701702B9C2BFBEDE36E7F0A3 /* Pods-Dreamio.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Dreamio.release.xcconfig"; path = "Target Support Files/Pods-Dreamio/Pods-Dreamio.release.xcconfig"; sourceTree = "<group>"; };
908FA15B08AB341C116BAB46 /* Pods_Dreamio.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_Dreamio.framework; sourceTree = BUILT_PRODUCTS_DIR; };
BF0A4D5BAC9400AEEF3B0181 /* Pods-Dreamio.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Dreamio.debug.xcconfig"; path = "Target Support Files/Pods-Dreamio/Pods-Dreamio.debug.xcconfig"; sourceTree = "<group>"; };
4 unmodified lines
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
);
runOnlyForDeploymentPostprocessing = 0;
};
46 unmodified lines
6F2A2B512C00100100DREAMIO /* StreamResolver.swift */,
6F2A2B472C00100100DREAMIO /* NativePlaybackBackend.swift */,
6F2A2B482C00100100DREAMIO /* VLCNativePlaybackBackend.swift */,
6F2A2B492C00100100DREAMIO /* NativePlayerViewController.swift */,
6F2A2B392C00100100DREAMIO /* Info.plist */,
);
140 unmodified lines
6F2A2B502C00100100DREAMIO /* StreamResolver.swift in Sources */,
6F2A2B432C00100100DREAMIO /* NativePlaybackBackend.swift in Sources */,
6F2A2B442C00100100DREAMIO /* VLCNativePlaybackBackend.swift in Sources */,
6F2A2B452C00100100DREAMIO /* NativePlayerViewController.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
14 unmodified lines
15
16
17
18
19
20
21
22
7 unmodified lines
30
31
32
33
34
35
36
4 unmodified lines
41
42
43
44
45
46
47
46 unmodified lines
94
95
96
97
98
99
100
140 unmodified lines
241
242
243
244
245
246
247
14 unmodified lines
6F2A2B442C00100100DREAMIO /* VLCNativePlaybackBackend.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6F2A2B482C00100100DREAMIO /* VLCNativePlaybackBackend.swift */; };
6F2A2B452C00100100DREAMIO /* NativePlayerViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6F2A2B492C00100100DREAMIO /* NativePlayerViewController.swift */; };
6F2A2B502C00100100DREAMIO /* StreamResolver.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6F2A2B512C00100100DREAMIO /* StreamResolver.swift */; };
6F2A2B522C00100100DREAMIO /* NativeStreamCacheProxy.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6F2A2B532C00100100DREAMIO /* NativeStreamCacheProxy.swift */; };
B6C42C187A771A50D200AD84 /* Pods_Dreamio.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 908FA15B08AB341C116BAB46 /* Pods_Dreamio.framework */; };
/* End PBXBuildFile section */
/* Begin PBXFileReference section */
7 unmodified lines
6F2A2B482C00100100DREAMIO /* VLCNativePlaybackBackend.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VLCNativePlaybackBackend.swift; sourceTree = "<group>"; };
6F2A2B492C00100100DREAMIO /* NativePlayerViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NativePlayerViewController.swift; sourceTree = "<group>"; };
6F2A2B512C00100100DREAMIO /* StreamResolver.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StreamResolver.swift; sourceTree = "<group>"; };
6F2A2B532C00100100DREAMIO /* NativeStreamCacheProxy.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NativeStreamCacheProxy.swift; sourceTree = "<group>"; };
701702B9C2BFBEDE36E7F0A3 /* Pods-Dreamio.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Dreamio.release.xcconfig"; path = "Target Support Files/Pods-Dreamio/Pods-Dreamio.release.xcconfig"; sourceTree = "<group>"; };
908FA15B08AB341C116BAB46 /* Pods_Dreamio.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_Dreamio.framework; sourceTree = BUILT_PRODUCTS_DIR; };
BF0A4D5BAC9400AEEF3B0181 /* Pods-Dreamio.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Dreamio.debug.xcconfig"; path = "Target Support Files/Pods-Dreamio/Pods-Dreamio.debug.xcconfig"; sourceTree = "<group>"; };
4 unmodified lines
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
B6C42C187A771A50D200AD84 /* Pods_Dreamio.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
46 unmodified lines
6F2A2B512C00100100DREAMIO /* StreamResolver.swift */,
6F2A2B472C00100100DREAMIO /* NativePlaybackBackend.swift */,
6F2A2B482C00100100DREAMIO /* VLCNativePlaybackBackend.swift */,
6F2A2B532C00100100DREAMIO /* NativeStreamCacheProxy.swift */,
6F2A2B492C00100100DREAMIO /* NativePlayerViewController.swift */,
6F2A2B392C00100100DREAMIO /* Info.plist */,
);
140 unmodified lines
6F2A2B502C00100100DREAMIO /* StreamResolver.swift in Sources */,
6F2A2B432C00100100DREAMIO /* NativePlaybackBackend.swift in Sources */,
6F2A2B442C00100100DREAMIO /* VLCNativePlaybackBackend.swift in Sources */,
6F2A2B522C00100100DREAMIO /* NativeStreamCacheProxy.swift in Sources */,
6F2A2B452C00100100DREAMIO /* NativePlayerViewController.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;

Expected Impact for End-Users

Short backward jumps should be more likely to resume quickly once playback has warmed the rolling cache. Short forward jumps can benefit from read-ahead when upstream speed allows. Seeking outside the retained window still falls back to upstream range fetching instead of wedging the player.

Validation

Issues, Limitations, and Mitigations

Follow-up Work

New Changes as of May 25, 2026 at 5:55 PM

Summary of changes

Adjusted the local stream proxy after device logs showed VLC remained pinned in buffering after a +15 second jump. The proxy now handles VLC HTTP probes more normally and reduces the amount of upstream data it waits for before responding to seek reads.

Why this change was made

The buffering logs showed VLC stayed at the pre-jump timestamp even though the player reported a seekable stream. That pointed at the local HTTP proxy path: VLC can issue HEAD probes and multiple range reads, and the first version treated every request as a large ranged GET on the listener queue.

Code diffs

Dreamio/NativeStreamCacheProxy.swift

Dreamio/NativeStreamCacheProxy.swift
-12+89
66 unmodified lines
67
68
69
70
71
72
6 unmodified lines
79
80
81
82
83
84
30 unmodified lines
115
116
117
118
119
120
12 unmodified lines
133
134
135
136
137
138
7 unmodified lines
146
147
148
149
150
151
48 unmodified lines
200
201
202
203
204
205
206
207
208
209
210
49 unmodified lines
260
261
262
263
264
265
266
267
268
269
270
271
20 unmodified lines
292
293
294
295
296
297
298
299
4 unmodified lines
304
305
306
307
308
309
8 unmodified lines
318
319
320
321
322
323
324
325
326
327
328
21 unmodified lines
350
351
352
353
354
355
356
11 unmodified lines
368
369
370
371
372
373
4 unmodified lines
378
379
380
381
382
383
1 unmodified line
385
386
387
388
389
390
391
392
1 unmodified line
394
395
396
397
398
399
66 unmodified lines
private let directory: URL
private let byteBudget: Int64
private let fileManager: FileManager
private var chunks: [Chunk] = []
init(sessionID: String, byteBudget: Int64, fileManager: FileManager = .default) {
6 unmodified lines
}
func lookup(range: HTTPRange, maximumLength: Int64) -> Lookup? {
let requestedEnd = range.end ?? range.start + maximumLength - 1
var cursor = range.start
var data = Data()
30 unmodified lines
}
func store(data: Data, start: Int64) {
guard !data.isEmpty else {
return
}
12 unmodified lines
}
func evictKeepingBytesNear(offset: Int64) {
let lowerBound = max(0, offset - byteBudget)
let removed = chunks.filter { $0.end < lowerBound }
chunks.removeAll { $0.end < lowerBound }
7 unmodified lines
}
func removeAll() {
try? fileManager.removeItem(at: directory)
chunks.removeAll()
}
48 unmodified lines
private let prefetchLength: Int64
private var listener: NWListener?
private let queue = DispatchQueue(label: "dreamio.native-stream-cache-proxy")
init(session: Session, byteBudget: Int64? = nil, fetchLength: Int64 = 8 * 1024 * 1024) {
self.session = session
self.fetchLength = fetchLength
prefetchLength = fetchLength
let budget = byteBudget ?? max(30 * 1024 * 1024, (session.estimatedBitrate ?? 0) * 30 / 8)
store = CachedRangeStore(sessionID: session.id, byteBudget: budget)
}
49 unmodified lines
connection.cancel()
return
}
let range = HTTPRange.parse(request.headers["range"])
self.respond(to: range, on: connection)
}
}
private func respond(to requestedRange: HTTPRange?, on connection: NWConnection) {
let range = requestedRange ?? HTTPRange(start: 0, end: fetchLength - 1)
let maximumLength = range.length ?? fetchLength
if let lookup = store.lookup(range: range, maximumLength: maximumLength), lookup.isComplete {
20 unmodified lines
if responseStatus == 206 {
store.store(data: response.data, start: range.start)
store.evictKeepingBytesNear(offset: range.start)
let contentType = response.headers["Content-Type"] as? String
let totalLength = totalLength(from: response.headers["Content-Range"] as? String)
send(data: response.data, statusCode: 206, rangeStart: range.start, totalLength: totalLength, contentType: contentType, on: connection)
prefetch(after: range.start + Int64(response.data.count))
} else {
4 unmodified lines
}
}
private func prefetch(after offset: Int64) {
let range = HTTPRange(start: offset, end: offset + prefetchLength - 1)
queue.async { [weak self] in
8 unmodified lines
}
private func fetch(range: HTTPRange) -> UpstreamResponse? {
let semaphore = DispatchSemaphore(value: 0)
var result: UpstreamResponse?
URLSession.shared.dataTask(with: upstreamRequest(for: range)) { data, response, _ in
if let http = response as? HTTPURLResponse, let data {
result = UpstreamResponse(statusCode: http.statusCode, headers: http.allHeaderFields, data: data)
}
semaphore.signal()
}.resume()
21 unmodified lines
"Accept-Ranges": "none",
"Connection": "close"
]
if let contentType = upstreamHeaders["Content-Type"] as? String {
headers["Content-Type"] = contentType
}
send(statusCode: statusCode, headers: headers, body: data, on: connection)
11 unmodified lines
connection.send(content: response, completion: .contentProcessed { _ in connection.cancel() })
}
private func totalLength(from contentRange: String?) -> Int64? {
guard let contentRange, let slash = contentRange.lastIndex(of: "/") else {
return nil
4 unmodified lines
}
private struct HTTPRequest {
let headers: [String: String]
init?(data: Data) {
1 unmodified line
let headerBlock = string.components(separatedBy: "\r\n\r\n").first else {
return nil
}
var headers: [String: String] = [:]
for line in headerBlock.components(separatedBy: "\r\n").dropFirst() {
guard let separator = line.firstIndex(of: ":") else {
continue
}
1 unmodified line
let value = line[line.index(after: separator)...].trimmingCharacters(in: .whitespacesAndNewlines)
headers[key] = value
}
self.headers = headers
}
}
66 unmodified lines
67
68
69
70
71
72
73
6 unmodified lines
80
81
82
83
84
85
86
87
30 unmodified lines
118
119
120
121
122
123
124
125
12 unmodified lines
138
139
140
141
142
143
144
145
7 unmodified lines
153
154
155
156
157
158
159
160
48 unmodified lines
209
210
211
212
213
214
215
216
217
218
219
220
49 unmodified lines
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
20 unmodified lines
316
317
318
319
320
321
322
323
4 unmodified lines
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
8 unmodified lines
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
21 unmodified lines
409
410
411
412
413
414
415
11 unmodified lines
427
428
429
430
431
432
433
434
435
436
437
438
4 unmodified lines
443
444
445
446
447
448
449
450
1 unmodified line
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
1 unmodified line
469
470
471
472
473
474
475
476
66 unmodified lines
private let directory: URL
private let byteBudget: Int64
private let fileManager: FileManager
private let lock = NSLock()
private var chunks: [Chunk] = []
init(sessionID: String, byteBudget: Int64, fileManager: FileManager = .default) {
6 unmodified lines
}
func lookup(range: HTTPRange, maximumLength: Int64) -> Lookup? {
lock.lock()
defer { lock.unlock() }
let requestedEnd = range.end ?? range.start + maximumLength - 1
var cursor = range.start
var data = Data()
30 unmodified lines
}
func store(data: Data, start: Int64) {
lock.lock()
defer { lock.unlock() }
guard !data.isEmpty else {
return
}
12 unmodified lines
}
func evictKeepingBytesNear(offset: Int64) {
lock.lock()
defer { lock.unlock() }
let lowerBound = max(0, offset - byteBudget)
let removed = chunks.filter { $0.end < lowerBound }
chunks.removeAll { $0.end < lowerBound }
7 unmodified lines
}
func removeAll() {
lock.lock()
defer { lock.unlock() }
try? fileManager.removeItem(at: directory)
chunks.removeAll()
}
48 unmodified lines
private let prefetchLength: Int64
private var listener: NWListener?
private let queue = DispatchQueue(label: "dreamio.native-stream-cache-proxy")
private let workQueue = DispatchQueue(label: "dreamio.native-stream-cache-proxy.work", attributes: .concurrent)
init(session: Session, byteBudget: Int64? = nil, fetchLength: Int64 = 1024 * 1024) {
self.session = session
self.fetchLength = fetchLength
prefetchLength = 4 * fetchLength
let budget = byteBudget ?? max(30 * 1024 * 1024, (session.estimatedBitrate ?? 0) * 30 / 8)
store = CachedRangeStore(sessionID: session.id, byteBudget: budget)
}
49 unmodified lines
connection.cancel()
return
}
self.workQueue.async {
self.respond(to: request, on: connection)
}
}
}
private func respond(to request: HTTPRequest, on connection: NWConnection) {
guard request.path.hasPrefix("/stream/") else {
sendStatus(404, on: connection)
return
}
if request.method == "HEAD" {
respondToHead(on: connection)
return
}
guard request.method == "GET" else {
sendStatus(405, on: connection)
return
}
let requestedRange = HTTPRange.parse(request.headers["range"])
let range = requestedRange ?? HTTPRange(start: 0, end: fetchLength - 1)
let maximumLength = range.length ?? fetchLength
if let lookup = store.lookup(range: range, maximumLength: maximumLength), lookup.isComplete {
20 unmodified lines
if responseStatus == 206 {
store.store(data: response.data, start: range.start)
store.evictKeepingBytesNear(offset: range.start)
let contentType = headerValue(response.headers, named: "Content-Type")
let totalLength = totalLength(from: headerValue(response.headers, named: "Content-Range"))
send(data: response.data, statusCode: 206, rangeStart: range.start, totalLength: totalLength, contentType: contentType, on: connection)
prefetch(after: range.start + Int64(response.data.count))
} else {
4 unmodified lines
}
}
private func respondToHead(on connection: NWConnection) {
guard let response = fetchHead() ?? fetch(range: HTTPRange(start: 0, end: 0)) else {
sendStatus(502, on: connection)
return
}
var headers = [
"Accept-Ranges": response.statusCode == 206 ? "bytes" : headerValue(response.headers, named: "Accept-Ranges") ?? "bytes",
"Connection": "close"
]
if let contentType = headerValue(response.headers, named: "Content-Type") {
headers["Content-Type"] = contentType
}
if let length = headerValue(response.headers, named: "Content-Length") {
headers["Content-Length"] = length
} else if let total = totalLength(from: headerValue(response.headers, named: "Content-Range")) {
headers["Content-Length"] = "\(total)"
} else {
headers["Content-Length"] = "0"
}
#if DEBUG
print("[DreamioStreamProxy] head status=\(response.statusCode) length=\(headers["Content-Length"] ?? "unknown")")
#endif
send(statusCode: 200, headers: headers, body: Data(), on: connection)
}
private func prefetch(after offset: Int64) {
let range = HTTPRange(start: offset, end: offset + prefetchLength - 1)
queue.async { [weak self] in
8 unmodified lines
}
private func fetch(range: HTTPRange) -> UpstreamResponse? {
fetch(request: upstreamRequest(for: range))
}
private func fetchHead() -> UpstreamResponse? {
var request = upstreamRequest(for: nil)
request.httpMethod = "HEAD"
return fetch(request: request)
}
private func fetch(request: URLRequest) -> UpstreamResponse? {
let semaphore = DispatchSemaphore(value: 0)
var result: UpstreamResponse?
URLSession.shared.dataTask(with: request) { data, response, _ in
if let http = response as? HTTPURLResponse {
result = UpstreamResponse(statusCode: http.statusCode, headers: http.allHeaderFields, data: data ?? Data())
}
semaphore.signal()
}.resume()
21 unmodified lines
"Accept-Ranges": "none",
"Connection": "close"
]
if let contentType = headerValue(upstreamHeaders, named: "Content-Type") {
headers["Content-Type"] = contentType
}
send(statusCode: statusCode, headers: headers, body: data, on: connection)
11 unmodified lines
connection.send(content: response, completion: .contentProcessed { _ in connection.cancel() })
}
private func headerValue(_ headers: [AnyHashable: Any], named name: String) -> String? {
headers.first { key, _ in
String(describing: key).caseInsensitiveCompare(name) == .orderedSame
}?.value as? String
}
private func totalLength(from contentRange: String?) -> Int64? {
guard let contentRange, let slash = contentRange.lastIndex(of: "/") else {
return nil
4 unmodified lines
}
private struct HTTPRequest {
let method: String
let path: String
let headers: [String: String]
init?(data: Data) {
1 unmodified line
let headerBlock = string.components(separatedBy: "\r\n\r\n").first else {
return nil
}
let lines = headerBlock.components(separatedBy: "\r\n")
guard let requestLine = lines.first else {
return nil
}
let parts = requestLine.split(separator: " ")
guard parts.count >= 2 else {
return nil
}
var headers: [String: String] = [:]
for line in lines.dropFirst() {
guard let separator = line.firstIndex(of: ":") else {
continue
}
1 unmodified line
let value = line[line.index(after: separator)...].trimmingCharacters(in: .whitespacesAndNewlines)
headers[key] = value
}
method = String(parts[0]).uppercased()
path = String(parts[1])
self.headers = headers
}
}

Related issues or PRs

Follow-up Beads issue: dreamio-6bv.