mirror of
https://github.com/yattee/yattee.git
synced 2025-08-06 10:44:06 +00:00
@@ -59,6 +59,7 @@ final class AVPlayerBackend: PlayerBackend {
|
||||
var controller: AppleAVPlayerViewController?
|
||||
#endif
|
||||
var startPictureInPictureOnPlay = false
|
||||
var startPictureInPictureOnSwitch = false
|
||||
|
||||
private var asset: AVURLAsset?
|
||||
private var composition = AVMutableComposition()
|
||||
@@ -124,6 +125,7 @@ final class AVPlayerBackend: PlayerBackend {
|
||||
}
|
||||
|
||||
avPlayer.play()
|
||||
model.objectWillChange.send()
|
||||
}
|
||||
|
||||
func pause() {
|
||||
@@ -132,6 +134,7 @@ final class AVPlayerBackend: PlayerBackend {
|
||||
}
|
||||
|
||||
avPlayer.pause()
|
||||
model.objectWillChange.send()
|
||||
}
|
||||
|
||||
func togglePlay() {
|
||||
@@ -147,7 +150,7 @@ final class AVPlayerBackend: PlayerBackend {
|
||||
|
||||
avPlayer.seek(
|
||||
to: time,
|
||||
toleranceBefore: .secondsInDefaultTimescale(1),
|
||||
toleranceBefore: .zero,
|
||||
toleranceAfter: .zero,
|
||||
completionHandler: completionHandler ?? { _ in }
|
||||
)
|
||||
@@ -165,6 +168,8 @@ final class AVPlayerBackend: PlayerBackend {
|
||||
|
||||
func closeItem() {
|
||||
avPlayer.replaceCurrentItem(with: nil)
|
||||
video = nil
|
||||
stream = nil
|
||||
}
|
||||
|
||||
func closePiP() {
|
||||
@@ -294,6 +299,7 @@ final class AVPlayerBackend: PlayerBackend {
|
||||
}
|
||||
|
||||
if !preservingTime,
|
||||
!self.model.transitioningToPiP,
|
||||
let segment = self.model.sponsorBlock.segments.first,
|
||||
segment.start < 3,
|
||||
self.model.lastSkipped.isNil
|
||||
@@ -434,11 +440,37 @@ final class AVPlayerBackend: PlayerBackend {
|
||||
|
||||
switch playerItem.status {
|
||||
case .readyToPlay:
|
||||
if self.model.playingInPictureInPicture {
|
||||
self.startPictureInPictureOnSwitch = false
|
||||
self.startPictureInPictureOnPlay = false
|
||||
}
|
||||
if self.model.activeBackend == .appleAVPlayer,
|
||||
self.isAutoplaying(playerItem)
|
||||
{
|
||||
self.model.updateAspectRatio()
|
||||
self.model.play()
|
||||
|
||||
if self.startPictureInPictureOnPlay,
|
||||
let controller = self.model.pipController,
|
||||
controller.isPictureInPicturePossible
|
||||
{
|
||||
self.tryStartingPictureInPicture()
|
||||
} else {
|
||||
self.model.play()
|
||||
}
|
||||
} else if self.startPictureInPictureOnPlay {
|
||||
self.startPictureInPictureOnPlay = false
|
||||
self.model.stream = self.stream
|
||||
self.model.streamSelection = self.stream
|
||||
|
||||
if self.model.activeBackend != .appleAVPlayer {
|
||||
self.startPictureInPictureOnSwitch = true
|
||||
let seconds = self.model.mpvBackend.currentTime?.seconds ?? 0
|
||||
self.seek(to: seconds) { finished in
|
||||
guard finished else { return }
|
||||
self.model.pause()
|
||||
self.model.changeActiveBackend(from: .mpv, to: .appleAVPlayer, changingStream: false)
|
||||
}
|
||||
}
|
||||
}
|
||||
case .failed:
|
||||
DispatchQueue.main.async {
|
||||
@@ -483,7 +515,7 @@ final class AVPlayerBackend: PlayerBackend {
|
||||
forInterval: interval,
|
||||
queue: .main
|
||||
) { [weak self] _ in
|
||||
guard let self = self else {
|
||||
guard let self = self, self.model.activeBackend == .appleAVPlayer else {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -511,6 +543,7 @@ final class AVPlayerBackend: PlayerBackend {
|
||||
if self.controlsUpdates {
|
||||
self.playerTime.duration = self.playerItemDuration ?? .zero
|
||||
self.playerTime.currentTime = self.currentTime ?? .zero
|
||||
self.model.objectWillChange.send()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -553,17 +586,6 @@ final class AVPlayerBackend: PlayerBackend {
|
||||
}
|
||||
|
||||
if player.timeControlStatus != .waitingToPlayAtSpecifiedRate {
|
||||
if let controller = self.model.pipController {
|
||||
if controller.isPictureInPicturePossible {
|
||||
if self.startPictureInPictureOnPlay {
|
||||
self.startPictureInPictureOnPlay = false
|
||||
DispatchQueue.main.async {
|
||||
self.model.pipController?.startPictureInPicture()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
DispatchQueue.main.async { [weak self] in
|
||||
self?.model.objectWillChange.send()
|
||||
}
|
||||
@@ -598,10 +620,12 @@ final class AVPlayerBackend: PlayerBackend {
|
||||
}
|
||||
logger.info("starting controls updates")
|
||||
controlsUpdates = true
|
||||
model.objectWillChange.send()
|
||||
}
|
||||
|
||||
func stopControlsUpdates() {
|
||||
controlsUpdates = false
|
||||
model.objectWillChange.send()
|
||||
}
|
||||
|
||||
func startMusicMode() {
|
||||
@@ -633,13 +657,33 @@ final class AVPlayerBackend: PlayerBackend {
|
||||
}
|
||||
|
||||
func didChangeTo() {
|
||||
if model.musicMode {
|
||||
if startPictureInPictureOnSwitch {
|
||||
startPictureInPictureOnSwitch = false
|
||||
tryStartingPictureInPicture()
|
||||
} else if model.musicMode {
|
||||
startMusicMode()
|
||||
} else {
|
||||
stopMusicMode()
|
||||
}
|
||||
}
|
||||
|
||||
func tryStartingPictureInPicture() {
|
||||
guard let controller = model.pipController else { return }
|
||||
|
||||
var opened = false
|
||||
for delay in [0.1, 0.3, 0.5, 1, 2, 3, 5] {
|
||||
Delay.by(delay) {
|
||||
guard !opened else { return }
|
||||
if controller.isPictureInPicturePossible {
|
||||
opened = true
|
||||
controller.startPictureInPicture()
|
||||
} else {
|
||||
print("PiP not possible, waited \(delay) seconds")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func setNeedsDrawing(_: Bool) {}
|
||||
func setSize(_: Double, _: Double) {}
|
||||
func setNeedsNetworkStateUpdates(_: Bool) {}
|
||||
|
@@ -169,7 +169,7 @@ final class MPVBackend: PlayerBackend {
|
||||
stream.resolution != .unknown && stream.format != .av1
|
||||
}
|
||||
|
||||
func playStream(_ stream: Stream, of video: Video, preservingTime: Bool, upgrading _: Bool) {
|
||||
func playStream(_ stream: Stream, of video: Video, preservingTime: Bool, upgrading: Bool) {
|
||||
#if !os(macOS)
|
||||
if model.presentingPlayer {
|
||||
UIApplication.shared.isIdleTimerDisabled = true
|
||||
@@ -204,6 +204,7 @@ final class MPVBackend: PlayerBackend {
|
||||
self.startClientUpdates()
|
||||
|
||||
if !preservingTime,
|
||||
!upgrading,
|
||||
let segment = self.model.sponsorBlock.segments.first,
|
||||
self.model.lastSkipped.isNil
|
||||
{
|
||||
@@ -325,6 +326,8 @@ final class MPVBackend: PlayerBackend {
|
||||
func closeItem() {
|
||||
client?.pause()
|
||||
client?.stop()
|
||||
self.video = nil
|
||||
self.stream = nil
|
||||
}
|
||||
|
||||
func closePiP() {}
|
||||
|
@@ -1,4 +1,5 @@
|
||||
import AVKit
|
||||
import Defaults
|
||||
import Foundation
|
||||
import SwiftUI
|
||||
|
||||
@@ -15,16 +16,21 @@ final class PiPDelegate: NSObject, AVPictureInPictureControllerDelegate {
|
||||
func pictureInPictureControllerWillStartPictureInPicture(_: AVPictureInPictureController) {}
|
||||
|
||||
func pictureInPictureControllerDidStartPictureInPicture(_: AVPictureInPictureController) {
|
||||
player?.playingInPictureInPicture = true
|
||||
player?.avPlayerBackend.startPictureInPictureOnPlay = false
|
||||
guard let player = player else { return }
|
||||
|
||||
player.playingInPictureInPicture = true
|
||||
player.avPlayerBackend.startPictureInPictureOnPlay = false
|
||||
player.avPlayerBackend.startPictureInPictureOnSwitch = false
|
||||
player.controls.objectWillChange.send()
|
||||
|
||||
if Defaults[.closePlayerOnOpeningPiP] { Delay.by(0.1) { player.hide() } }
|
||||
}
|
||||
|
||||
func pictureInPictureControllerDidStopPictureInPicture(_: AVPictureInPictureController) {
|
||||
guard let player = player else {
|
||||
return
|
||||
}
|
||||
guard let player = player else { return }
|
||||
|
||||
player.playingInPictureInPicture = false
|
||||
player.controls.objectWillChange.send()
|
||||
}
|
||||
|
||||
func pictureInPictureControllerWillStopPictureInPicture(_: AVPictureInPictureController) {}
|
||||
|
@@ -126,26 +126,6 @@ final class PlayerControlsModel: ObservableObject {
|
||||
}
|
||||
}
|
||||
|
||||
func startPiP(startImmediately: Bool = true) {
|
||||
player?.avPlayerBackend.startPictureInPictureOnPlay = true
|
||||
|
||||
#if !os(macOS)
|
||||
player.exitFullScreen()
|
||||
#endif
|
||||
|
||||
if player.activeBackend != PlayerBackendType.appleAVPlayer {
|
||||
player.saveTime { [weak player] in
|
||||
player?.changeActiveBackend(from: .mpv, to: .appleAVPlayer)
|
||||
}
|
||||
}
|
||||
|
||||
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) { [weak player] in
|
||||
if startImmediately {
|
||||
player?.pipController?.startPictureInPicture()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func removeTimer() {
|
||||
timer?.invalidate()
|
||||
timer = nil
|
||||
|
@@ -272,6 +272,9 @@ final class PlayerModel: ObservableObject {
|
||||
Orientation.lockOrientation(.allButUpsideDown)
|
||||
}
|
||||
#endif
|
||||
#if os(macOS)
|
||||
Windows.player.hide()
|
||||
#endif
|
||||
}
|
||||
|
||||
func togglePlayer() {
|
||||
@@ -280,6 +283,13 @@ final class PlayerModel: ObservableObject {
|
||||
Windows.player.open()
|
||||
}
|
||||
Windows.player.focus()
|
||||
|
||||
if Windows.player.visible,
|
||||
closePiPOnOpeningPlayer
|
||||
{
|
||||
closePiP()
|
||||
}
|
||||
|
||||
#else
|
||||
if presentingPlayer {
|
||||
hide()
|
||||
@@ -398,7 +408,8 @@ final class PlayerModel: ObservableObject {
|
||||
_ stream: Stream,
|
||||
of video: Video,
|
||||
preservingTime: Bool = false,
|
||||
upgrading: Bool = false
|
||||
upgrading: Bool = false,
|
||||
withBackend: PlayerBackend? = nil
|
||||
) {
|
||||
playerError = nil
|
||||
if !upgrading {
|
||||
@@ -420,9 +431,7 @@ final class PlayerModel: ObservableObject {
|
||||
}
|
||||
}
|
||||
|
||||
playerTime.reset()
|
||||
|
||||
backend.playStream(
|
||||
(withBackend ?? backend).playStream(
|
||||
stream,
|
||||
of: video,
|
||||
preservingTime: preservingTime,
|
||||
@@ -515,15 +524,13 @@ final class PlayerModel: ObservableObject {
|
||||
}
|
||||
}
|
||||
|
||||
func changeActiveBackend(from: PlayerBackendType, to: PlayerBackendType) {
|
||||
func changeActiveBackend(from: PlayerBackendType, to: PlayerBackendType, changingStream: Bool = true) {
|
||||
guard activeBackend != to else {
|
||||
return
|
||||
}
|
||||
|
||||
logger.info("changing backend from \(from.rawValue) to \(to.rawValue)")
|
||||
|
||||
pause()
|
||||
|
||||
if to == .mpv {
|
||||
closePiP()
|
||||
}
|
||||
@@ -531,15 +538,17 @@ final class PlayerModel: ObservableObject {
|
||||
Defaults[.activeBackend] = to
|
||||
self.activeBackend = to
|
||||
|
||||
self.backend.didChangeTo()
|
||||
|
||||
guard var stream = stream else {
|
||||
return
|
||||
}
|
||||
|
||||
let fromBackend: PlayerBackend = from == .appleAVPlayer ? avPlayerBackend : mpvBackend
|
||||
let toBackend: PlayerBackend = to == .appleAVPlayer ? avPlayerBackend : mpvBackend
|
||||
|
||||
self.backend.didChangeTo()
|
||||
|
||||
fromBackend.pause()
|
||||
|
||||
guard var stream = stream, changingStream else {
|
||||
return
|
||||
}
|
||||
|
||||
if let stream = toBackend.stream, toBackend.video == fromBackend.video {
|
||||
toBackend.seek(to: fromBackend.currentTime?.seconds ?? .zero) { finished in
|
||||
guard finished else {
|
||||
@@ -610,11 +619,54 @@ final class PlayerModel: ObservableObject {
|
||||
#endif
|
||||
}
|
||||
|
||||
func startPiP() {
|
||||
avPlayerBackend.startPictureInPictureOnPlay = false
|
||||
avPlayerBackend.startPictureInPictureOnSwitch = false
|
||||
|
||||
if activeBackend == .appleAVPlayer {
|
||||
avPlayerBackend.tryStartingPictureInPicture()
|
||||
return
|
||||
}
|
||||
|
||||
guard let video = currentVideo else { return }
|
||||
guard let stream = avPlayerBackend.bestPlayable(availableStreams, maxResolution: .hd720p30) else { return }
|
||||
|
||||
exitFullScreen()
|
||||
|
||||
if avPlayerBackend.video == video {
|
||||
if activeBackend != .appleAVPlayer {
|
||||
avPlayerBackend.startPictureInPictureOnSwitch = true
|
||||
changeActiveBackend(from: activeBackend, to: .appleAVPlayer)
|
||||
}
|
||||
} else {
|
||||
avPlayerBackend.startPictureInPictureOnPlay = true
|
||||
playStream(stream, of: video, preservingTime: true, upgrading: true, withBackend: avPlayerBackend)
|
||||
}
|
||||
|
||||
controls.objectWillChange.send()
|
||||
}
|
||||
|
||||
var transitioningToPiP: Bool {
|
||||
avPlayerBackend.startPictureInPictureOnPlay || avPlayerBackend.startPictureInPictureOnSwitch
|
||||
}
|
||||
|
||||
var pipPossible: Bool {
|
||||
guard activeBackend == .appleAVPlayer else { return !transitioningToPiP }
|
||||
|
||||
guard let pipController = pipController else { return false }
|
||||
guard !pipController.isPictureInPictureActive else { return true }
|
||||
|
||||
return pipController.isPictureInPicturePossible && !transitioningToPiP
|
||||
}
|
||||
|
||||
func closePiP() {
|
||||
guard playingInPictureInPicture else {
|
||||
return
|
||||
}
|
||||
|
||||
avPlayerBackend.startPictureInPictureOnPlay = false
|
||||
avPlayerBackend.startPictureInPictureOnSwitch = false
|
||||
|
||||
#if os(tvOS)
|
||||
show()
|
||||
#endif
|
||||
|
Reference in New Issue
Block a user