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

205 lines
5.4 KiB
Swift
Raw Normal View History

import AVKit
2021-10-25 13:55:41 +05:30
import Defaults
import Foundation
2021-10-17 04:18:58 +05:30
import Siesta
extension PlayerModel {
var currentVideo: Video? {
currentItem?.video
}
func play(_ videos: [Video], shuffling: Bool = false, inNavigationView: Bool = false) {
let videosToPlay = shuffling ? videos.shuffled() : videos
guard let first = videosToPlay.first else {
return
}
enqueueVideo(first, prepending: true) { _, item in
self.advanceToItem(item)
}
videosToPlay.dropFirst().reversed().forEach { video in
enqueueVideo(video, prepending: true) { _, item in
if item.video == first {
self.advanceToItem(item)
}
}
}
if inNavigationView {
playerNavigationLinkActive = true
} else {
show()
}
}
func playNext(_ video: Video) {
enqueueVideo(video, prepending: true) { _, item in
2021-10-17 04:18:58 +05:30
if self.currentItem.isNil {
self.advanceToItem(item)
}
}
}
2021-10-24 14:46:04 +05:30
func playNow(_ video: Video, at time: TimeInterval? = nil) {
if playingInPictureInPicture, closePiPOnNavigation {
closePiP()
}
prepareCurrentItemForHistory()
enqueueVideo(video, prepending: true) { _, item in
2021-10-24 14:46:04 +05:30
self.advanceToItem(item, at: time)
}
}
2021-10-24 14:46:04 +05:30
func playItem(_ item: PlayerQueueItem, video: Video? = nil, at time: TimeInterval? = nil) {
if !playingInPictureInPicture {
player.replaceCurrentItem(with: nil)
}
comments.reset()
stream = nil
currentItem = item
2021-10-24 14:46:04 +05:30
if !time.isNil {
currentItem.playbackTime = .secondsInDefaultTimescale(time!)
2021-10-24 14:46:04 +05:30
} else if currentItem.playbackTime.isNil {
currentItem.playbackTime = .zero
}
if video != nil {
currentItem.video = video!
}
2021-12-18 01:31:05 +05:30
preservedTime = currentItem.playbackTime
restoreLoadedChannel()
DispatchQueue.main.async { [weak self] in
guard let video = self?.currentVideo else {
return
}
self?.loadAvailableStreams(video)
}
}
2021-12-19 22:26:47 +05:30
func preferredStream(_ streams: [Stream]) -> Stream? {
2021-11-06 01:05:27 +05:30
let quality = Defaults[.quality]
var streams = streams
2021-11-04 05:10:01 +05:30
if let id = Defaults[.playerInstanceID] {
2021-11-06 01:05:27 +05:30
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-04 05:10:01 +05:30
}
}
func advanceToNextItem() {
prepareCurrentItemForHistory()
if let nextItem = queue.first {
advanceToItem(nextItem)
}
}
2021-10-24 14:46:04 +05:30
func advanceToItem(_ newItem: PlayerQueueItem, at time: TimeInterval? = nil) {
prepareCurrentItemForHistory()
remove(newItem)
currentItem = newItem
player.pause()
accounts.api.loadDetails(newItem) { newItem in
self.playItem(newItem, video: newItem.video, at: time)
}
}
@discardableResult func remove(_ item: PlayerQueueItem) -> PlayerQueueItem? {
2021-10-27 04:29:59 +05:30
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 {
player.currentItem == item
}
@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)
if play {
currentItem = item
// pause playing current video as it's going to be replaced with next one
player.pause()
}
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 prepareCurrentItemForHistory(finished: Bool = false) {
if !currentItem.isNil, Defaults[.saveHistory] {
if let video = currentVideo, !historyVideos.contains(where: { $0 == video }) {
historyVideos.append(video)
}
updateWatch(finished: finished)
}
}
2021-10-14 03:35:19 +05:30
func playHistory(_ item: PlayerQueueItem) {
var time = item.playbackTime
if item.shouldRestartPlaying {
time = .zero
}
let newItem = enqueueVideo(item.video, atTime: time, prepending: true)
2021-10-14 03:35:19 +05:30
advanceToItem(newItem!)
}
func removeQueueItems() {
queue.removeAll()
}
}