2021-10-05 20:20:09 +00:00
|
|
|
|
import AVKit
|
2021-12-26 21:14:46 +00:00
|
|
|
|
import CoreData
|
2022-01-02 19:43:30 +00:00
|
|
|
|
#if os(iOS)
|
|
|
|
|
import CoreMotion
|
|
|
|
|
#endif
|
2021-10-05 20:20:09 +00:00
|
|
|
|
import Defaults
|
2021-06-14 18:05:02 +00:00
|
|
|
|
import Foundation
|
2021-06-15 16:35:21 +00:00
|
|
|
|
import Logging
|
2021-10-28 20:18:23 +00:00
|
|
|
|
import MediaPlayer
|
2021-10-16 22:48:58 +00:00
|
|
|
|
import Siesta
|
2021-10-23 16:49:45 +00:00
|
|
|
|
import SwiftUI
|
2021-10-16 22:48:58 +00:00
|
|
|
|
import SwiftyJSON
|
2022-01-02 19:43:30 +00:00
|
|
|
|
#if !os(macOS)
|
|
|
|
|
import UIKit
|
|
|
|
|
#endif
|
2021-06-14 18:05:02 +00:00
|
|
|
|
|
2021-09-25 08:18:22 +00:00
|
|
|
|
final class PlayerModel: ObservableObject {
|
2021-11-02 17:24:59 +00:00
|
|
|
|
static let availableRates: [Float] = [0.5, 0.67, 0.8, 1, 1.25, 1.5, 2]
|
2021-11-07 13:32:01 +00:00
|
|
|
|
let logger = Logger(label: "stream.yattee.app")
|
2021-06-15 16:35:21 +00:00
|
|
|
|
|
2022-02-27 20:31:17 +00:00
|
|
|
|
var avPlayerView = AppleAVPlayerView()
|
2021-12-29 18:55:41 +00:00
|
|
|
|
var playerItem: AVPlayerItem?
|
2021-06-15 21:21:57 +00:00
|
|
|
|
|
2022-02-16 20:23:11 +00:00
|
|
|
|
var mpvPlayerView = MPVPlayerView()
|
|
|
|
|
|
2021-12-19 17:17:04 +00:00
|
|
|
|
@Published var presentingPlayer = false { didSet { handlePresentationChange() } }
|
2022-02-16 20:23:11 +00:00
|
|
|
|
@Published var activeBackend = PlayerBackendType.mpv
|
|
|
|
|
|
|
|
|
|
var avPlayerBackend: AVPlayerBackend!
|
|
|
|
|
var mpvBackend: MPVBackend!
|
|
|
|
|
|
|
|
|
|
var backends: [PlayerBackend] {
|
|
|
|
|
[avPlayerBackend, mpvBackend]
|
|
|
|
|
}
|
2021-06-18 10:17:01 +00:00
|
|
|
|
|
2022-02-16 20:23:11 +00:00
|
|
|
|
var backend: PlayerBackend! {
|
|
|
|
|
switch activeBackend {
|
|
|
|
|
case .mpv:
|
|
|
|
|
return mpvBackend
|
|
|
|
|
case .appleAVPlayer:
|
|
|
|
|
return avPlayerBackend
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-27 11:42:20 +00:00
|
|
|
|
@Published var playerSize: CGSize = .zero { didSet {
|
|
|
|
|
backend.setSize(playerSize.width, playerSize.height)
|
|
|
|
|
}}
|
2021-10-05 20:20:09 +00:00
|
|
|
|
@Published var stream: Stream?
|
2022-02-16 20:23:11 +00:00
|
|
|
|
@Published var currentRate: Float = 1.0 { didSet { backend.setRate(currentRate) } }
|
2021-06-15 16:35:21 +00:00
|
|
|
|
|
2021-12-19 17:17:04 +00:00
|
|
|
|
@Published var availableStreams = [Stream]() { didSet { handleAvailableStreamsChange() } }
|
2021-10-23 16:49:45 +00:00
|
|
|
|
@Published var streamSelection: Stream? { didSet { rebuildTVMenu() } }
|
2021-10-16 22:48:58 +00:00
|
|
|
|
|
2021-10-24 18:01:08 +00:00
|
|
|
|
@Published var queue = [PlayerQueueItem]() { didSet { Defaults[.queue] = queue } }
|
2022-01-09 15:05:05 +00:00
|
|
|
|
@Published var currentItem: PlayerQueueItem! { didSet { handleCurrentItemChange() } }
|
2021-12-26 21:14:46 +00:00
|
|
|
|
@Published var historyVideos = [Video]()
|
2021-06-15 16:35:21 +00:00
|
|
|
|
|
2021-12-17 20:01:05 +00:00
|
|
|
|
@Published var preservedTime: CMTime?
|
2021-06-17 22:43:29 +00:00
|
|
|
|
|
2021-10-23 16:49:45 +00:00
|
|
|
|
@Published var sponsorBlock = SponsorBlockAPI()
|
|
|
|
|
@Published var segmentRestorationTime: CMTime?
|
|
|
|
|
@Published var lastSkipped: Segment? { didSet { rebuildTVMenu() } }
|
|
|
|
|
@Published var restoredSegments = [Segment]()
|
|
|
|
|
|
2022-03-19 23:04:56 +00:00
|
|
|
|
@Published var returnYouTubeDislike = ReturnYouTubeDislikeAPI()
|
|
|
|
|
|
2022-01-02 19:43:30 +00:00
|
|
|
|
#if os(iOS)
|
|
|
|
|
@Published var motionManager: CMMotionManager!
|
|
|
|
|
@Published var lockedOrientation: UIInterfaceOrientation?
|
|
|
|
|
@Published var lastOrientation: UIInterfaceOrientation?
|
|
|
|
|
#endif
|
|
|
|
|
|
2021-10-16 22:48:58 +00:00
|
|
|
|
var accounts: AccountsModel
|
2021-12-04 19:35:41 +00:00
|
|
|
|
var comments: CommentsModel
|
2022-02-16 20:23:11 +00:00
|
|
|
|
var controls: PlayerControlsModel { didSet {
|
|
|
|
|
backends.forEach { backend in
|
|
|
|
|
var backend = backend
|
|
|
|
|
backend.controls = controls
|
|
|
|
|
}
|
|
|
|
|
}}
|
2021-12-26 21:14:46 +00:00
|
|
|
|
var context: NSManagedObjectContext = PersistenceController.shared.container.viewContext
|
|
|
|
|
|
2022-02-16 20:23:11 +00:00
|
|
|
|
@Published var playingInPictureInPicture = false
|
2022-05-20 21:23:14 +00:00
|
|
|
|
var pipController: AVPictureInPictureController?
|
|
|
|
|
var pipDelegate = PiPDelegate()
|
2021-10-28 17:14:55 +00:00
|
|
|
|
|
2021-11-07 16:52:42 +00:00
|
|
|
|
@Published var presentingErrorDetails = false
|
|
|
|
|
var playerError: Error? { didSet {
|
|
|
|
|
#if !os(tvOS)
|
|
|
|
|
if !playerError.isNil {
|
|
|
|
|
presentingErrorDetails = true
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
}}
|
|
|
|
|
|
2021-12-19 17:17:04 +00:00
|
|
|
|
@Default(.pauseOnHidingPlayer) private var pauseOnHidingPlayer
|
|
|
|
|
@Default(.closePiPOnNavigation) var closePiPOnNavigation
|
|
|
|
|
@Default(.closePiPOnOpeningPlayer) var closePiPOnOpeningPlayer
|
|
|
|
|
|
|
|
|
|
#if !os(macOS)
|
|
|
|
|
@Default(.closePiPAndOpenPlayerOnEnteringForeground) var closePiPAndOpenPlayerOnEnteringForeground
|
|
|
|
|
#endif
|
|
|
|
|
|
2022-02-16 21:10:57 +00:00
|
|
|
|
private var currentArtwork: MPMediaItemArtwork?
|
2022-05-20 21:23:14 +00:00
|
|
|
|
#if !os(macOS)
|
|
|
|
|
var playerLayerView: PlayerLayerView!
|
|
|
|
|
#endif
|
2022-02-16 21:10:57 +00:00
|
|
|
|
|
2022-02-16 20:23:11 +00:00
|
|
|
|
init(accounts: AccountsModel? = nil, comments: CommentsModel? = nil, controls: PlayerControlsModel? = nil) {
|
2021-10-16 22:48:58 +00:00
|
|
|
|
self.accounts = accounts ?? AccountsModel()
|
2021-12-04 19:35:41 +00:00
|
|
|
|
self.comments = comments ?? CommentsModel()
|
2022-02-16 20:23:11 +00:00
|
|
|
|
self.controls = controls ?? PlayerControlsModel()
|
|
|
|
|
|
|
|
|
|
self.avPlayerBackend = AVPlayerBackend(model: self, controls: controls)
|
|
|
|
|
self.mpvBackend = MPVBackend(model: self)
|
2021-10-25 08:25:41 +00:00
|
|
|
|
|
2022-02-16 20:23:11 +00:00
|
|
|
|
self.activeBackend = Defaults[.activeBackend]
|
2021-10-24 18:01:08 +00:00
|
|
|
|
}
|
2021-10-24 14:01:36 +00:00
|
|
|
|
|
2021-12-19 17:17:04 +00:00
|
|
|
|
func show() {
|
2022-05-28 21:41:23 +00:00
|
|
|
|
#if os(macOS)
|
|
|
|
|
if presentingPlayer {
|
2022-01-06 15:35:45 +00:00
|
|
|
|
Windows.player.focus()
|
2022-05-28 21:41:23 +00:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
presentingPlayer = true
|
|
|
|
|
|
2021-12-19 17:17:04 +00:00
|
|
|
|
#if os(macOS)
|
2022-01-06 15:35:45 +00:00
|
|
|
|
Windows.player.open()
|
|
|
|
|
Windows.player.focus()
|
2021-12-19 17:17:04 +00:00
|
|
|
|
#endif
|
2021-06-15 21:21:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-19 17:17:04 +00:00
|
|
|
|
func hide() {
|
2022-03-27 19:17:52 +00:00
|
|
|
|
controls.playingFullscreen = false
|
2021-12-19 17:17:04 +00:00
|
|
|
|
presentingPlayer = false
|
2022-03-27 19:17:52 +00:00
|
|
|
|
|
|
|
|
|
#if os(iOS)
|
|
|
|
|
if Defaults[.lockPortraitWhenBrowsing] {
|
|
|
|
|
Orientation.lockOrientation(.portrait, andRotateTo: .portrait)
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2021-12-19 17:17:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-08 23:14:28 +00:00
|
|
|
|
func togglePlayer() {
|
2021-12-19 17:17:04 +00:00
|
|
|
|
#if os(macOS)
|
|
|
|
|
if !presentingPlayer {
|
2022-01-06 15:35:45 +00:00
|
|
|
|
Windows.player.open()
|
2021-12-19 17:17:04 +00:00
|
|
|
|
}
|
2022-01-06 15:35:45 +00:00
|
|
|
|
Windows.player.focus()
|
2021-12-19 17:17:04 +00:00
|
|
|
|
#else
|
|
|
|
|
if presentingPlayer {
|
|
|
|
|
hide()
|
|
|
|
|
} else {
|
|
|
|
|
show()
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2021-11-08 23:14:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-29 18:39:38 +00:00
|
|
|
|
var isLoadingVideo: Bool {
|
|
|
|
|
guard !currentVideo.isNil else {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-16 20:23:11 +00:00
|
|
|
|
return backend.isLoadingVideo
|
2021-12-29 18:39:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-16 22:48:58 +00:00
|
|
|
|
var isPlaying: Bool {
|
2022-02-16 20:23:11 +00:00
|
|
|
|
backend.isPlaying
|
2021-10-16 22:48:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-16 20:23:11 +00:00
|
|
|
|
var playerItemDuration: CMTime? {
|
|
|
|
|
backend.playerItemDuration
|
2021-10-22 20:49:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-16 20:23:11 +00:00
|
|
|
|
var playerItemDurationWithoutSponsorSegments: CMTime? {
|
|
|
|
|
(backend.playerItemDuration ?? .zero) - .secondsInDefaultTimescale(
|
|
|
|
|
sponsorBlock.segments.reduce(0) { $0 + $1.duration }
|
|
|
|
|
)
|
2021-10-22 23:04:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-16 20:23:11 +00:00
|
|
|
|
var videoDuration: TimeInterval? {
|
|
|
|
|
currentItem?.duration ?? currentVideo?.length ?? playerItemDuration?.seconds
|
2021-10-22 20:49:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-16 20:23:11 +00:00
|
|
|
|
var time: CMTime? {
|
|
|
|
|
currentItem?.playbackTime
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var live: Bool {
|
|
|
|
|
currentVideo?.live ?? false
|
2021-10-22 20:49:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-05 20:20:09 +00:00
|
|
|
|
func togglePlay() {
|
2022-02-16 20:23:11 +00:00
|
|
|
|
backend.togglePlay()
|
2021-06-15 16:35:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-05 20:20:09 +00:00
|
|
|
|
func play() {
|
2022-02-16 20:23:11 +00:00
|
|
|
|
backend.play()
|
2021-07-29 22:28:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-05 20:20:09 +00:00
|
|
|
|
func pause() {
|
2022-02-16 20:23:11 +00:00
|
|
|
|
backend.pause()
|
2021-09-25 08:18:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-28 21:41:23 +00:00
|
|
|
|
func play(_ video: Video, at time: CMTime? = nil) {
|
|
|
|
|
var delay = 0.0
|
|
|
|
|
#if !os(macOS)
|
|
|
|
|
delay = 0.3
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + delay) { [weak self] in
|
|
|
|
|
guard let self = self else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.playNow(video, at: time)
|
|
|
|
|
}
|
2021-12-26 21:14:46 +00:00
|
|
|
|
|
|
|
|
|
guard !playingInPictureInPicture else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-28 21:41:23 +00:00
|
|
|
|
show()
|
2021-10-16 22:48:58 +00:00
|
|
|
|
}
|
2021-07-18 22:32:46 +00:00
|
|
|
|
|
2021-10-24 18:01:08 +00:00
|
|
|
|
func playStream(
|
2021-10-16 22:48:58 +00:00
|
|
|
|
_ stream: Stream,
|
|
|
|
|
of video: Video,
|
2021-12-17 19:55:52 +00:00
|
|
|
|
preservingTime: Bool = false,
|
|
|
|
|
upgrading: Bool = false
|
2021-10-16 22:48:58 +00:00
|
|
|
|
) {
|
2021-11-07 16:52:42 +00:00
|
|
|
|
playerError = nil
|
2021-12-17 19:55:52 +00:00
|
|
|
|
if !upgrading {
|
|
|
|
|
resetSegments()
|
|
|
|
|
|
2021-12-29 18:55:41 +00:00
|
|
|
|
DispatchQueue.main.async { [weak self] in
|
|
|
|
|
self?.sponsorBlock.loadSegments(
|
|
|
|
|
videoID: video.videoID,
|
|
|
|
|
categories: Defaults[.sponsorBlockCategories]
|
2022-04-16 17:50:02 +00:00
|
|
|
|
)
|
2022-03-19 23:04:56 +00:00
|
|
|
|
|
2022-03-20 20:31:19 +00:00
|
|
|
|
guard Defaults[.enableReturnYouTubeDislike] else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-19 23:04:56 +00:00
|
|
|
|
self?.returnYouTubeDislike.loadDislikes(videoID: video.videoID) { [weak self] dislikes in
|
|
|
|
|
self?.currentItem?.video?.dislikes = dislikes
|
|
|
|
|
}
|
2021-12-17 19:55:52 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-10-23 16:49:45 +00:00
|
|
|
|
|
2022-02-16 20:23:11 +00:00
|
|
|
|
controls.reset()
|
2021-10-16 22:48:58 +00:00
|
|
|
|
|
2022-02-16 20:23:11 +00:00
|
|
|
|
backend.playStream(
|
|
|
|
|
stream,
|
|
|
|
|
of: video,
|
|
|
|
|
preservingTime: preservingTime,
|
|
|
|
|
upgrading: upgrading
|
|
|
|
|
)
|
2022-02-16 21:10:57 +00:00
|
|
|
|
|
|
|
|
|
if !upgrading {
|
|
|
|
|
updateCurrentArtwork()
|
|
|
|
|
}
|
2022-02-16 20:23:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func saveTime(completionHandler: @escaping () -> Void = {}) {
|
|
|
|
|
guard let currentTime = backend.currentTime, currentTime.seconds > 0 else {
|
|
|
|
|
return
|
2021-07-18 22:32:46 +00:00
|
|
|
|
}
|
2021-10-28 20:18:23 +00:00
|
|
|
|
|
2022-02-16 20:23:11 +00:00
|
|
|
|
DispatchQueue.main.async { [weak self] in
|
|
|
|
|
self?.preservedTime = currentTime
|
|
|
|
|
completionHandler()
|
2021-12-17 19:55:52 +00:00
|
|
|
|
}
|
2021-07-18 22:32:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-16 20:23:11 +00:00
|
|
|
|
func upgradeToStream(_ stream: Stream, force: Bool = false) {
|
|
|
|
|
if !self.stream.isNil, force || self.stream != stream {
|
2021-12-26 21:14:46 +00:00
|
|
|
|
playStream(stream, of: currentVideo!, preservingTime: true, upgrading: true)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-19 16:56:47 +00:00
|
|
|
|
private func handleAvailableStreamsChange() {
|
|
|
|
|
rebuildTVMenu()
|
|
|
|
|
|
|
|
|
|
guard stream.isNil else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
guard let stream = preferredStream(availableStreams) else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
streamSelection = stream
|
|
|
|
|
playStream(
|
|
|
|
|
stream,
|
|
|
|
|
of: currentVideo!,
|
|
|
|
|
preservingTime: !currentItem.playbackTime.isNil
|
|
|
|
|
)
|
|
|
|
|
}
|
2021-12-19 17:17:04 +00:00
|
|
|
|
|
|
|
|
|
private func handlePresentationChange() {
|
2022-05-28 21:41:23 +00:00
|
|
|
|
var delay = 0.0
|
|
|
|
|
|
|
|
|
|
#if os(iOS)
|
|
|
|
|
if presentingPlayer {
|
|
|
|
|
delay = 0.2
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + delay) { [weak self] in
|
|
|
|
|
self?.backend.setNeedsDrawing(self?.presentingPlayer ?? false)
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-16 20:23:11 +00:00
|
|
|
|
controls.hide()
|
|
|
|
|
|
2022-04-03 15:03:56 +00:00
|
|
|
|
#if !os(macOS)
|
|
|
|
|
UIApplication.shared.isIdleTimerDisabled = presentingPlayer
|
|
|
|
|
#endif
|
|
|
|
|
|
2021-12-19 17:17:04 +00:00
|
|
|
|
if presentingPlayer, closePiPOnOpeningPlayer, playingInPictureInPicture {
|
|
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { [weak self] in
|
|
|
|
|
self?.closePiP()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !presentingPlayer, pauseOnHidingPlayer, !playingInPictureInPicture {
|
|
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { [weak self] in
|
|
|
|
|
self?.pause()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-16 20:23:11 +00:00
|
|
|
|
if !presentingPlayer, !pauseOnHidingPlayer, backend.isPlaying {
|
2021-12-19 17:17:04 +00:00
|
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 1) { [weak self] in
|
|
|
|
|
self?.play()
|
2021-11-05 14:58:51 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-16 20:23:11 +00:00
|
|
|
|
func changeActiveBackend(from: PlayerBackendType, to: PlayerBackendType) {
|
|
|
|
|
Defaults[.activeBackend] = to
|
|
|
|
|
self.activeBackend = to
|
2021-12-29 18:55:41 +00:00
|
|
|
|
|
2022-02-16 20:23:11 +00:00
|
|
|
|
guard var stream = stream else {
|
2021-07-18 22:32:46 +00:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-16 20:23:11 +00:00
|
|
|
|
inactiveBackends().forEach { $0.pause() }
|
2021-12-17 20:02:15 +00:00
|
|
|
|
|
2022-02-16 20:23:11 +00:00
|
|
|
|
let fromBackend: PlayerBackend = from == .appleAVPlayer ? avPlayerBackend : mpvBackend
|
|
|
|
|
let toBackend: PlayerBackend = to == .appleAVPlayer ? avPlayerBackend : mpvBackend
|
2021-10-24 18:01:08 +00:00
|
|
|
|
|
2022-02-16 20:23:11 +00:00
|
|
|
|
if let stream = toBackend.stream, toBackend.video == fromBackend.video {
|
|
|
|
|
toBackend.seek(to: fromBackend.currentTime?.seconds ?? .zero) { finished in
|
2021-10-22 20:49:31 +00:00
|
|
|
|
guard finished else {
|
|
|
|
|
return
|
|
|
|
|
}
|
2022-02-16 20:23:11 +00:00
|
|
|
|
toBackend.play()
|
2021-10-05 20:20:09 +00:00
|
|
|
|
}
|
2021-10-16 22:48:58 +00:00
|
|
|
|
|
2022-02-16 20:23:11 +00:00
|
|
|
|
self.stream = stream
|
|
|
|
|
streamSelection = stream
|
2021-07-22 12:43:13 +00:00
|
|
|
|
|
2021-10-16 22:48:58 +00:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-16 20:23:11 +00:00
|
|
|
|
if !backend.canPlay(stream) {
|
|
|
|
|
guard let preferredStream = preferredStream(availableStreams) else {
|
2021-10-24 18:01:08 +00:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-16 20:23:11 +00:00
|
|
|
|
stream = preferredStream
|
|
|
|
|
streamSelection = preferredStream
|
2021-12-05 17:15:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-16 20:23:11 +00:00
|
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { [weak self] in
|
2022-05-27 22:59:35 +00:00
|
|
|
|
guard let self = self else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
self.upgradeToStream(stream, force: true)
|
|
|
|
|
self.setNeedsDrawing(self.presentingPlayer)
|
2021-12-26 21:14:46 +00:00
|
|
|
|
}
|
2021-10-28 20:18:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-16 20:23:11 +00:00
|
|
|
|
private func inactiveBackends() -> [PlayerBackend] {
|
|
|
|
|
[activeBackend == PlayerBackendType.mpv ? avPlayerBackend : mpvBackend]
|
2021-10-28 20:18:23 +00:00
|
|
|
|
}
|
2021-11-02 17:24:59 +00:00
|
|
|
|
|
|
|
|
|
func rateLabel(_ rate: Float) -> String {
|
|
|
|
|
let formatter = NumberFormatter()
|
|
|
|
|
formatter.minimumFractionDigits = 0
|
|
|
|
|
formatter.maximumFractionDigits = 2
|
|
|
|
|
|
|
|
|
|
return "\(formatter.string(from: NSNumber(value: rate))!)×"
|
|
|
|
|
}
|
2021-12-02 20:19:10 +00:00
|
|
|
|
|
2022-04-03 12:23:42 +00:00
|
|
|
|
func closeCurrentItem(finished: Bool = false) {
|
|
|
|
|
prepareCurrentItemForHistory(finished: finished)
|
2021-12-02 20:19:10 +00:00
|
|
|
|
currentItem = nil
|
2022-02-16 20:23:11 +00:00
|
|
|
|
|
|
|
|
|
backend.closeItem()
|
2021-12-02 20:19:10 +00:00
|
|
|
|
}
|
2021-12-19 17:17:04 +00:00
|
|
|
|
|
|
|
|
|
func closePiP() {
|
|
|
|
|
guard playingInPictureInPicture else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let wasPlaying = isPlaying
|
|
|
|
|
pause()
|
|
|
|
|
|
|
|
|
|
#if os(tvOS)
|
|
|
|
|
show()
|
|
|
|
|
#endif
|
2021-12-19 23:36:12 +00:00
|
|
|
|
|
2022-02-16 20:23:11 +00:00
|
|
|
|
backend.closePiP(wasPlaying: wasPlaying)
|
2021-12-19 17:17:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-09 15:05:05 +00:00
|
|
|
|
func handleCurrentItemChange() {
|
2022-01-06 15:35:45 +00:00
|
|
|
|
#if os(macOS)
|
|
|
|
|
Windows.player.window?.title = windowTitle
|
|
|
|
|
#endif
|
2022-01-09 15:05:05 +00:00
|
|
|
|
|
|
|
|
|
Defaults[.lastPlayed] = currentItem
|
2022-01-06 15:35:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-19 17:17:04 +00:00
|
|
|
|
#if os(macOS)
|
|
|
|
|
var windowTitle: String {
|
|
|
|
|
currentVideo.isNil ? "Not playing" : "\(currentVideo!.title) - \(currentVideo!.author)"
|
|
|
|
|
}
|
|
|
|
|
#else
|
|
|
|
|
func handleEnterForeground() {
|
|
|
|
|
guard closePiPAndOpenPlayerOnEnteringForeground, playingInPictureInPicture else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
show()
|
|
|
|
|
closePiP()
|
|
|
|
|
}
|
2022-01-02 19:43:30 +00:00
|
|
|
|
|
|
|
|
|
func enterFullScreen() {
|
2022-02-16 20:23:11 +00:00
|
|
|
|
guard !controls.playingFullscreen else {
|
2022-01-02 19:43:30 +00:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logger.info("entering fullscreen")
|
|
|
|
|
|
2022-02-16 20:23:11 +00:00
|
|
|
|
backend.enterFullScreen()
|
2022-01-02 19:43:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func exitFullScreen() {
|
2022-02-16 20:23:11 +00:00
|
|
|
|
guard controls.playingFullscreen else {
|
2022-01-02 19:43:30 +00:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logger.info("exiting fullscreen")
|
|
|
|
|
|
2022-05-27 22:59:35 +00:00
|
|
|
|
if controls.playingFullscreen {
|
|
|
|
|
toggleFullscreen(true)
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-16 20:23:11 +00:00
|
|
|
|
backend.exitFullScreen()
|
2022-01-02 19:43:30 +00:00
|
|
|
|
}
|
2021-12-19 17:17:04 +00:00
|
|
|
|
#endif
|
2022-02-16 21:10:57 +00:00
|
|
|
|
|
|
|
|
|
func updateNowPlayingInfo() {
|
2022-02-27 20:25:29 +00:00
|
|
|
|
guard let video = currentItem?.video else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-16 21:10:57 +00:00
|
|
|
|
let currentTime = (backend.currentTime?.seconds.isFinite ?? false) ? backend.currentTime!.seconds : 0
|
|
|
|
|
var nowPlayingInfo: [String: AnyObject] = [
|
2022-02-27 20:25:29 +00:00
|
|
|
|
MPMediaItemPropertyTitle: video.title as AnyObject,
|
|
|
|
|
MPMediaItemPropertyArtist: video.author as AnyObject,
|
|
|
|
|
MPNowPlayingInfoPropertyIsLiveStream: video.live as AnyObject,
|
2022-02-16 21:10:57 +00:00
|
|
|
|
MPNowPlayingInfoPropertyElapsedPlaybackTime: currentTime as AnyObject,
|
|
|
|
|
MPNowPlayingInfoPropertyPlaybackQueueCount: queue.count as AnyObject,
|
|
|
|
|
MPNowPlayingInfoPropertyPlaybackQueueIndex: 1 as AnyObject,
|
|
|
|
|
MPMediaItemPropertyMediaType: MPMediaType.anyVideo.rawValue as AnyObject
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
if !currentArtwork.isNil {
|
|
|
|
|
nowPlayingInfo[MPMediaItemPropertyArtwork] = currentArtwork as AnyObject
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-27 20:25:29 +00:00
|
|
|
|
if !video.live {
|
2022-02-16 21:10:57 +00:00
|
|
|
|
let itemDuration = (backend.playerItemDuration ?? .zero).seconds
|
|
|
|
|
let duration = itemDuration.isFinite ? Double(itemDuration) : nil
|
|
|
|
|
|
|
|
|
|
if !duration.isNil {
|
|
|
|
|
nowPlayingInfo[MPMediaItemPropertyPlaybackDuration] = duration as AnyObject
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func updateCurrentArtwork() {
|
|
|
|
|
guard let video = currentVideo,
|
|
|
|
|
let thumbnailData = try? Data(contentsOf: video.thumbnailURL(quality: .medium)!)
|
|
|
|
|
else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if os(macOS)
|
|
|
|
|
let image = NSImage(data: thumbnailData)
|
|
|
|
|
#else
|
|
|
|
|
let image = UIImage(data: thumbnailData)
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
if image.isNil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
currentArtwork = MPMediaItemArtwork(boundsSize: image!.size) { _ in image! }
|
|
|
|
|
}
|
2022-04-03 12:23:42 +00:00
|
|
|
|
|
|
|
|
|
func toggleFullscreen(_ isFullScreen: Bool) {
|
|
|
|
|
controls.resetTimer()
|
|
|
|
|
|
|
|
|
|
#if os(macOS)
|
|
|
|
|
Windows.player.toggleFullScreen()
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
controls.playingFullscreen = !isFullScreen
|
|
|
|
|
|
|
|
|
|
#if os(iOS)
|
|
|
|
|
if controls.playingFullscreen {
|
|
|
|
|
guard !(UIApplication.shared.windows.first?.windowScene?.interfaceOrientation.isLandscape ?? true) else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
Orientation.lockOrientation(.landscape, andRotateTo: .landscapeRight)
|
|
|
|
|
} else {
|
|
|
|
|
Orientation.lockOrientation(.allButUpsideDown, andRotateTo: .portrait)
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
}
|
2021-06-14 18:05:02 +00:00
|
|
|
|
}
|