Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Core/Audio/ByteSource.swift3// Description: Byte sources of the decoder: local file, and progressive HTTP source with range requests and a sparse cache.4import Foundation5import CryptoKit67public enum SourceError: Error, Sendable, Equatable {8 case cancelled9 case interrupted // a seek asked the blocked read to give up10 case stalled // no byte arrived for too long11 case unavailable(Int) // the cluster answered with this HTTP status12 case unauthenticated13 case corrupt // the complete file does not match its announced checksum14 case io(Int32)15}1617/// Random-access bytes of one audio file. `read` blocks until the bytes are there (they may still be on their way18/// from the cluster) and is called from a decoding thread, never from the main thread.19public protocol ByteSource: AnyObject, Sendable {20 var length: Int64 { get }21 var isLocal: Bool { get }22 /// Fraction of the file present locally (1 for a local file).23 var bufferedFraction: Double { get }24 func read(at offset: Int64, into buffer: UnsafeMutableRawPointer, count: Int) throws -> Int25 /// Makes a read that is currently blocked throw `.interrupted`; reads work again after `resumeReads()`.26 func interruptReads()27 func resumeReads()28 func cancel()29}3031/// A file on disk: a download, a complete streaming-cache file, or a file opened by the listener.32public final class LocalFileSource: ByteSource, @unchecked Sendable {33 private let fd: Int3234 public let length: Int6435 public let url: URL36 public var isLocal: Bool { true }37 public var bufferedFraction: Double { 1 }3839 public init(url: URL) throws {40 self.url = url41 fd = open(url.path, O_RDONLY)42 guard fd >= 0 else { throw SourceError.io(errno) }43 var st = stat()44 guard fstat(fd, &st) == 0 else { let e = errno; close(fd); throw SourceError.io(e) }45 length = Int64(st.st_size)46 }47 deinit { close(fd) }4849 public func read(at offset: Int64, into buffer: UnsafeMutableRawPointer, count: Int) throws -> Int {50 var done = 051 while done < count {52 let n = pread(fd, buffer + done, count - done, off_t(offset) + off_t(done))53 if n < 0 { if errno == EINTR { continue }; throw SourceError.io(errno) }54 if n == 0 { break }55 done += n56 }57 return done58 }59 public func interruptReads() {}60 public func resumeReads() {}61 public func cancel() {}62}6364/// Sorted, non-overlapping half-open byte ranges.65public struct ByteRangeSet: Sendable, Equatable {66 public private(set) var ranges: [Range<Int64>] = []67 public init() {}68 public init(_ pairs: [[Int64]]) { for p in pairs where p.count == 2 && p[1] > p[0] { insert(p[0]..<p[1]) } }69 public var pairs: [[Int64]] { ranges.map { [$0.lowerBound, $0.upperBound] } }70 public var total: Int64 { ranges.reduce(0) { $0 + Int64($1.count) } }7172 public mutating func insert(_ r: Range<Int64>) {73 guard !r.isEmpty else { return }74 var lo = r.lowerBound, hi = r.upperBound75 var out: [Range<Int64>] = []76 var placed = false77 for x in ranges {78 if x.upperBound < lo { out.append(x) }79 else if x.lowerBound > hi { if !placed { out.append(lo..<hi); placed = true }; out.append(x) }80 else { lo = min(lo, x.lowerBound); hi = max(hi, x.upperBound) }81 }82 if !placed { out.append(lo..<hi) }83 ranges = out84 }85 /// Number of bytes available without a gap starting at `offset`.86 public func contiguous(from offset: Int64) -> Int64 {87 for x in ranges where x.contains(offset) { return x.upperBound - offset }88 return 089 }90 /// First byte at or after `offset` that is missing, below `limit`; nil when everything from `offset` is there.91 public func firstMissing(from offset: Int64, limit: Int64) -> Int64? {92 var p = offset93 for x in ranges where x.upperBound > p { if x.lowerBound > p { break }; p = x.upperBound }94 return p < limit ? p : nil95 }96 /// End (exclusive) of the hole that starts at `start`.97 public func holeEnd(from start: Int64, limit: Int64) -> Int64 {98 for x in ranges where x.lowerBound > start { return min(x.lowerBound, limit) }99 return limit100 }101}102103/// A file being streamed from the cluster. Bytes are fetched with HTTP range requests into a sparse cache file and104/// served to the decoder as soon as they are there, so playback starts after the first few hundred kilobytes and a105/// seek jumps straight to the bytes it needs. Network cuts are retried; what was received is never fetched twice.106/// When the last byte is in, the file is checked against its SHA-256 and marked complete.107public final class ProgressiveHTTPSource: ByteSource, @unchecked Sendable {108 public let length: Int64109 public var isLocal: Bool { false }110 private let makeRequest: @Sendable () throws -> URLRequest111 private let etag: String?112 private let expectedSHA: String?113 private let cacheURL: URL, rangesURL: URL, doneURL: URL114 private let cond = NSCondition()115 private var have = ByteRangeSet()116 private var fd: Int32 = -1117 private var cancelled = false, interrupted = false, complete = false118 private var failure: SourceError?119 private var wanted: Int64? // offset a reader is waiting for120 private var segmentStart: Int64 = 0, head: Int64 = 0121 private var restart = false122 private var lastProgress = Date()123 private var task: Task<Void, Never>?124 private var onOffline: (@Sendable () -> Void)?125 private static let seekWindow: Int64 = 384 * 1024126 public var stallTimeout: TimeInterval = 45127128 public init(length: Int64, cacheURL: URL, rangesURL: URL, doneURL: URL, etag: String?, sha256: String?, onOffline: (@Sendable () -> Void)? = nil,129 request: @escaping @Sendable () throws -> URLRequest) throws {130 self.length = length; self.cacheURL = cacheURL; self.rangesURL = rangesURL; self.doneURL = doneURL; self.etag = etag; self.expectedSHA = sha256131 self.makeRequest = request; self.onOffline = onOffline132 fd = open(cacheURL.path, O_RDWR | O_CREAT, 0o600)133 guard fd >= 0 else { throw SourceError.io(errno) }134 var st = stat(); fstat(fd, &st)135 if Int64(st.st_size) == length, let data = try? Data(contentsOf: rangesURL), let pairs = try? JSONDecoder().decode([[Int64]].self, from: data) {136 have = ByteRangeSet(pairs.filter { $0.count == 2 && $0[0] >= 0 && $0[1] <= length })137 } else { ftruncate(fd, off_t(length)) }138 complete = have.total >= length && length > 0139 task = Task.detached(priority: .userInitiated) { [weak self] in await self?.fetchLoop() }140 }141142 deinit { task?.cancel(); if fd >= 0 { close(fd) } }143144 public var bufferedFraction: Double { cond.lock(); defer { cond.unlock() }; return length > 0 ? Double(have.total) / Double(length) : 0 }145 public var isComplete: Bool { cond.lock(); defer { cond.unlock() }; return complete }146147 public func read(at offset: Int64, into buffer: UnsafeMutableRawPointer, count: Int) throws -> Int {148 let need = Int(min(Int64(count), length - offset))149 guard need > 0 else { return 0 }150 cond.lock()151 var waitedSince: Date?152 while true {153 if cancelled { cond.unlock(); throw SourceError.cancelled }154 if interrupted { cond.unlock(); throw SourceError.interrupted }155 if have.contiguous(from: offset) >= Int64(need) { break }156 if let failure { cond.unlock(); throw failure }157 if waitedSince == nil { waitedSince = Date(); lastProgress = Date() }158 if Date().timeIntervalSince(lastProgress) > stallTimeout { cond.unlock(); throw SourceError.stalled }159 wanted = offset160 // Far from where the fetch is writing: ask it to jump here instead of waiting for it to arrive.161 if !(offset >= segmentStart && offset <= head + Self.seekWindow) { restart = true }162 cond.wait(until: Date().addingTimeInterval(0.25))163 }164 wanted = nil165 cond.unlock()166 var done = 0167 while done < need {168 let n = pread(fd, buffer + done, need - done, off_t(offset) + off_t(done))169 if n < 0 { if errno == EINTR { continue }; throw SourceError.io(errno) }170 if n == 0 { break }171 done += n172 }173 return done174 }175176 public func interruptReads() { cond.lock(); interrupted = true; cond.broadcast(); cond.unlock() }177 public func resumeReads() { cond.lock(); interrupted = false; cond.unlock() }178 public func cancel() {179 cond.lock(); cancelled = true; cond.broadcast(); let pairs = have.pairs; let done = complete; cond.unlock()180 task?.cancel()181 if !done, let data = try? JSONEncoder().encode(pairs) { try? data.write(to: rangesURL, options: .atomic) }182 }183184 private func nextSegment() -> Range<Int64>? {185 cond.lock(); defer { cond.unlock() }186 restart = false187 let from = wanted ?? 0188 guard let start = have.firstMissing(from: from, limit: length) ?? have.firstMissing(from: 0, limit: length) else { return nil }189 segmentStart = start; head = start190 return start..<have.holeEnd(from: start, limit: length)191 }192193 private func fetchLoop() async {194 var failures = 0195 while !Task.isCancelled {196 guard let segment = nextSegment() else { await finish(); return }197 do {198 var request = try makeRequest()199 request.setValue("bytes=\(segment.lowerBound)-\(segment.upperBound - 1)", forHTTPHeaderField: "Range")200 if let etag { request.setValue(etag, forHTTPHeaderField: "If-Range") }201 request.timeoutInterval = 20202 let stream = HTTPByteStream()203 let headResponse = try await stream.start(request)204 switch headResponse.status {205 case 206: break206 case 401: stream.cancel(); fail(.unauthenticated); return207 case 200: // If-Range did not match: the file changed on the cluster while we were reading it208 stream.cancel(); fail(.unavailable(409)); return209 case 502, 503, 504: stream.cancel(); throw URLError(.cannotConnectToHost)210 default: stream.cancel(); fail(.unavailable(headResponse.status)); return211 }212 var position = segment.lowerBound213 var jump = false214 try await withTaskCancellationHandler {215 for try await chunk in stream.body {216 let written = chunk.withUnsafeBytes { pwrite(fd, $0.baseAddress, chunk.count, off_t(position)) }217 guard written == chunk.count else { throw SourceError.io(errno) }218 jump = received(at: position, count: chunk.count)219 position += Int64(chunk.count)220 if jump { stream.cancel(); break }221 }222 } onCancel: { stream.cancel() }223 failures = 0224 } catch {225 if Task.isCancelled { return }226 if let e = error as? SourceError { fail(e); return }227 if let e = error as? APIError, e == .unauthenticated { fail(.unauthenticated); return }228 // Network trouble: keep what we have, tell the app, try again shortly. Readers wait (and time out on their own).229 failures += 1230 if failures == 1 { onOffline?() }231 try? await Task.sleep(nanoseconds: UInt64(min(8.0, 0.5 * Double(1 << min(failures, 4))) * 1_000_000_000))232 }233 }234 }235236 /// Records bytes just written to the cache file and wakes readers. Returns true when a reader asked the fetch to jump elsewhere.237 private func received(at position: Int64, count: Int) -> Bool {238 cond.lock(); defer { cond.unlock() }239 have.insert(position..<(position + Int64(count)))240 head = position + Int64(count); lastProgress = Date()241 cond.broadcast()242 return restart243 }244 private func markComplete() { cond.lock(); complete = true; cond.broadcast(); cond.unlock() }245246 private func fail(_ e: SourceError) { cond.lock(); failure = e; cond.broadcast(); cond.unlock() }247248 private func finish() async {249 fsync(fd)250 if let want = expectedSHA, !want.isEmpty {251 let got = (try? StorageManager.sha256(of: cacheURL)) ?? ""252 if got != want {253 try? FileManager.default.removeItem(at: cacheURL); try? FileManager.default.removeItem(at: rangesURL)254 fail(.corrupt); return255 }256 }257 markComplete()258 try? FileManager.default.removeItem(at: rangesURL)259 FileManager.default.createFile(atPath: doneURL.path, contents: nil)260 }261}262