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-12-29 18:55:41 +00:00
|
|
|
|
static let assetKeysToLoad = ["tracks", "playable", "duration"]
|
2021-11-07 13:32:01 +00:00
|
|
|
|
let logger = Logger(label: "stream.yattee.app")
|
2021-06-15 16:35:21 +00:00
|
|
|
|
|
2021-10-05 20:20:09 +00:00
|
|
|
|
private(set) var player = AVPlayer()
|
2021-12-19 17:17:04 +00:00
|
|
|
|
var playerView = Player()
|
|
|
|
|
var controller: PlayerViewController?
|
2021-12-29 18:55:41 +00:00
|
|
|
|
var playerItem: AVPlayerItem?
|
2021-06-15 21:21:57 +00:00
|
|
|
|
|
2021-12-19 17:17:04 +00:00
|
|
|
|
@Published var presentingPlayer = false { didSet { handlePresentationChange() } }
|
2021-06-18 10:17:01 +00:00
|
|
|
|
|
2021-10-05 20:20:09 +00:00
|
|
|
|
@Published var stream: Stream?
|
2021-11-02 17:24:59 +00:00
|
|
|
|
@Published var currentRate: Float = 1.0 { didSet { player.rate = 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-12-19 17:17:04 +00:00
|
|
|
|
@Published var playerNavigationLinkActive = false { didSet { handleNavigationViewPlayerPresentationChange() } }
|
2021-08-22 19:13:33 +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]()
|
|
|
|
|
|
2021-12-17 20:01:05 +00:00
|
|
|
|
@Published var channelWithDetails: Channel?
|
|
|
|
|
|
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
|
2021-10-05 20:20:09 +00:00
|
|
|
|
|
2021-12-29 18:55:41 +00:00
|
|
|
|
var asset: AVURLAsset?
|
2021-10-22 23:04:03 +00:00
|
|
|
|
var composition = AVMutableComposition()
|
2021-11-28 14:37:55 +00:00
|
|
|
|
var loadedCompositionAssets = [AVMediaType]()
|
2021-10-24 18:01:08 +00:00
|
|
|
|
|
2021-12-26 21:14:46 +00:00
|
|
|
|
var context: NSManagedObjectContext = PersistenceController.shared.container.viewContext
|
|
|
|
|
|
2021-10-28 20:18:23 +00:00
|
|
|
|
private var currentArtwork: MPMediaItemArtwork?
|
2021-10-24 18:01:08 +00:00
|
|
|
|
private var frequentTimeObserver: Any?
|
|
|
|
|
private var infrequentTimeObserver: Any?
|
|
|
|
|
private var playerTimeControlStatusObserver: Any?
|
|
|
|
|
|
2021-10-16 22:48:58 +00:00
|
|
|
|
private var statusObservation: NSKeyValueObservation?
|
|
|
|
|
|
2021-10-25 21:29:06 +00:00
|
|
|
|
private var timeObserverThrottle = Throttle(interval: 2)
|
|
|
|
|
|
2021-10-28 17:14:55 +00:00
|
|
|
|
var playingInPictureInPicture = false
|
2022-01-02 19:43:30 +00:00
|
|
|
|
var playingFullscreen = false
|
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
|
|
|
|
|
|
2021-12-04 19:35:41 +00:00
|
|
|
|
init(accounts: AccountsModel? = nil, comments: CommentsModel? = 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()
|
2021-10-25 08:25:41 +00:00
|
|
|
|
|
2021-10-24 18:01:08 +00:00
|
|
|
|
addFrequentTimeObserver()
|
|
|
|
|
addInfrequentTimeObserver()
|
|
|
|
|
addPlayerTimeControlStatusObserver()
|
|
|
|
|
}
|
2021-10-24 14:01:36 +00:00
|
|
|
|
|
2021-12-19 17:17:04 +00:00
|
|
|
|
func show() {
|
|
|
|
|
guard !presentingPlayer else {
|
|
|
|
|
#if os(macOS)
|
2022-01-06 15:35:45 +00:00
|
|
|
|
Windows.player.focus()
|
2021-12-19 17:17:04 +00:00
|
|
|
|
#endif
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
#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-10-05 20:20:09 +00:00
|
|
|
|
presentingPlayer = true
|
2021-06-15 21:21:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-19 17:17:04 +00:00
|
|
|
|
func hide() {
|
|
|
|
|
presentingPlayer = false
|
2022-01-02 19:43:30 +00:00
|
|
|
|
playerNavigationLinkActive = false
|
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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return player.currentItem == nil || time == nil || !time!.isValid
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-16 22:48:58 +00:00
|
|
|
|
var isPlaying: Bool {
|
|
|
|
|
player.timeControlStatus == .playing
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-22 20:49:31 +00:00
|
|
|
|
var time: CMTime? {
|
|
|
|
|
currentItem?.playbackTime
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-22 23:04:03 +00:00
|
|
|
|
var live: Bool {
|
2021-12-26 21:14:46 +00:00
|
|
|
|
currentVideo?.live ?? false
|
2021-10-22 23:04:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-22 20:49:31 +00:00
|
|
|
|
var playerItemDuration: CMTime? {
|
|
|
|
|
player.currentItem?.asset.duration
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var videoDuration: TimeInterval? {
|
2022-01-06 16:47:07 +00:00
|
|
|
|
currentItem?.duration ?? currentVideo?.length ?? player.currentItem?.asset.duration.seconds
|
2021-10-22 20:49:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-05 20:20:09 +00:00
|
|
|
|
func togglePlay() {
|
|
|
|
|
isPlaying ? pause() : play()
|
2021-06-15 16:35:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-05 20:20:09 +00:00
|
|
|
|
func play() {
|
2021-10-24 18:01:08 +00:00
|
|
|
|
guard player.timeControlStatus != .playing else {
|
2021-06-15 16:35:21 +00:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-05 20:20:09 +00:00
|
|
|
|
player.play()
|
2021-07-29 22:28:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-05 20:20:09 +00:00
|
|
|
|
func pause() {
|
2021-10-24 18:01:08 +00:00
|
|
|
|
guard player.timeControlStatus != .paused else {
|
2021-07-29 22:28:28 +00:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-05 20:20:09 +00:00
|
|
|
|
player.pause()
|
2021-09-25 08:18:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-26 21:14:46 +00:00
|
|
|
|
func play(_ video: Video, at time: TimeInterval? = nil, inNavigationView: Bool = false) {
|
|
|
|
|
playNow(video, at: time)
|
|
|
|
|
|
|
|
|
|
guard !playingInPictureInPicture else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if inNavigationView {
|
|
|
|
|
playerNavigationLinkActive = true
|
|
|
|
|
} else {
|
|
|
|
|
show()
|
2021-07-22 12:43:13 +00:00
|
|
|
|
}
|
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]
|
|
|
|
|
) { [weak self] in
|
|
|
|
|
if Defaults[.showChannelSubscribers] {
|
|
|
|
|
self?.loadCurrentItemChannelDetails()
|
|
|
|
|
}
|
2021-12-17 19:55:52 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-10-23 16:49:45 +00:00
|
|
|
|
|
2021-10-16 22:48:58 +00:00
|
|
|
|
if let url = stream.singleAssetURL {
|
|
|
|
|
logger.info("playing stream with one asset\(stream.kind == .hls ? " (HLS)" : ""): \(url)")
|
2021-12-29 18:55:41 +00:00
|
|
|
|
loadSingleAsset(url, stream: stream, of: video, preservingTime: preservingTime)
|
2021-10-05 20:20:09 +00:00
|
|
|
|
} else {
|
2021-10-16 22:48:58 +00:00
|
|
|
|
logger.info("playing stream with many assets:")
|
|
|
|
|
logger.info("composition audio asset: \(stream.audioAsset.url)")
|
|
|
|
|
logger.info("composition video asset: \(stream.videoAsset.url)")
|
|
|
|
|
|
2021-11-28 14:37:55 +00:00
|
|
|
|
loadComposition(stream, of: video, preservingTime: preservingTime)
|
2021-07-18 22:32:46 +00:00
|
|
|
|
}
|
2021-10-28 20:18:23 +00:00
|
|
|
|
|
2021-12-17 19:55:52 +00:00
|
|
|
|
if !upgrading {
|
|
|
|
|
updateCurrentArtwork()
|
|
|
|
|
}
|
2021-07-18 22:32:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-26 21:14:46 +00:00
|
|
|
|
func upgradeToStream(_ stream: Stream) {
|
|
|
|
|
if !self.stream.isNil, self.stream != stream {
|
|
|
|
|
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() {
|
|
|
|
|
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()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !presentingPlayer, !pauseOnHidingPlayer, isPlaying {
|
|
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 1) { [weak self] in
|
|
|
|
|
self?.play()
|
2021-11-05 14:58:51 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-19 17:17:04 +00:00
|
|
|
|
private func handleNavigationViewPlayerPresentationChange() {
|
|
|
|
|
if pauseOnHidingPlayer, !playingInPictureInPicture, !playerNavigationLinkActive {
|
2021-12-17 20:01:05 +00:00
|
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
|
|
|
|
|
self.pause()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-16 22:48:58 +00:00
|
|
|
|
private func insertPlayerItem(
|
|
|
|
|
_ stream: Stream,
|
|
|
|
|
for video: Video,
|
|
|
|
|
preservingTime: Bool = false
|
|
|
|
|
) {
|
2021-12-29 18:55:41 +00:00
|
|
|
|
removeItemDidPlayToEndTimeObserver()
|
|
|
|
|
|
|
|
|
|
playerItem = playerItem(stream)
|
2021-10-05 20:20:09 +00:00
|
|
|
|
guard playerItem != nil else {
|
2021-07-18 22:32:46 +00:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-29 18:55:41 +00:00
|
|
|
|
addItemDidPlayToEndTimeObserver()
|
2021-10-16 22:48:58 +00:00
|
|
|
|
attachMetadata(to: playerItem!, video: video, for: stream)
|
2021-10-22 20:49:31 +00:00
|
|
|
|
|
2021-10-24 18:01:08 +00:00
|
|
|
|
DispatchQueue.main.async { [weak self] in
|
|
|
|
|
guard let self = self else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-05 20:20:09 +00:00
|
|
|
|
self.stream = stream
|
2021-10-16 22:48:58 +00:00
|
|
|
|
self.composition = AVMutableComposition()
|
2021-12-29 18:55:41 +00:00
|
|
|
|
self.asset = nil
|
2021-10-05 20:20:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-24 18:01:08 +00:00
|
|
|
|
let startPlaying = {
|
|
|
|
|
#if !os(macOS)
|
|
|
|
|
try? AVAudioSession.sharedInstance().setActive(true)
|
|
|
|
|
#endif
|
|
|
|
|
|
2021-12-29 18:55:41 +00:00
|
|
|
|
if self.isAutoplaying(self.playerItem!) {
|
2021-11-01 21:56:18 +00:00
|
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) { [weak self] in
|
2021-12-17 20:02:15 +00:00
|
|
|
|
guard let self = self else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-19 23:39:45 +00:00
|
|
|
|
if !preservingTime,
|
|
|
|
|
let segment = self.sponsorBlock.segments.first,
|
2021-12-17 20:02:15 +00:00
|
|
|
|
segment.start < 3,
|
|
|
|
|
self.lastSkipped.isNil
|
|
|
|
|
{
|
|
|
|
|
self.player.seek(
|
|
|
|
|
to: segment.endTime,
|
|
|
|
|
toleranceBefore: .secondsInDefaultTimescale(1),
|
|
|
|
|
toleranceAfter: .zero
|
|
|
|
|
) { finished in
|
|
|
|
|
guard finished else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.lastSkipped = segment
|
|
|
|
|
self.play()
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
self.play()
|
|
|
|
|
}
|
2021-11-01 21:56:18 +00:00
|
|
|
|
}
|
2021-10-24 18:01:08 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-22 20:49:31 +00:00
|
|
|
|
let replaceItemAndSeek = {
|
2021-12-29 18:55:41 +00:00
|
|
|
|
guard video == self.currentVideo else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
self.player.replaceCurrentItem(with: self.playerItem)
|
2021-12-17 20:01:05 +00:00
|
|
|
|
self.seekToPreservedTime { finished in
|
2021-10-22 20:49:31 +00:00
|
|
|
|
guard finished else {
|
|
|
|
|
return
|
|
|
|
|
}
|
2021-12-17 20:01:05 +00:00
|
|
|
|
self.preservedTime = nil
|
2021-10-24 14:01:36 +00:00
|
|
|
|
|
2021-10-24 18:01:08 +00:00
|
|
|
|
startPlaying()
|
2021-10-22 20:49:31 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-07-18 22:32:46 +00:00
|
|
|
|
|
2021-10-16 22:48:58 +00:00
|
|
|
|
if preservingTime {
|
2021-12-17 20:01:05 +00:00
|
|
|
|
if preservedTime.isNil {
|
2021-10-22 20:49:31 +00:00
|
|
|
|
saveTime {
|
|
|
|
|
replaceItemAndSeek()
|
|
|
|
|
startPlaying()
|
2021-10-16 22:48:58 +00:00
|
|
|
|
}
|
2021-10-22 20:49:31 +00:00
|
|
|
|
} else {
|
|
|
|
|
replaceItemAndSeek()
|
|
|
|
|
startPlaying()
|
2021-10-16 22:48:58 +00:00
|
|
|
|
}
|
2021-07-22 12:43:13 +00:00
|
|
|
|
} else {
|
2021-10-16 22:48:58 +00:00
|
|
|
|
player.replaceCurrentItem(with: playerItem)
|
2021-10-22 20:49:31 +00:00
|
|
|
|
startPlaying()
|
2021-06-15 16:35:21 +00:00
|
|
|
|
}
|
2021-06-16 19:12:41 +00:00
|
|
|
|
}
|
2021-06-15 16:35:21 +00:00
|
|
|
|
|
2021-12-29 18:55:41 +00:00
|
|
|
|
private func loadSingleAsset(
|
|
|
|
|
_ url: URL,
|
|
|
|
|
stream: Stream,
|
|
|
|
|
of video: Video,
|
|
|
|
|
preservingTime: Bool = false
|
|
|
|
|
) {
|
|
|
|
|
asset?.cancelLoading()
|
|
|
|
|
asset = AVURLAsset(url: url)
|
|
|
|
|
asset?.loadValuesAsynchronously(forKeys: Self.assetKeysToLoad) { [weak self] in
|
|
|
|
|
var error: NSError?
|
|
|
|
|
|
|
|
|
|
switch self?.asset?.statusOfValue(forKey: "duration", error: &error) {
|
|
|
|
|
case .loaded:
|
|
|
|
|
DispatchQueue.main.async { [weak self] in
|
|
|
|
|
self?.insertPlayerItem(stream, for: video, preservingTime: preservingTime)
|
|
|
|
|
}
|
|
|
|
|
case .failed:
|
2022-01-02 19:43:30 +00:00
|
|
|
|
DispatchQueue.main.async { [weak self] in
|
|
|
|
|
self?.playerError = error
|
|
|
|
|
}
|
2021-12-29 18:55:41 +00:00
|
|
|
|
default:
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-16 22:48:58 +00:00
|
|
|
|
private func loadComposition(
|
|
|
|
|
_ stream: Stream,
|
|
|
|
|
of video: Video,
|
|
|
|
|
preservingTime: Bool = false
|
2021-11-28 14:37:55 +00:00
|
|
|
|
) {
|
|
|
|
|
loadedCompositionAssets = []
|
|
|
|
|
loadCompositionAsset(stream.audioAsset, stream: stream, type: .audio, of: video, preservingTime: preservingTime)
|
|
|
|
|
loadCompositionAsset(stream.videoAsset, stream: stream, type: .video, of: video, preservingTime: preservingTime)
|
2021-06-15 16:35:21 +00:00
|
|
|
|
}
|
2021-06-15 21:21:57 +00:00
|
|
|
|
|
2021-12-02 20:19:10 +00:00
|
|
|
|
private func loadCompositionAsset(
|
2021-10-24 18:01:08 +00:00
|
|
|
|
_ asset: AVURLAsset,
|
2021-11-28 14:37:55 +00:00
|
|
|
|
stream: Stream,
|
2021-10-24 18:01:08 +00:00
|
|
|
|
type: AVMediaType,
|
2021-11-28 14:37:55 +00:00
|
|
|
|
of video: Video,
|
|
|
|
|
preservingTime: Bool = false
|
|
|
|
|
) {
|
2021-12-29 18:55:41 +00:00
|
|
|
|
asset.loadValuesAsynchronously(forKeys: Self.assetKeysToLoad) { [weak self] in
|
2021-11-28 14:37:55 +00:00
|
|
|
|
guard let self = self else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
self.logger.info("loading \(type.rawValue) track")
|
2021-06-16 19:12:41 +00:00
|
|
|
|
|
2021-11-28 14:37:55 +00:00
|
|
|
|
let assetTracks = asset.tracks(withMediaType: type)
|
|
|
|
|
|
|
|
|
|
guard let compositionTrack = self.composition.addMutableTrack(
|
|
|
|
|
withMediaType: type,
|
|
|
|
|
preferredTrackID: kCMPersistentTrackID_Invalid
|
|
|
|
|
) else {
|
|
|
|
|
self.logger.critical("composition \(type.rawValue) addMutableTrack FAILED")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
guard let assetTrack = assetTracks.first else {
|
|
|
|
|
self.logger.critical("asset \(type.rawValue) track FAILED")
|
|
|
|
|
return
|
|
|
|
|
}
|
2021-10-16 22:48:58 +00:00
|
|
|
|
|
2021-11-28 14:37:55 +00:00
|
|
|
|
try! compositionTrack.insertTimeRange(
|
|
|
|
|
CMTimeRange(start: .zero, duration: CMTime.secondsInDefaultTimescale(video.length)),
|
|
|
|
|
of: assetTrack,
|
|
|
|
|
at: .zero
|
|
|
|
|
)
|
2021-10-16 22:48:58 +00:00
|
|
|
|
|
2021-11-28 14:37:55 +00:00
|
|
|
|
self.logger.critical("\(type.rawValue) LOADED")
|
|
|
|
|
|
|
|
|
|
guard self.streamSelection == stream else {
|
|
|
|
|
self.logger.critical("IGNORING LOADED")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.loadedCompositionAssets.append(type)
|
|
|
|
|
|
|
|
|
|
if self.loadedCompositionAssets.count == 2 {
|
|
|
|
|
self.insertPlayerItem(stream, for: video, preservingTime: preservingTime)
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-10-16 22:48:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-29 18:55:41 +00:00
|
|
|
|
private func playerItem(_: Stream) -> AVPlayerItem? {
|
|
|
|
|
if let asset = asset {
|
|
|
|
|
return AVPlayerItem(asset: asset)
|
2021-10-16 22:48:58 +00:00
|
|
|
|
} else {
|
|
|
|
|
return AVPlayerItem(asset: composition)
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-07-22 12:43:13 +00:00
|
|
|
|
|
2021-10-16 22:48:58 +00:00
|
|
|
|
private func attachMetadata(to item: AVPlayerItem, video: Video, for _: Stream? = nil) {
|
2021-07-22 12:43:13 +00:00
|
|
|
|
#if !os(macOS)
|
2021-10-16 22:48:58 +00:00
|
|
|
|
var externalMetadata = [
|
|
|
|
|
makeMetadataItem(.commonIdentifierTitle, value: video.title),
|
2021-10-20 22:21:50 +00:00
|
|
|
|
makeMetadataItem(.quickTimeMetadataGenre, value: video.genre ?? ""),
|
|
|
|
|
makeMetadataItem(.commonIdentifierDescription, value: video.description ?? "")
|
2021-10-16 22:48:58 +00:00
|
|
|
|
]
|
2021-10-05 20:20:09 +00:00
|
|
|
|
if let thumbnailData = try? Data(contentsOf: video.thumbnailURL(quality: .medium)!),
|
2021-07-22 12:43:13 +00:00
|
|
|
|
let image = UIImage(data: thumbnailData),
|
|
|
|
|
let pngData = image.pngData()
|
|
|
|
|
{
|
|
|
|
|
let artworkItem = makeMetadataItem(.commonIdentifierArtwork, value: pngData)
|
|
|
|
|
externalMetadata.append(artworkItem)
|
|
|
|
|
}
|
2021-06-15 21:21:57 +00:00
|
|
|
|
|
2021-10-16 22:48:58 +00:00
|
|
|
|
item.externalMetadata = externalMetadata
|
2021-07-22 12:43:13 +00:00
|
|
|
|
#endif
|
2021-06-15 21:21:57 +00:00
|
|
|
|
|
2021-10-16 22:48:58 +00:00
|
|
|
|
item.preferredForwardBufferDuration = 5
|
2021-06-15 21:21:57 +00:00
|
|
|
|
|
2021-12-19 17:17:04 +00:00
|
|
|
|
observePlayerItemStatus(item)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private func observePlayerItemStatus(_ item: AVPlayerItem) {
|
2021-10-05 20:20:09 +00:00
|
|
|
|
statusObservation?.invalidate()
|
2021-10-24 18:01:08 +00:00
|
|
|
|
statusObservation = item.observe(\.status, options: [.old, .new]) { [weak self] playerItem, _ in
|
|
|
|
|
guard let self = self else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-05 20:20:09 +00:00
|
|
|
|
switch playerItem.status {
|
|
|
|
|
case .readyToPlay:
|
2021-10-24 18:01:08 +00:00
|
|
|
|
if self.isAutoplaying(playerItem) {
|
2021-10-16 22:48:58 +00:00
|
|
|
|
self.play()
|
2021-10-05 20:20:09 +00:00
|
|
|
|
}
|
2021-10-16 22:48:58 +00:00
|
|
|
|
case .failed:
|
2021-11-07 16:52:42 +00:00
|
|
|
|
self.playerError = item.error
|
2021-10-16 22:48:58 +00:00
|
|
|
|
|
2021-10-05 20:20:09 +00:00
|
|
|
|
default:
|
|
|
|
|
return
|
|
|
|
|
}
|
2021-06-18 10:17:01 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-16 22:48:58 +00:00
|
|
|
|
#if !os(macOS)
|
|
|
|
|
private func makeMetadataItem(_ identifier: AVMetadataIdentifier, value: Any) -> AVMetadataItem {
|
|
|
|
|
let item = AVMutableMetadataItem()
|
|
|
|
|
|
|
|
|
|
item.identifier = identifier
|
|
|
|
|
item.value = value as? NSCopying & NSObjectProtocol
|
|
|
|
|
item.extendedLanguageTag = "und"
|
|
|
|
|
|
|
|
|
|
return item.copy() as! AVMetadataItem
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
private func addItemDidPlayToEndTimeObserver() {
|
2021-10-05 20:20:09 +00:00
|
|
|
|
NotificationCenter.default.addObserver(
|
|
|
|
|
self,
|
|
|
|
|
selector: #selector(itemDidPlayToEndTime),
|
|
|
|
|
name: NSNotification.Name.AVPlayerItemDidPlayToEndTime,
|
2021-12-29 18:55:41 +00:00
|
|
|
|
object: playerItem
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private func removeItemDidPlayToEndTimeObserver() {
|
|
|
|
|
NotificationCenter.default.removeObserver(
|
|
|
|
|
self,
|
|
|
|
|
name: NSNotification.Name.AVPlayerItemDidPlayToEndTime,
|
|
|
|
|
object: playerItem
|
2021-10-05 20:20:09 +00:00
|
|
|
|
)
|
2021-06-18 10:17:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-05 20:20:09 +00:00
|
|
|
|
@objc func itemDidPlayToEndTime() {
|
2021-12-26 21:14:46 +00:00
|
|
|
|
prepareCurrentItemForHistory(finished: true)
|
2021-10-24 18:01:08 +00:00
|
|
|
|
|
2021-10-05 20:20:09 +00:00
|
|
|
|
if queue.isEmpty {
|
2021-10-24 22:26:25 +00:00
|
|
|
|
#if !os(macOS)
|
|
|
|
|
try? AVAudioSession.sharedInstance().setActive(false)
|
|
|
|
|
#endif
|
2021-10-05 20:20:09 +00:00
|
|
|
|
resetQueue()
|
|
|
|
|
#if os(tvOS)
|
2021-12-26 21:14:46 +00:00
|
|
|
|
controller?.playerView.dismiss(animated: false) { [weak self] in
|
|
|
|
|
self?.controller?.dismiss(animated: true)
|
|
|
|
|
}
|
2021-10-05 20:20:09 +00:00
|
|
|
|
#endif
|
|
|
|
|
} else {
|
|
|
|
|
advanceToNextItem()
|
2021-06-15 21:21:57 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-16 22:48:58 +00:00
|
|
|
|
private func saveTime(completionHandler: @escaping () -> Void = {}) {
|
|
|
|
|
let currentTime = player.currentTime()
|
|
|
|
|
|
|
|
|
|
guard currentTime.seconds > 0 else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-24 18:01:08 +00:00
|
|
|
|
DispatchQueue.main.async { [weak self] in
|
2021-12-17 20:01:05 +00:00
|
|
|
|
self?.preservedTime = currentTime
|
2021-10-16 22:48:58 +00:00
|
|
|
|
completionHandler()
|
2021-06-17 22:43:29 +00:00
|
|
|
|
}
|
2021-10-16 22:48:58 +00:00
|
|
|
|
}
|
2021-07-22 12:43:13 +00:00
|
|
|
|
|
2021-12-17 20:01:05 +00:00
|
|
|
|
private func seekToPreservedTime(completionHandler: @escaping (Bool) -> Void = { _ in }) {
|
|
|
|
|
guard let time = preservedTime else {
|
2021-10-16 22:48:58 +00:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
player.seek(
|
|
|
|
|
to: time,
|
2021-10-24 18:01:08 +00:00
|
|
|
|
toleranceBefore: .secondsInDefaultTimescale(1),
|
2021-10-16 22:48:58 +00:00
|
|
|
|
toleranceAfter: .zero,
|
|
|
|
|
completionHandler: completionHandler
|
|
|
|
|
)
|
2021-06-17 22:43:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-24 18:01:08 +00:00
|
|
|
|
private func addFrequentTimeObserver() {
|
|
|
|
|
let interval = CMTime.secondsInDefaultTimescale(0.5)
|
|
|
|
|
|
|
|
|
|
frequentTimeObserver = player.addPeriodicTimeObserver(
|
|
|
|
|
forInterval: interval,
|
|
|
|
|
queue: .main
|
|
|
|
|
) { [weak self] _ in
|
|
|
|
|
guard let self = self else {
|
|
|
|
|
return
|
|
|
|
|
}
|
2021-06-19 20:10:14 +00:00
|
|
|
|
|
2021-10-23 16:49:45 +00:00
|
|
|
|
guard !self.currentItem.isNil else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-02 19:40:49 +00:00
|
|
|
|
#if !os(tvOS)
|
|
|
|
|
self.updateNowPlayingInfo()
|
|
|
|
|
#endif
|
|
|
|
|
|
2021-10-24 18:01:08 +00:00
|
|
|
|
self.handleSegments(at: self.player.currentTime())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private func addInfrequentTimeObserver() {
|
|
|
|
|
let interval = CMTime.secondsInDefaultTimescale(5)
|
2021-10-23 16:49:45 +00:00
|
|
|
|
|
2021-10-24 18:01:08 +00:00
|
|
|
|
infrequentTimeObserver = player.addPeriodicTimeObserver(
|
|
|
|
|
forInterval: interval,
|
|
|
|
|
queue: .main
|
|
|
|
|
) { [weak self] _ in
|
|
|
|
|
guard let self = self else {
|
|
|
|
|
return
|
|
|
|
|
}
|
2021-10-23 16:49:45 +00:00
|
|
|
|
|
2021-10-24 18:01:08 +00:00
|
|
|
|
guard !self.currentItem.isNil else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-25 21:29:06 +00:00
|
|
|
|
self.timeObserverThrottle.execute {
|
2021-12-26 21:14:46 +00:00
|
|
|
|
self.updateWatch()
|
2021-10-25 21:29:06 +00:00
|
|
|
|
}
|
2021-06-17 22:43:29 +00:00
|
|
|
|
}
|
2021-06-16 19:12:41 +00:00
|
|
|
|
}
|
2021-10-24 14:01:36 +00:00
|
|
|
|
|
2021-10-24 18:01:08 +00:00
|
|
|
|
private func addPlayerTimeControlStatusObserver() {
|
|
|
|
|
playerTimeControlStatusObserver = player.observe(\.timeControlStatus) { [weak self] player, _ in
|
|
|
|
|
guard let self = self,
|
|
|
|
|
self.player == player
|
|
|
|
|
else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-25 21:29:06 +00:00
|
|
|
|
if player.timeControlStatus != .waitingToPlayAtSpecifiedRate {
|
|
|
|
|
self.objectWillChange.send()
|
|
|
|
|
}
|
2021-10-24 18:01:08 +00:00
|
|
|
|
|
2021-11-02 17:24:59 +00:00
|
|
|
|
if player.timeControlStatus == .playing, player.rate != self.currentRate {
|
|
|
|
|
player.rate = self.currentRate
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-02 11:01:45 +00:00
|
|
|
|
#if os(macOS)
|
|
|
|
|
if player.timeControlStatus == .playing {
|
|
|
|
|
ScreenSaverManager.shared.disable(reason: "Yattee is playing video")
|
|
|
|
|
} else {
|
|
|
|
|
ScreenSaverManager.shared.enable()
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2021-10-25 21:29:06 +00:00
|
|
|
|
|
2021-11-02 11:01:45 +00:00
|
|
|
|
self.timeObserverThrottle.execute {
|
2021-12-26 21:14:46 +00:00
|
|
|
|
self.updateWatch()
|
2021-10-25 21:29:06 +00:00
|
|
|
|
}
|
2021-10-24 14:01:36 +00:00
|
|
|
|
}
|
2021-10-24 18:01:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-28 20:18:23 +00:00
|
|
|
|
fileprivate func updateNowPlayingInfo() {
|
2021-12-05 17:15:40 +00:00
|
|
|
|
var nowPlayingInfo: [String: AnyObject] = [
|
2021-10-28 20:18:23 +00:00
|
|
|
|
MPMediaItemPropertyTitle: currentItem.video.title as AnyObject,
|
|
|
|
|
MPMediaItemPropertyArtist: currentItem.video.author as AnyObject,
|
2021-11-01 21:56:18 +00:00
|
|
|
|
MPNowPlayingInfoPropertyIsLiveStream: currentItem.video.live as AnyObject,
|
2021-10-28 20:18:23 +00:00
|
|
|
|
MPNowPlayingInfoPropertyElapsedPlaybackTime: player.currentTime().seconds as AnyObject,
|
|
|
|
|
MPNowPlayingInfoPropertyPlaybackQueueCount: queue.count as AnyObject,
|
|
|
|
|
MPMediaItemPropertyMediaType: MPMediaType.anyVideo.rawValue as AnyObject
|
|
|
|
|
]
|
|
|
|
|
|
2021-12-05 17:15:40 +00:00
|
|
|
|
if !currentArtwork.isNil {
|
|
|
|
|
nowPlayingInfo[MPMediaItemPropertyArtwork] = currentArtwork as AnyObject
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-26 21:14:46 +00:00
|
|
|
|
if !currentItem.video.live {
|
2021-12-29 19:20:09 +00:00
|
|
|
|
let itemDuration = currentItem.videoDuration ?? currentItem.duration
|
|
|
|
|
let duration = itemDuration.isFinite ? Double(itemDuration) : nil
|
2021-12-26 21:14:46 +00:00
|
|
|
|
|
|
|
|
|
if !duration.isNil {
|
|
|
|
|
nowPlayingInfo[MPMediaItemPropertyPlaybackDuration] = duration as AnyObject
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-28 20:18:23 +00:00
|
|
|
|
MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private func updateCurrentArtwork() {
|
|
|
|
|
guard let thumbnailData = try? Data(contentsOf: currentItem.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! }
|
|
|
|
|
}
|
2021-11-02 17:24:59 +00:00
|
|
|
|
|
2021-12-17 20:01:05 +00:00
|
|
|
|
func loadCurrentItemChannelDetails() {
|
|
|
|
|
guard let video = currentVideo,
|
|
|
|
|
!video.channel.detailsLoaded
|
|
|
|
|
else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if restoreLoadedChannel() {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
accounts.api.channel(video.channel.id).load().onSuccess { [weak self] response in
|
|
|
|
|
if let channel: Channel = response.typedContent() {
|
|
|
|
|
self?.channelWithDetails = channel
|
|
|
|
|
withAnimation {
|
2021-12-26 21:14:46 +00:00
|
|
|
|
self?.currentItem?.video.channel = channel
|
2021-12-17 20:01:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@discardableResult func restoreLoadedChannel() -> Bool {
|
|
|
|
|
if !currentVideo.isNil, channelWithDetails?.id == currentVideo!.channel.id {
|
|
|
|
|
currentItem.video.channel = channelWithDetails!
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
func closeCurrentItem() {
|
2021-12-26 21:14:46 +00:00
|
|
|
|
prepareCurrentItemForHistory()
|
2021-12-02 20:19:10 +00:00
|
|
|
|
currentItem = nil
|
|
|
|
|
player.replaceCurrentItem(with: nil)
|
|
|
|
|
}
|
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
|
|
|
|
|
|
|
|
|
doClosePiP(wasPlaying: wasPlaying)
|
2021-12-19 17:17:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-19 23:36:12 +00:00
|
|
|
|
#if os(tvOS)
|
|
|
|
|
private func doClosePiP(wasPlaying: Bool) {
|
|
|
|
|
let item = player.currentItem
|
|
|
|
|
let time = player.currentTime()
|
2021-12-19 17:17:04 +00:00
|
|
|
|
|
2021-12-19 23:36:12 +00:00
|
|
|
|
self.player.replaceCurrentItem(with: nil)
|
2021-12-19 17:17:04 +00:00
|
|
|
|
|
2021-12-19 23:36:12 +00:00
|
|
|
|
guard !item.isNil else {
|
|
|
|
|
return
|
|
|
|
|
}
|
2021-12-19 17:17:04 +00:00
|
|
|
|
|
2021-12-19 23:36:12 +00:00
|
|
|
|
self.player.seek(to: time)
|
|
|
|
|
self.player.replaceCurrentItem(with: item)
|
2021-12-19 17:17:04 +00:00
|
|
|
|
|
2021-12-19 23:36:12 +00:00
|
|
|
|
guard wasPlaying else {
|
|
|
|
|
return
|
|
|
|
|
}
|
2021-12-19 17:17:04 +00:00
|
|
|
|
|
2021-12-19 23:36:12 +00:00
|
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 1) { [weak self] in
|
|
|
|
|
self?.play()
|
|
|
|
|
}
|
2021-12-19 17:17:04 +00:00
|
|
|
|
}
|
2021-12-19 23:36:12 +00:00
|
|
|
|
#else
|
|
|
|
|
private func doClosePiP(wasPlaying: Bool) {
|
|
|
|
|
controller?.playerView.player = nil
|
|
|
|
|
controller?.playerView.player = player
|
2021-12-19 17:17:04 +00:00
|
|
|
|
|
2021-12-19 23:36:12 +00:00
|
|
|
|
guard wasPlaying else {
|
|
|
|
|
return
|
|
|
|
|
}
|
2021-12-19 17:17:04 +00:00
|
|
|
|
|
2021-12-19 23:36:12 +00:00
|
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { [weak self] in
|
|
|
|
|
self?.play()
|
|
|
|
|
}
|
2021-12-19 17:17:04 +00:00
|
|
|
|
}
|
2021-12-19 23:36:12 +00:00
|
|
|
|
#endif
|
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() {
|
|
|
|
|
guard !playingFullscreen else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logger.info("entering fullscreen")
|
|
|
|
|
|
|
|
|
|
controller?.playerView
|
|
|
|
|
.perform(NSSelectorFromString("enterFullScreenAnimated:completionHandler:"), with: false, with: nil)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func exitFullScreen() {
|
|
|
|
|
guard playingFullscreen else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logger.info("exiting fullscreen")
|
|
|
|
|
|
|
|
|
|
controller?.playerView
|
|
|
|
|
.perform(NSSelectorFromString("exitFullScreenAnimated:completionHandler:"), with: false, with: nil)
|
|
|
|
|
}
|
2021-12-19 17:17:04 +00:00
|
|
|
|
#endif
|
2021-06-14 18:05:02 +00:00
|
|
|
|
}
|