~ knotless Search/
↑↓ move↵ open⇧↵ all results

geesawra.industries/hydraulophone

An unofficial YouTube Music app for iPhone, iPad, and Mac.

0
0
main
401 lines · 14.9 KB Raw History
import CryptoKit
import Foundation
import OSLog

public final class CachedHLSPackage: Sendable {
  public let url: URL
  public let duration: TimeInterval?
  public let bitrate: Int?
  public let loudness: AudioLoudnessMetadata?
  public let isLocalPackage: Bool
  public let trackID: String
  public let encodedByteCount: Int64
  public let song: Song?
  fileprivate let packageLocation: URL
  private let cache: TrackCacheStore
  private let key: String

  init(url: URL, entry: HLSCacheEntry, cache: TrackCacheStore) {
    packageLocation = url
    self.url = entry.localPackage == true ? url.appendingPathComponent("audio.aac") : url
    duration = entry.duration
    bitrate = entry.bitrate
    loudness = entry.loudness
    isLocalPackage = entry.localPackage == true
    trackID = entry.trackID
    encodedByteCount = entry.bytes
    song = entry.song
    self.cache = cache
    key = entry.key
  }

  deinit {
    let cache = cache
    let key = key
    Task { await cache.releaseHLSPackage(key) }
  }
}

struct HLSCacheEntry: Codable {
  var schema = 1
  let trackID: String
  var song: Song? = nil
  let bookmark: Data
  let duration: TimeInterval?
  let bitrate: Int?
  var loudness: AudioLoudnessMetadata? = nil
  let localPackage: Bool?
  var bytes: Int64
  var complete: Bool
  var accessed = Date()
  var key: String {
    "hls-" + SHA256.hash(data: Data(trackID.utf8)).map { String(format: "%02x", $0) }.joined()
  }
}

extension TrackCacheStore {
  /// Copies a completed local AAC cache package into this store's own package
  /// index. The source package remains leased for the entire copy operation.
  public func promoteLocalAudioPackage(
    from source: CachedHLSPackage, trackID: String, song: Song? = nil
  ) throws -> Bool {
    load()
    guard source.isLocalPackage, source.trackID == trackID,
      source.encodedByteCount >= 7, source.encodedByteCount <= min(limit, 2_000_000_000),
      Self.isValidLocalPackage(source.packageLocation),
      Self.packageBytes(source.packageLocation) == source.encodedByteCount,
      let expectedGeneration = hlsWriteGeneration(
        trackID: trackID, estimatedBytes: source.encodedByteCount),
      let destination = beginLocalAudioFill(
        trackID: trackID, generation: expectedGeneration, song: song ?? source.song,
        loudness: source.loudness)
    else { return false }

    let sourceAudio = source.packageLocation.appendingPathComponent("audio.aac")
    let destinationAudio = destination.appendingPathComponent("audio.aac")
    do {
      guard
        FileManager.default.createFile(
          atPath: destinationAudio.path, contents: nil,
          attributes: [.protectionKey: FileProtectionType.completeUntilFirstUserAuthentication])
      else { throw CocoaError(.fileWriteUnknown) }
      let input = try FileHandle(forReadingFrom: sourceAudio)
      let output = try FileHandle(forWritingTo: destinationAudio)
      defer {
        try? input.close()
        try? output.close()
      }
      var copied: Int64 = 0
      while copied < source.encodedByteCount {
        try Task.checkCancellation()
        let count = min(1_048_576, Int(source.encodedByteCount - copied))
        let data = try input.read(upToCount: count) ?? Data()
        guard data.count == count else { throw CocoaError(.fileReadCorruptFile) }
        try output.write(contentsOf: data)
        copied += Int64(data.count)
      }
      guard (try input.read(upToCount: 1) ?? Data()).isEmpty else {
        throw CocoaError(.fileReadCorruptFile)
      }
      try output.synchronize()
      try Task.checkCancellation()
      guard Self.isValidLocalPackage(destination),
        Self.packageBytes(destination) == source.encodedByteCount,
        finishLocalAudioFill(
          trackID: trackID, location: destination, duration: source.duration,
          bitrate: source.bitrate, generation: expectedGeneration,
          loudness: source.loudness, song: song ?? source.song)
      else {
        discardIncompleteHLS(location: destination)
        return false
      }
      return true
    } catch is CancellationError {
      discardIncompleteHLS(location: destination)
      throw CancellationError()
    } catch {
      discardIncompleteHLS(location: destination)
      return false
    }
  }

  public func hlsWriteGeneration(trackID: String, estimatedBytes: Int64) -> UInt64? {
    load()
    guard limit > 0, estimatedBytes > 0, estimatedBytes <= limit,
      !hasCompleteHigh(trackID: trackID),
      !hlsEntries.values.contains(where: { $0.trackID == trackID })
    else { return nil }
    return generation
  }

  public func completedHLS(trackID: String) -> CachedHLSPackage? {
    load()
    guard limit > 0,
      var entry = hlsEntries.values.first(where: { $0.complete && $0.trackID == trackID }),
      !hlsPendingRemoval.contains(entry.key),
      let url = Self.hlsLocation(bookmark: entry.bookmark)
    else { return nil }
    guard FileManager.default.fileExists(atPath: url.path),
      entry.localPackage != true || Self.isValidLocalPackage(url)
    else {
      remove(entry.key)
      return nil
    }
    entry.accessed = Date()
    hlsEntries[entry.key] = entry
    try? persistHLS(entry)
    hlsReaders[entry.key, default: 0] += 1
    return CachedHLSPackage(url: url, entry: entry, cache: self)
  }

  /// Creates a temporary AAC package inside the shared cache root.
  public func beginLocalAudioFill(
    trackID: String, generation expectedGeneration: UInt64,
    song: Song? = nil, loudness: AudioLoudnessMetadata? = nil
  ) -> URL? {
    load()
    guard generation == expectedGeneration, limit > 0,
      song?.id == trackID,
      !hasCompleteHigh(trackID: trackID),
      !hlsEntries.values.contains(where: { $0.trackID == trackID })
    else { return nil }
    let digest = SHA256.hash(data: Data(trackID.utf8))
    let key = "hls-" + digest.map { String(format: "%02x", $0) }.joined()
    let root = folder(key)
    try? FileManager.default.removeItem(at: root)
    let package = root.appendingPathComponent(
      "package-\(UUID().uuidString).aac-package", isDirectory: true)
    do {
      try FileManager.default.createDirectory(
        at: package, withIntermediateDirectories: true,
        attributes: [.protectionKey: FileProtectionType.completeUntilFirstUserAuthentication])
      guard let bookmark = try? package.bookmarkData(options: .minimalBookmark) else {
        throw CocoaError(.fileWriteUnknown)
      }
      let entry = HLSCacheEntry(
        trackID: trackID, song: song, bookmark: bookmark, duration: nil, bitrate: nil,
        loudness: loudness, localPackage: true, bytes: 0, complete: false)
      hlsEntries[key] = entry
      try persistHLS(entry)
      return package
    } catch {
      hlsEntries.removeValue(forKey: key)
      try? FileManager.default.removeItem(at: root)
      return nil
    }
  }

  /// Indexes an extracted, complete AAC bitstream with no remote URLs.
  public func finishLocalAudioFill(
    trackID: String, location: URL, duration: TimeInterval?, bitrate: Int?,
    generation expectedGeneration: UInt64, loudness: AudioLoudnessMetadata? = nil,
    song: Song? = nil
  ) -> Bool {
    load()
    let digest = SHA256.hash(data: Data(trackID.utf8))
    let key = "hls-" + digest.map { String(format: "%02x", $0) }.joined()
    guard generation == expectedGeneration, limit > 0,
      !hasCompleteHigh(trackID: trackID),
      location.standardizedFileURL
        .deletingLastPathComponent() == folder(key).standardizedFileURL,
      location.pathExtension == "aac-package",
      let previous = hlsEntries[key], previous.trackID == trackID, !previous.complete,
      Self.hlsLocation(bookmark: previous.bookmark) == location,
      Self.isValidLocalPackage(location),
      Self.packageBytes(location) <= limit,
      let bookmark = try? location.bookmarkData(options: .minimalBookmark)
    else {
      discardIncompleteHLS(location: location)
      return false
    }
    let entry = HLSCacheEntry(
      trackID: trackID, song: song ?? previous.song, bookmark: bookmark,
      duration: duration, bitrate: bitrate,
      loudness: loudness ?? previous.loudness,
      localPackage: true, bytes: Self.packageBytes(location), complete: true)
    do {
      hlsEntries[key] = entry
      try persistHLS(entry)
      trim()
      return hlsEntries[key] != nil
    } catch {
      remove(key)
      return false
    }
  }

  /// The system-provided HLS package stays in its original location, as
  /// required by AVFoundation. Only locations supplied by its download delegate
  /// enter here. A local file bookmark also supports system-managed locations
  /// outside the app container; no network URL is persisted in our index.
  public func recordHLS(
    trackID: String, location: URL, duration: TimeInterval?, bitrate: Int?,
    complete: Bool, generation expectedGeneration: UInt64,
    loudness: AudioLoudnessMetadata? = nil, song: Song? = nil
  ) -> Bool {
    load()
    guard Self.isHLSPackage(location), song?.id == trackID else { return false }
    // willDownloadTo can precede creation of the system-managed directory.
    guard FileManager.default.fileExists(atPath: location.path) else { return !complete }
    guard let bookmark = try? location.bookmarkData(options: .minimalBookmark) else {
      return false
    }
    let bytes = Self.packageBytes(location)
    guard generation == expectedGeneration, limit > 0, bytes <= limit,
      !hasCompleteHigh(trackID: trackID)
    else {
      Logger(subsystem: "com.hydraulophone", category: "track-cache").notice(
        "hls_cache rejected=policy")
      discardHLS(location: location)
      return false
    }
    let previousAtLocation = hlsEntries.values.first {
      Self.hlsLocation(bookmark: $0.bookmark) == location
    }
    let entry = HLSCacheEntry(
      trackID: trackID, song: song ?? previousAtLocation?.song,
      bookmark: bookmark,
      duration: duration, bitrate: bitrate,
      loudness: loudness ?? previousAtLocation?.loudness, localPackage: false,
      bytes: bytes, complete: complete)
    // A queued progress callback must not downgrade a finished package.
    if let previous = hlsEntries[entry.key],
      Self.hlsLocation(bookmark: previous.bookmark) == location,
      previous.complete, !complete
    {
      return true
    }
    if let previous = hlsEntries[entry.key],
      Self.hlsLocation(bookmark: previous.bookmark) != location
    {
      guard hlsReaders[entry.key, default: 0] == 0 else {
        discardHLS(location: location)
        return false
      }
      remove(entry.key)
    }
    do {
      hlsEntries[entry.key] = entry
      try persistHLS(entry)
      trim()
      return hlsEntries[entry.key] != nil
    } catch {
      Logger(subsystem: "com.hydraulophone", category: "track-cache").notice(
        "hls_cache rejected=index code=\((error as NSError).code)")
      remove(entry.key)
      return false
    }
  }

  public func discardHLS(location: URL) {
    guard Self.isHLSPackage(location) else { return }
    if let entry = hlsEntries.values.first(where: {
      Self.hlsLocation(bookmark: $0.bookmark) == location
    }) {
      remove(entry.key)
    } else {
      try? FileManager.default.removeItem(at: location)
    }
  }

  /// Cancels abandoned partial fills without deleting an indexed completed
  /// package that may be awaiting reader or catalog restoration.
  public func discardIncompleteHLS(location: URL) {
    guard Self.isHLSPackage(location) else { return }
    if hlsEntries.values.contains(where: {
      $0.complete && Self.hlsLocation(bookmark: $0.bookmark) == location
    }) {
      return
    }
    discardHLS(location: location)
  }

  func releaseHLSPackage(_ key: String) {
    hlsReaders[key] = max(0, hlsReaders[key, default: 0] - 1)
    if hlsReaders[key] == 0 {
      hlsReaders.removeValue(forKey: key)
      if hlsPendingRemoval.contains(key) { remove(key) }
    }
    trim()
  }

  func loadHLSEntry(at folder: URL) -> Bool {
    guard folder.lastPathComponent.hasPrefix("hls-") else { return false }
    let indexURL = folder.appendingPathComponent("hls.json")
    guard
      let indexValues = try? indexURL.resourceValues(
        forKeys: [.isRegularFileKey, .fileSizeKey, .isSymbolicLinkKey]),
      indexValues.isRegularFile == true, indexValues.isSymbolicLink != true,
      let indexSize = indexValues.fileSize, indexSize < 16_384,
      let data = try? Data(contentsOf: indexURL),
      var entry = try? JSONDecoder().decode(HLSCacheEntry.self, from: data), entry.schema == 1,
      entry.key == folder.lastPathComponent,
      let location = Self.hlsLocation(bookmark: entry.bookmark)
    else {
      try? FileManager.default.removeItem(at: folder)
      return true
    }
    guard entry.complete, FileManager.default.fileExists(atPath: location.path),
      entry.localPackage != true || Self.isValidLocalPackage(location)
    else {
      try? FileManager.default.removeItem(at: location)
      try? FileManager.default.removeItem(at: folder)
      return true
    }
    entry.bytes = Self.packageBytes(location)
    hlsEntries[entry.key] = entry
    return true
  }

  func persistHLS(_ entry: HLSCacheEntry) throws {
    try FileManager.default.createDirectory(
      at: folder(entry.key), withIntermediateDirectories: true)
    try JSONEncoder().encode(entry).write(
      to: folder(entry.key).appendingPathComponent("hls.json"),
      options: [.atomic, .completeFileProtectionUntilFirstUserAuthentication])
  }

  private static func isHLSPackage(_ url: URL) -> Bool {
    url.isFileURL && (url.pathExtension == "movpkg" || url.pathExtension == "aac-package")
      && (try? url.resourceValues(forKeys: [.isSymbolicLinkKey]).isSymbolicLink) != true
  }

  static func isValidLocalPackage(_ url: URL) -> Bool {
    guard isHLSPackage(url), url.pathExtension == "aac-package",
      let audio = try? url.appendingPathComponent("audio.aac").resourceValues(
        forKeys: [.isRegularFileKey, .fileSizeKey, .isSymbolicLinkKey]),
      audio.isRegularFile == true, audio.isSymbolicLink != true,
      let size = audio.fileSize, size >= 7, size <= 2_000_000_000
    else { return false }
    guard let handle = try? FileHandle(forReadingFrom: url.appendingPathComponent("audio.aac"))
    else { return false }
    defer { try? handle.close() }
    guard let header = try? handle.read(upToCount: 7), header.count == 7 else { return false }
    return header[0] == 0xff && header[1] & 0xf6 == 0xf0
  }

  static func hlsLocation(bookmark: Data) -> URL? {
    var stale = false
    guard
      let url = try? URL(
        resolvingBookmarkData: bookmark, options: .withoutUI,
        bookmarkDataIsStale: &stale), isHLSPackage(url)
    else { return nil }
    return url
  }

  private static func packageBytes(_ url: URL) -> Int64 {
    let keys: [URLResourceKey] = [.isRegularFileKey, .fileSizeKey, .isSymbolicLinkKey]
    guard let enumerator = FileManager.default.enumerator(at: url, includingPropertiesForKeys: keys)
    else { return 0 }
    var total: Int64 = 0
    for case let item as URL in enumerator {
      guard let values = try? item.resourceValues(forKeys: Set(keys)),
        values.isSymbolicLink != true,
        values.isRegularFile == true
      else { continue }
      total += Int64(values.fileSize ?? 0)
    }
    return total
  }
}