1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-13 13:50:32 +05:30
yattee/Model/Stream.swift

126 lines
3.2 KiB
Swift
Raw Normal View History

2021-06-14 23:35:02 +05:30
import AVFoundation
2021-09-25 13:48:22 +05:30
import Defaults
2021-06-14 23:35:02 +05:30
import Foundation
// swiftlint:disable:next final_class
2021-10-17 04:18:58 +05:30
class Stream: Equatable, Hashable, Identifiable {
2021-09-25 13:48:22 +05:30
enum Resolution: String, CaseIterable, Comparable, Defaults.Serializable {
2021-10-17 04:18:58 +05:30
case hd1440p60, hd1440p, hd1080p60, hd1080p, hd720p60, hd720p, sd480p, sd360p, sd240p, sd144p, unknown
var name: String {
"\(height)p\(refreshRate != -1 ? ", \(refreshRate) fps" : "")"
}
2021-07-22 18:13:13 +05:30
var height: Int {
2021-10-17 04:18:58 +05:30
if self == .unknown {
return -1
}
let resolutionPart = rawValue.components(separatedBy: "p").first!
return Int(resolutionPart.components(separatedBy: CharacterSet.decimalDigits.inverted).joined())!
}
var refreshRate: Int {
if self == .unknown {
return -1
}
let refreshRatePart = rawValue.components(separatedBy: "p")[1]
return Int(refreshRatePart.components(separatedBy: CharacterSet.decimalDigits.inverted).joined()) ?? -1
2021-07-22 18:13:13 +05:30
}
2021-10-17 04:18:58 +05:30
static func from(resolution: String) -> Resolution {
allCases.first { "\($0)".contains(resolution) } ?? .unknown
2021-07-22 18:13:13 +05:30
}
static func < (lhs: Resolution, rhs: Resolution) -> Bool {
lhs.height < rhs.height
}
}
enum Kind: String, Comparable {
2021-10-17 04:18:58 +05:30
case stream, adaptive, hls
2021-07-22 18:13:13 +05:30
private var sortOrder: Int {
switch self {
2021-10-17 04:18:58 +05:30
case .hls:
2021-07-22 18:13:13 +05:30
return 0
2021-10-17 04:18:58 +05:30
case .stream:
2021-07-22 18:13:13 +05:30
return 1
2021-10-17 04:18:58 +05:30
case .adaptive:
return 2
2021-07-22 18:13:13 +05:30
}
}
static func < (lhs: Kind, rhs: Kind) -> Bool {
lhs.sortOrder < rhs.sortOrder
}
}
2021-10-17 04:18:58 +05:30
let id = UUID()
var instance: Instance!
var audioAsset: AVURLAsset!
var videoAsset: AVURLAsset!
var hlsURL: URL!
2021-06-14 23:35:02 +05:30
2021-10-17 04:18:58 +05:30
var resolution: Resolution!
var kind: Kind!
2021-06-14 23:35:02 +05:30
2021-10-17 04:18:58 +05:30
var encoding: String!
2021-06-14 23:35:02 +05:30
2021-10-17 04:18:58 +05:30
init(
instance: Instance? = nil,
audioAsset: AVURLAsset? = nil,
videoAsset: AVURLAsset? = nil,
hlsURL: URL? = nil,
resolution: Resolution? = nil,
kind: Kind = .hls,
encoding: String? = nil
) {
self.instance = instance
2021-06-14 23:35:02 +05:30
self.audioAsset = audioAsset
self.videoAsset = videoAsset
2021-10-17 04:18:58 +05:30
self.hlsURL = hlsURL
2021-06-14 23:35:02 +05:30
self.resolution = resolution
2021-07-22 18:13:13 +05:30
self.kind = kind
2021-06-14 23:35:02 +05:30
self.encoding = encoding
}
2021-10-17 04:18:58 +05:30
var quality: String {
kind == .hls ? "adaptive (HLS)" : "\(resolution.name) \(kind == .stream ? "(\(kind.rawValue))" : "")"
}
2021-06-14 23:35:02 +05:30
var description: String {
2021-10-17 04:18:58 +05:30
"\(quality) - \(instance?.description ?? "")"
2021-06-14 23:35:02 +05:30
}
2021-06-15 22:05:21 +05:30
var assets: [AVURLAsset] {
[audioAsset, videoAsset]
}
2021-10-17 04:18:58 +05:30
var videoAssetContainsAudio: Bool {
assets.dropFirst().allSatisfy { $0.url == assets.first!.url }
2021-07-22 18:13:13 +05:30
}
2021-10-17 04:18:58 +05:30
var singleAssetURL: URL? {
if kind == .hls {
return hlsURL
} else if videoAssetContainsAudio {
return videoAsset.url
}
return nil
}
2021-06-14 23:35:02 +05:30
static func == (lhs: Stream, rhs: Stream) -> Bool {
2021-10-17 04:18:58 +05:30
lhs.id == rhs.id
2021-07-22 18:13:13 +05:30
}
func hash(into hasher: inout Hasher) {
2021-10-17 04:18:58 +05:30
hasher.combine(videoAsset?.url)
hasher.combine(audioAsset?.url)
hasher.combine(hlsURL)
}
2021-06-14 23:35:02 +05:30
}