yattee/Model/Player/PlayerQueue.swift

240 lines
6.5 KiB
Swift
Raw Normal View History

import AVFoundation
2021-10-25 08:25:41 +00:00
import Defaults
import Foundation
2021-10-16 22:48:58 +00:00
import Siesta
extension PlayerModel {
var currentVideo: Video? {
currentItem?.video
}
func playAll(_ videos: [Video]) {
let first = videos.first
videos.forEach { video in
enqueueVideo(video) { _, item in
if item.video == first {
self.advanceToItem(item)
}
}
}
}
func playNext(_ video: Video) {
enqueueVideo(video, prepending: true) { _, item in
2021-10-16 22:48:58 +00:00
if self.currentItem.isNil {
self.advanceToItem(item)
}
}
}
2021-10-24 09:16:04 +00:00
func playNow(_ video: Video, at time: TimeInterval? = nil) {
addCurrentItemToHistory()
enqueueVideo(video, prepending: true) { _, item in
2021-10-24 09:16:04 +00:00
self.advanceToItem(item, at: time)
}
}
2021-10-24 09:16:04 +00:00
func playItem(_ item: PlayerQueueItem, video: Video? = nil, at time: TimeInterval? = nil) {
2021-12-04 19:35:41 +00:00
comments.clear()
currentItem = item
2021-10-24 09:16:04 +00:00
if !time.isNil {
currentItem.playbackTime = .secondsInDefaultTimescale(time!)
2021-10-24 09:16:04 +00:00
} else if currentItem.playbackTime.isNil {
currentItem.playbackTime = .zero
}
if video != nil {
currentItem.video = video!
}
savedTime = currentItem.playbackTime
loadAvailableStreams(currentVideo!) { streams in
2021-11-03 23:40:01 +00:00
guard let stream = self.preferredStream(streams) else {
return
}
self.streamSelection = stream
self.playStream(
stream,
of: self.currentVideo!,
preservingTime: !self.currentItem.playbackTime.isNil
)
}
}
2021-11-03 23:40:01 +00:00
private func preferredStream(_ streams: [Stream]) -> Stream? {
2021-11-05 19:35:27 +00:00
let quality = Defaults[.quality]
var streams = streams
2021-11-03 23:40:01 +00:00
if let id = Defaults[.playerInstanceID] {
2021-11-05 19:35:27 +00:00
streams = streams.filter { $0.instance.id == id }
}
switch quality {
case .best:
return streams.first { $0.kind == .hls } ?? streams.first
default:
let sorted = streams.filter { $0.kind != .hls }.sorted { $0.resolution > $1.resolution }
return sorted.first(where: { $0.resolution.height <= quality.value.height })
2021-11-03 23:40:01 +00:00
}
}
func advanceToNextItem() {
addCurrentItemToHistory()
if let nextItem = queue.first {
advanceToItem(nextItem)
}
}
2021-10-24 09:16:04 +00:00
func advanceToItem(_ newItem: PlayerQueueItem, at time: TimeInterval? = nil) {
addCurrentItemToHistory()
remove(newItem)
accounts.api.loadDetails(newItem) { newItem in
self.playItem(newItem, video: newItem.video, at: time)
}
}
@discardableResult func remove(_ item: PlayerQueueItem) -> PlayerQueueItem? {
2021-10-26 22:59:59 +00:00
if let index = queue.firstIndex(where: { $0.videoID == item.videoID }) {
return queue.remove(at: index)
}
return nil
}
func resetQueue() {
DispatchQueue.main.async { [weak self] in
guard let self = self else {
return
}
self.currentItem = nil
self.stream = nil
self.removeQueueItems()
}
player.replaceCurrentItem(with: nil)
}
func isAutoplaying(_ item: AVPlayerItem) -> Bool {
2021-11-01 21:56:18 +00:00
player.currentItem == item && presentingPlayer
}
@discardableResult func enqueueVideo(
_ video: Video,
play: Bool = false,
atTime: CMTime? = nil,
prepending: Bool = false,
videoDetailsLoadHandler: @escaping (Video, PlayerQueueItem) -> Void = { _, _ in }
) -> PlayerQueueItem? {
let item = PlayerQueueItem(video, playbackTime: atTime)
queue.insert(item, at: prepending ? 0 : queue.endIndex)
accounts.api.loadDetails(item) { newItem in
videoDetailsLoadHandler(newItem.video, newItem)
if play {
self.playItem(newItem, video: video)
}
}
return item
}
func addCurrentItemToHistory() {
2021-11-05 19:57:22 +00:00
if let item = currentItem, Defaults[.saveHistory] {
2021-10-25 08:25:41 +00:00
addItemToHistory(item)
}
}
2021-10-25 08:25:41 +00:00
func addItemToHistory(_ item: PlayerQueueItem) {
2021-10-26 22:59:59 +00:00
if let index = history.firstIndex(where: { $0.video?.videoID == item.video?.videoID }) {
2021-10-25 08:25:41 +00:00
history.remove(at: index)
}
2021-10-25 08:25:41 +00:00
history.insert(currentItem, at: 0)
}
2021-10-13 22:05:19 +00:00
func playHistory(_ item: PlayerQueueItem) {
var time = item.playbackTime
if item.shouldRestartPlaying {
time = .zero
}
let newItem = enqueueVideo(item.video, atTime: time, prepending: true)
2021-10-13 22:05:19 +00:00
advanceToItem(newItem!)
if let historyItemIndex = history.firstIndex(of: item) {
history.remove(at: historyItemIndex)
}
}
@discardableResult func removeHistory(_ item: PlayerQueueItem) -> PlayerQueueItem? {
if let index = history.firstIndex(where: { $0 == item }) {
return history.remove(at: index)
}
return nil
}
func removeQueueItems() {
queue.removeAll()
}
func removeHistoryItems() {
history.removeAll()
}
2021-10-25 08:25:41 +00:00
func loadHistoryDetails() {
guard !accounts.current.isNil else {
return
}
queue = Defaults[.queue]
queue.forEach { item in
accounts.api.loadDetails(item) { newItem in
if let index = self.queue.firstIndex(where: { $0.id == item.id }) {
self.queue[index] = newItem
}
}
}
var savedHistory = Defaults[.history]
if let lastPlayed = Defaults[.lastPlayed] {
if let index = savedHistory.firstIndex(where: { $0.videoID == lastPlayed.videoID }) {
var updatedLastPlayed = savedHistory[index]
updatedLastPlayed.playbackTime = lastPlayed.playbackTime
updatedLastPlayed.videoDuration = lastPlayed.videoDuration
savedHistory.remove(at: index)
savedHistory.insert(updatedLastPlayed, at: 0)
} else {
savedHistory.insert(lastPlayed, at: 0)
}
Defaults[.lastPlayed] = nil
}
history = savedHistory
2021-10-25 08:25:41 +00:00
history.forEach { item in
accounts.api.loadDetails(item) { newItem in
if let index = self.history.firstIndex(where: { $0.id == item.id }) {
self.history[index] = newItem
}
}
}
}
}