1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-13 22:00:31 +05:30
yattee/Model/Player/Backends/MPVBackend.swift

388 lines
10 KiB
Swift
Raw Normal View History

2022-02-17 01:53:11 +05:30
import AVFAudio
import CoreMedia
import Defaults
2022-02-17 01:53:11 +05:30
import Foundation
import Logging
import SwiftUI
final class MPVBackend: PlayerBackend {
private var logger = Logger(label: "mpv-backend")
var model: PlayerModel!
var controls: PlayerControlsModel!
var stream: Stream?
var video: Video?
var currentTime: CMTime?
var loadedVideo = false
2022-02-28 02:01:17 +05:30
var isLoadingVideo = true { didSet {
DispatchQueue.main.async { [weak self] in
2022-04-03 20:16:33 +05:30
guard let self = self else {
return
}
self.controls.isLoadingVideo = self.isLoadingVideo
if !self.isLoadingVideo {
DispatchQueue.main.asyncAfter(deadline: .now() + 2) { [weak self] in
self?.handleEOF = true
}
}
2022-02-28 02:01:17 +05:30
}
}}
2022-02-17 01:53:11 +05:30
var isPlaying = true { didSet {
if isPlaying {
startClientUpdates()
} else {
stopControlsUpdates()
}
updateControlsIsPlaying()
2022-04-03 20:33:56 +05:30
#if !os(macOS)
2022-05-21 02:53:14 +05:30
DispatchQueue.main.async {
UIApplication.shared.isIdleTimerDisabled = self.model.presentingPlayer && self.isPlaying
}
2022-04-03 20:33:56 +05:30
#endif
2022-02-17 01:53:11 +05:30
}}
var playerItemDuration: CMTime?
2022-02-28 02:01:17 +05:30
#if !os(macOS)
var controller: MPVViewController!
#endif
2022-02-17 01:53:11 +05:30
var client: MPVClient! { didSet { client.backend = self } }
private var clientTimer: RepeatingTimer!
2022-04-03 20:16:33 +05:30
private var handleEOF = false
2022-02-17 01:53:11 +05:30
private var onFileLoaded: (() -> Void)?
private var controlsUpdates = false
private var timeObserverThrottle = Throttle(interval: 2)
init(model: PlayerModel, controls: PlayerControlsModel? = nil) {
self.model = model
self.controls = controls
clientTimer = .init(timeInterval: 1)
clientTimer.eventHandler = getClientUpdates
}
typealias AreInIncreasingOrder = (Stream, Stream) -> Bool
2022-03-28 00:29:22 +05:30
func bestPlayable(_ streams: [Stream], maxResolution: ResolutionSetting) -> Stream? {
streams
2022-05-22 01:08:26 +05:30
.filter { $0.kind != .hls && $0.resolution <= maxResolution.value }
.max { lhs, rhs in
let predicates: [AreInIncreasingOrder] = [
2022-05-22 01:08:26 +05:30
{ $0.resolution < $1.resolution },
{ $0.format > $1.format }
]
for predicate in predicates {
if !predicate(lhs, rhs), !predicate(rhs, lhs) {
continue
}
return predicate(lhs, rhs)
}
return false
} ??
2022-02-17 01:53:11 +05:30
streams.first { $0.kind == .hls } ??
streams.first
}
func canPlay(_ stream: Stream) -> Bool {
stream.resolution != .unknown && stream.format != .av1
2022-02-17 01:53:11 +05:30
}
func playStream(_ stream: Stream, of video: Video, preservingTime: Bool, upgrading _: Bool) {
2022-04-03 20:16:33 +05:30
handleEOF = false
2022-04-03 20:33:56 +05:30
#if !os(macOS)
if model.presentingPlayer {
UIApplication.shared.isIdleTimerDisabled = true
}
#endif
2022-02-17 01:53:11 +05:30
let updateCurrentStream = {
DispatchQueue.main.async { [weak self] in
self?.stream = stream
self?.video = video
self?.model.stream = stream
}
}
let startPlaying = {
#if !os(macOS)
try? AVAudioSession.sharedInstance().setActive(true)
#endif
DispatchQueue.main.async { [weak self] in
guard let self = self else {
return
}
self.startClientUpdates()
if !preservingTime,
let segment = self.model.sponsorBlock.segments.first,
segment.start > 4,
2022-02-17 01:53:11 +05:30
self.model.lastSkipped.isNil
{
self.seek(to: segment.endTime) { finished in
guard finished else {
return
}
self.model.lastSkipped = segment
self.play()
}
} else {
self.play()
}
}
}
let replaceItem: (CMTime?) -> Void = { [weak self] time in
guard let self = self else {
return
}
self.stop()
if let url = stream.singleAssetURL {
2022-02-28 02:01:17 +05:30
self.onFileLoaded = {
2022-02-17 01:53:11 +05:30
updateCurrentStream()
startPlaying()
}
self.client.loadFile(url, time: time) { [weak self] _ in
2022-02-28 02:01:17 +05:30
self?.isLoadingVideo = true
2022-02-17 01:53:11 +05:30
}
} else {
self.onFileLoaded = { [weak self] in
2022-02-28 02:01:17 +05:30
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
self?.client.addAudio(stream.audioAsset.url) { _ in
updateCurrentStream()
startPlaying()
}
2022-02-17 01:53:11 +05:30
}
}
self.client.loadFile(stream.videoAsset.url, time: time) { [weak self] _ in
2022-02-28 02:01:17 +05:30
self?.isLoadingVideo = true
2022-02-17 01:53:11 +05:30
self?.pause()
}
}
}
if preservingTime {
if model.preservedTime.isNil {
model.saveTime {
replaceItem(self.model.preservedTime)
}
} else {
replaceItem(self.model.preservedTime)
}
} else {
replaceItem(nil)
}
2022-03-28 00:54:32 +05:30
startClientUpdates()
2022-02-17 01:53:11 +05:30
}
func play() {
isPlaying = true
startClientUpdates()
2022-03-27 17:12:20 +05:30
if controls.presentingControls {
startControlsUpdates()
}
2022-05-22 02:28:11 +05:30
setRate(model.currentRate)
2022-02-17 01:53:11 +05:30
client?.play()
}
func pause() {
isPlaying = false
stopClientUpdates()
client?.pause()
}
func togglePlay() {
isPlaying ? pause() : play()
}
func stop() {
client?.stop()
}
func seek(to time: CMTime, completionHandler: ((Bool) -> Void)?) {
client.seek(to: time) { [weak self] _ in
self?.getClientUpdates()
self?.updateControls()
completionHandler?(true)
}
}
func seek(relative time: CMTime, completionHandler: ((Bool) -> Void)? = nil) {
client.seek(relative: time) { [weak self] _ in
self?.getClientUpdates()
self?.updateControls()
completionHandler?(true)
}
}
2022-04-17 02:20:37 +05:30
func setRate(_ rate: Float) {
2022-05-21 02:50:18 +05:30
client?.setDoubleAsync("speed", Double(rate))
2022-02-17 01:53:11 +05:30
}
func closeItem() {
handleEOF = false
client?.pause()
client?.stop()
}
2022-02-17 01:53:11 +05:30
func enterFullScreen() {
model.toggleFullscreen(controls?.playingFullscreen ?? false)
2022-05-29 17:59:43 +05:30
#if os(iOS)
if Defaults[.lockLandscapeWhenEnteringFullscreen] {
Orientation.lockOrientation(.landscape, andRotateTo: UIDevice.current.orientation.isLandscape ? nil : .landscapeRight)
}
2022-05-29 17:59:43 +05:30
#endif
}
2022-02-17 01:53:11 +05:30
func exitFullScreen() {}
func closePiP(wasPlaying _: Bool) {}
func updateControls() {
DispatchQueue.main.async { [weak self] in
self?.logger.info("updating controls")
self?.controls.currentTime = self?.currentTime ?? .zero
self?.controls.duration = self?.playerItemDuration ?? .zero
}
}
func startControlsUpdates() {
self.logger.info("starting controls updates")
controlsUpdates = true
}
func stopControlsUpdates() {
self.logger.info("stopping controls updates")
controlsUpdates = false
}
func startClientUpdates() {
clientTimer.resume()
}
2022-04-17 15:02:04 +05:30
private var handleSegmentsThrottle = Throttle(interval: 1)
2022-02-17 01:53:11 +05:30
private func getClientUpdates() {
self.logger.info("getting client updates")
currentTime = client?.currentTime
playerItemDuration = client?.duration
if controlsUpdates {
updateControls()
}
2022-02-17 02:40:57 +05:30
model.updateNowPlayingInfo()
2022-04-17 15:02:04 +05:30
handleSegmentsThrottle.execute {
if let currentTime = currentTime {
model.handleSegments(at: currentTime)
}
2022-02-17 01:53:11 +05:30
}
2022-02-17 02:40:57 +05:30
timeObserverThrottle.execute {
2022-02-17 01:53:11 +05:30
self.model.updateWatch()
}
}
private func stopClientUpdates() {
clientTimer.suspend()
}
private func updateControlsIsPlaying() {
DispatchQueue.main.async { [weak self] in
2022-03-29 21:01:27 +05:30
self?.controls?.isPlaying = self?.isPlaying ?? false
2022-02-17 01:53:11 +05:30
}
}
func handle(_ event: UnsafePointer<mpv_event>!) {
logger.info("\(String(cString: mpv_event_name(event.pointee.event_id)))")
switch event.pointee.event_id {
case MPV_EVENT_SHUTDOWN:
mpv_destroy(client.mpv)
client.mpv = nil
case MPV_EVENT_LOG_MESSAGE:
let logmsg = UnsafeMutablePointer<mpv_event_log_message>(OpaquePointer(event.pointee.data))
logger.info(.init(stringLiteral: "log: \(String(cString: (logmsg!.pointee.prefix)!)), "
+ "\(String(cString: (logmsg!.pointee.level)!)), "
+ "\(String(cString: (logmsg!.pointee.text)!))"))
case MPV_EVENT_FILE_LOADED:
onFileLoaded?()
2022-03-20 04:35:09 +05:30
startClientUpdates()
2022-02-17 01:53:11 +05:30
onFileLoaded = nil
2022-03-28 00:52:13 +05:30
case MPV_EVENT_PLAYBACK_RESTART:
isLoadingVideo = false
onFileLoaded?()
startClientUpdates()
onFileLoaded = nil
2022-02-28 02:01:17 +05:30
case MPV_EVENT_UNPAUSE:
isLoadingVideo = false
2022-02-17 01:53:11 +05:30
case MPV_EVENT_END_FILE:
2022-02-28 01:55:29 +05:30
DispatchQueue.main.async { [weak self] in
self?.handleEndOfFile(event)
}
2022-02-17 01:53:11 +05:30
default:
logger.info(.init(stringLiteral: "event: \(String(cString: mpv_event_name(event.pointee.event_id)))"))
}
}
func handleEndOfFile(_: UnsafePointer<mpv_event>!) {
2022-04-03 20:16:33 +05:30
guard handleEOF, !isLoadingVideo else {
2022-02-17 01:53:11 +05:30
return
}
model.prepareCurrentItemForHistory(finished: true)
if model.queue.isEmpty {
#if !os(macOS)
try? AVAudioSession.sharedInstance().setActive(false)
#endif
model.resetQueue()
model.hide()
} else {
model.advanceToNextItem()
}
}
func setNeedsDrawing(_ needsDrawing: Bool) {
client?.setNeedsDrawing(needsDrawing)
}
2022-03-27 17:12:20 +05:30
func setSize(_ width: Double, _ height: Double) {
self.client?.setSize(width, height)
}
2022-02-17 01:53:11 +05:30
}