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