2022-02-17 01:53:11 +05:30
|
|
|
import AVFAudio
|
|
|
|
import CoreMedia
|
2022-05-29 03:11:23 +05:30
|
|
|
import Defaults
|
2022-02-17 01:53:11 +05:30
|
|
|
import Foundation
|
|
|
|
import Logging
|
2022-08-21 01:39:26 +05:30
|
|
|
import MediaPlayer
|
2022-06-30 04:13:41 +05:30
|
|
|
import Repeat
|
2022-02-17 01:53:11 +05:30
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
final class MPVBackend: PlayerBackend {
|
2022-08-28 22:48:49 +05:30
|
|
|
static var timeUpdateInterval = 0.5
|
2022-08-25 22:39:55 +05:30
|
|
|
static var networkStateUpdateInterval = 1.0
|
2022-06-16 23:14:39 +05:30
|
|
|
|
2022-02-17 01:53:11 +05:30
|
|
|
private var logger = Logger(label: "mpv-backend")
|
|
|
|
|
2022-11-25 02:06:05 +05:30
|
|
|
var model: PlayerModel { .shared }
|
|
|
|
var controls: PlayerControlsModel { .shared }
|
|
|
|
var playerTime: PlayerTimeModel { .shared }
|
|
|
|
var networkState: NetworkStateModel { .shared }
|
|
|
|
var seek: SeekModel { .shared }
|
2022-02-17 01:53:11 +05:30
|
|
|
|
|
|
|
var stream: Stream?
|
|
|
|
var video: Video?
|
2022-07-05 22:50:25 +05:30
|
|
|
var captions: Captions? { didSet {
|
2022-09-28 19:57:01 +05:30
|
|
|
guard let captions else {
|
2023-02-24 22:49:55 +05:30
|
|
|
client?.removeSubs()
|
2022-07-05 22:50:25 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
addSubTrack(captions.url)
|
|
|
|
}}
|
2022-02-17 01:53:11 +05:30
|
|
|
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-09-28 19:57:01 +05:30
|
|
|
guard let self else {
|
2022-04-03 20:16:33 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-25 02:06:05 +05:30
|
|
|
self.controls.isLoadingVideo = self.isLoadingVideo
|
2022-06-25 05:09:29 +05:30
|
|
|
self.setNeedsNetworkStateUpdates(true)
|
2022-11-25 02:06:05 +05:30
|
|
|
self.model.objectWillChange.send()
|
2022-02-28 02:01:17 +05:30
|
|
|
}
|
|
|
|
}}
|
2022-02-17 01:53:11 +05:30
|
|
|
|
|
|
|
var isPlaying = true { didSet {
|
2022-06-30 04:13:41 +05:30
|
|
|
networkStateTimer.start()
|
2022-06-18 18:09:49 +05:30
|
|
|
|
2022-02-17 01:53:11 +05:30
|
|
|
if isPlaying {
|
|
|
|
startClientUpdates()
|
|
|
|
} else {
|
|
|
|
stopControlsUpdates()
|
|
|
|
}
|
|
|
|
|
|
|
|
updateControlsIsPlaying()
|
2022-04-03 20:33:56 +05:30
|
|
|
|
2022-08-13 19:47:10 +05:30
|
|
|
#if os(macOS)
|
|
|
|
if isPlaying {
|
|
|
|
ScreenSaverManager.shared.disable(reason: "Yattee is playing video")
|
|
|
|
} else {
|
|
|
|
ScreenSaverManager.shared.enable()
|
|
|
|
}
|
2022-08-21 01:39:26 +05:30
|
|
|
|
|
|
|
MPNowPlayingInfoCenter.default().playbackState = isPlaying ? .playing : .paused
|
2022-08-13 19:47:10 +05:30
|
|
|
#else
|
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
|
|
|
}}
|
2022-06-18 18:09:49 +05:30
|
|
|
var isSeeking = false {
|
|
|
|
didSet {
|
|
|
|
DispatchQueue.main.async { [weak self] in
|
2022-09-28 19:57:01 +05:30
|
|
|
guard let self else { return }
|
2022-06-18 18:09:49 +05:30
|
|
|
self.model.isSeeking = self.isSeeking
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 } }
|
|
|
|
|
2022-06-30 04:13:41 +05:30
|
|
|
private var clientTimer: Repeater!
|
|
|
|
private var networkStateTimer: Repeater!
|
2022-02-17 01:53:11 +05:30
|
|
|
|
|
|
|
private var onFileLoaded: (() -> Void)?
|
|
|
|
|
2022-08-24 02:59:50 +05:30
|
|
|
internal var controlsUpdates = false
|
2022-02-17 01:53:11 +05:30
|
|
|
private var timeObserverThrottle = Throttle(interval: 2)
|
|
|
|
|
2022-11-11 03:49:34 +05:30
|
|
|
var suggestedPlaybackRates: [Double] {
|
2022-11-19 04:15:49 +05:30
|
|
|
[0.25, 0.33, 0.5, 0.67, 0.75, 1, 1.25, 1.5, 1.75, 2, 3, 4]
|
2022-11-11 03:49:34 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func canPlayAtRate(_ rate: Double) -> Bool {
|
|
|
|
rate > 0 && rate <= 100
|
|
|
|
}
|
|
|
|
|
2022-06-08 02:57:48 +05:30
|
|
|
var tracks: Int {
|
|
|
|
client?.tracksCount ?? -1
|
|
|
|
}
|
|
|
|
|
2022-07-09 05:51:04 +05:30
|
|
|
var aspectRatio: Double {
|
|
|
|
client?.aspectRatio ?? VideoPlayerView.defaultAspectRatio
|
|
|
|
}
|
|
|
|
|
2022-06-16 23:14:39 +05:30
|
|
|
var frameDropCount: Int {
|
|
|
|
client?.frameDropCount ?? 0
|
|
|
|
}
|
|
|
|
|
2022-06-17 15:57:01 +05:30
|
|
|
var outputFps: Double {
|
|
|
|
client?.outputFps ?? 0
|
|
|
|
}
|
|
|
|
|
2022-11-10 22:41:28 +05:30
|
|
|
var formattedOutputFps: String {
|
|
|
|
String(format: "%.2ffps", outputFps)
|
|
|
|
}
|
|
|
|
|
2022-06-17 15:57:01 +05:30
|
|
|
var hwDecoder: String {
|
|
|
|
client?.hwDecoder ?? "unknown"
|
|
|
|
}
|
|
|
|
|
2022-06-17 17:13:11 +05:30
|
|
|
var bufferingState: Double {
|
|
|
|
client?.bufferingState ?? 0
|
|
|
|
}
|
|
|
|
|
|
|
|
var cacheDuration: Double {
|
|
|
|
client?.cacheDuration ?? 0
|
|
|
|
}
|
|
|
|
|
2022-11-10 22:41:28 +05:30
|
|
|
var videoFormat: String {
|
|
|
|
client?.videoFormat ?? "unknown"
|
|
|
|
}
|
|
|
|
|
|
|
|
var videoCodec: String {
|
|
|
|
client?.videoCodec ?? "unknown"
|
|
|
|
}
|
|
|
|
|
|
|
|
var currentVo: String {
|
|
|
|
client?.currentVo ?? "unknown"
|
|
|
|
}
|
|
|
|
|
|
|
|
var videoWidth: Double? {
|
|
|
|
if let width = client?.width, width != "unknown" {
|
|
|
|
return Double(width)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var videoHeight: Double? {
|
|
|
|
if let height = client?.height, height != "unknown" {
|
|
|
|
return Double(height)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var audioFormat: String {
|
|
|
|
client?.audioFormat ?? "unknown"
|
|
|
|
}
|
|
|
|
|
|
|
|
var audioCodec: String {
|
|
|
|
client?.audioCodec ?? "unknown"
|
|
|
|
}
|
|
|
|
|
|
|
|
var currentAo: String {
|
|
|
|
client?.currentAo ?? "unknown"
|
|
|
|
}
|
|
|
|
|
|
|
|
var audioChannels: String {
|
|
|
|
client?.audioChannels ?? "unknown"
|
|
|
|
}
|
|
|
|
|
|
|
|
var audioSampleRate: String {
|
|
|
|
client?.audioSampleRate ?? "unknown"
|
|
|
|
}
|
|
|
|
|
2022-09-02 04:35:31 +05:30
|
|
|
init() {
|
2022-08-28 22:48:49 +05:30
|
|
|
clientTimer = .init(interval: .seconds(Self.timeUpdateInterval), mode: .infinite) { [weak self] _ in
|
|
|
|
self?.getTimeUpdates()
|
2022-06-30 04:13:41 +05:30
|
|
|
}
|
2022-06-25 05:02:21 +05:30
|
|
|
|
2022-06-30 04:13:41 +05:30
|
|
|
networkStateTimer = .init(interval: .seconds(Self.networkStateUpdateInterval), mode: .infinite) { [weak self] _ in
|
|
|
|
self?.updateNetworkState()
|
|
|
|
}
|
2022-02-17 01:53:11 +05:30
|
|
|
}
|
|
|
|
|
2022-05-22 01:00:01 +05:30
|
|
|
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 }
|
2022-05-22 01:00:01 +05:30
|
|
|
.max { lhs, rhs in
|
|
|
|
let predicates: [AreInIncreasingOrder] = [
|
2022-05-22 01:08:26 +05:30
|
|
|
{ $0.resolution < $1.resolution },
|
|
|
|
{ $0.format > $1.format }
|
2022-05-22 01:00:01 +05:30
|
|
|
]
|
|
|
|
|
|
|
|
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 {
|
2022-05-22 01:00:01 +05:30
|
|
|
stream.resolution != .unknown && stream.format != .av1
|
2022-02-17 01:53:11 +05:30
|
|
|
}
|
|
|
|
|
2022-08-27 01:47:21 +05:30
|
|
|
func playStream(_ stream: Stream, of video: Video, preservingTime: Bool, upgrading: Bool) {
|
2022-04-03 20:33:56 +05:30
|
|
|
#if !os(macOS)
|
|
|
|
if model.presentingPlayer {
|
2023-05-16 22:21:07 +05:30
|
|
|
DispatchQueue.main.async {
|
|
|
|
UIApplication.shared.isIdleTimerDisabled = true
|
|
|
|
}
|
2022-04-03 20:33:56 +05:30
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-07-05 22:50:25 +05:30
|
|
|
var captions: Captions?
|
|
|
|
if let captionsLanguageCode = Defaults[.captionsLanguageCode] {
|
|
|
|
captions = video.captions.first { $0.code == captionsLanguageCode } ??
|
|
|
|
video.captions.first { $0.code.contains(captionsLanguageCode) }
|
|
|
|
}
|
|
|
|
|
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
|
2022-07-05 22:50:25 +05:30
|
|
|
self?.captions = captions
|
2022-02-17 01:53:11 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let startPlaying = {
|
2022-09-24 00:06:10 +05:30
|
|
|
#if !os(macOS)
|
|
|
|
try? AVAudioSession.sharedInstance().setActive(true)
|
|
|
|
#endif
|
|
|
|
|
2022-02-17 01:53:11 +05:30
|
|
|
DispatchQueue.main.async { [weak self] in
|
2022-09-28 19:57:01 +05:30
|
|
|
guard let self else {
|
2022-02-17 01:53:11 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
self.startClientUpdates()
|
|
|
|
|
|
|
|
if !preservingTime,
|
2022-08-27 01:47:21 +05:30
|
|
|
!upgrading,
|
2022-02-17 01:53:11 +05:30
|
|
|
let segment = self.model.sponsorBlock.segments.first,
|
|
|
|
self.model.lastSkipped.isNil
|
|
|
|
{
|
2022-08-28 22:48:49 +05:30
|
|
|
self.seek(to: segment.endTime, seekType: .segmentSkip(segment.category)) { finished in
|
2022-02-17 01:53:11 +05:30
|
|
|
guard finished else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
self.model.lastSkipped = segment
|
|
|
|
self.play()
|
2023-06-08 01:09:03 +05:30
|
|
|
self.model.handleOnPlayStream(stream)
|
2022-02-17 01:53:11 +05:30
|
|
|
}
|
|
|
|
} else {
|
|
|
|
self.play()
|
2023-06-08 01:09:03 +05:30
|
|
|
self.model.handleOnPlayStream(stream)
|
2022-02-17 01:53:11 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let replaceItem: (CMTime?) -> Void = { [weak self] time in
|
2022-09-28 19:57:01 +05:30
|
|
|
guard let self else {
|
2022-02-17 01:53:11 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
self.stop()
|
|
|
|
|
2022-05-30 21:06:26 +05:30
|
|
|
DispatchQueue.main.async { [weak self] in
|
2023-05-08 01:24:48 +05:30
|
|
|
guard let self, let client = self.client else {
|
2022-05-30 21:06:26 +05:30
|
|
|
return
|
2022-02-17 01:53:11 +05:30
|
|
|
}
|
|
|
|
|
2022-05-30 21:06:26 +05:30
|
|
|
if let url = stream.singleAssetURL {
|
|
|
|
self.onFileLoaded = {
|
|
|
|
updateCurrentStream()
|
|
|
|
startPlaying()
|
|
|
|
}
|
|
|
|
|
2022-11-12 07:35:56 +05:30
|
|
|
if video.isLocal, video.localStreamIsFile {
|
2022-12-04 17:51:50 +05:30
|
|
|
if url.startAccessingSecurityScopedResource() {
|
|
|
|
URLBookmarkModel.shared.saveBookmark(url)
|
|
|
|
}
|
2022-11-10 22:41:28 +05:30
|
|
|
}
|
|
|
|
|
2023-05-08 01:24:48 +05:30
|
|
|
client.loadFile(url, sub: captions?.url, time: time, forceSeekable: stream.kind == .hls) { [weak self] _ in
|
2022-05-30 21:06:26 +05:30
|
|
|
self?.isLoadingVideo = true
|
|
|
|
}
|
|
|
|
} else {
|
2022-06-08 02:50:24 +05:30
|
|
|
self.onFileLoaded = {
|
2022-05-30 21:06:26 +05:30
|
|
|
updateCurrentStream()
|
|
|
|
startPlaying()
|
2022-02-17 01:53:11 +05:30
|
|
|
}
|
|
|
|
|
2022-06-08 02:50:24 +05:30
|
|
|
let fileToLoad = self.model.musicMode ? stream.audioAsset.url : stream.videoAsset.url
|
|
|
|
let audioTrack = self.model.musicMode ? nil : stream.audioAsset.url
|
|
|
|
|
2023-05-08 01:24:48 +05:30
|
|
|
client.loadFile(fileToLoad, audio: audioTrack, sub: captions?.url, time: time, forceSeekable: stream.kind == .hls) { [weak self] _ in
|
2022-05-30 21:06:26 +05:30
|
|
|
self?.isLoadingVideo = true
|
|
|
|
self?.pause()
|
|
|
|
}
|
2022-02-17 01:53:11 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-11-25 02:06:05 +05:30
|
|
|
if controls.presentingControls {
|
2022-03-27 17:12:20 +05:30
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2022-11-13 17:58:25 +05:30
|
|
|
func cancelLoads() {
|
|
|
|
stop()
|
|
|
|
}
|
|
|
|
|
2022-02-17 01:53:11 +05:30
|
|
|
func stop() {
|
|
|
|
client?.stop()
|
|
|
|
}
|
|
|
|
|
2022-08-29 17:25:23 +05:30
|
|
|
func seek(to time: CMTime, seekType _: SeekType, completionHandler: ((Bool) -> Void)?) {
|
2022-06-18 18:09:49 +05:30
|
|
|
client?.seek(to: time) { [weak self] _ in
|
2022-08-28 22:48:49 +05:30
|
|
|
self?.getTimeUpdates()
|
2022-02-17 01:53:11 +05:30
|
|
|
self?.updateControls()
|
|
|
|
completionHandler?(true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-11 03:30:17 +05:30
|
|
|
func setRate(_ rate: Double) {
|
|
|
|
client?.setDoubleAsync("speed", rate)
|
2022-02-17 01:53:11 +05:30
|
|
|
}
|
|
|
|
|
2022-05-28 04:29:35 +05:30
|
|
|
func closeItem() {
|
|
|
|
client?.pause()
|
|
|
|
client?.stop()
|
2022-08-27 01:47:21 +05:30
|
|
|
self.video = nil
|
|
|
|
self.stream = nil
|
2022-05-28 04:29:35 +05:30
|
|
|
}
|
2022-02-17 01:53:11 +05:30
|
|
|
|
2022-08-19 04:10:46 +05:30
|
|
|
func closePiP() {}
|
2022-02-17 01:53:11 +05:30
|
|
|
|
|
|
|
func startControlsUpdates() {
|
2022-08-15 03:46:37 +05:30
|
|
|
guard model.presentingPlayer, model.controls.presentingControls, !model.controls.presentingOverlays else {
|
2022-08-14 22:23:03 +05:30
|
|
|
self.logger.info("ignored controls update start")
|
|
|
|
return
|
|
|
|
}
|
2022-02-17 01:53:11 +05:30
|
|
|
self.logger.info("starting controls updates")
|
|
|
|
controlsUpdates = true
|
|
|
|
}
|
|
|
|
|
|
|
|
func stopControlsUpdates() {
|
|
|
|
self.logger.info("stopping controls updates")
|
|
|
|
controlsUpdates = false
|
|
|
|
}
|
|
|
|
|
|
|
|
func startClientUpdates() {
|
2022-06-30 04:13:41 +05:30
|
|
|
clientTimer.start()
|
2022-02-17 01:53:11 +05:30
|
|
|
}
|
|
|
|
|
2022-04-17 15:02:04 +05:30
|
|
|
private var handleSegmentsThrottle = Throttle(interval: 1)
|
|
|
|
|
2022-08-28 22:48:49 +05:30
|
|
|
func getTimeUpdates() {
|
2022-02-17 01:53:11 +05:30
|
|
|
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 {
|
2022-09-28 19:57:01 +05:30
|
|
|
if let currentTime {
|
2022-04-17 15:02:04 +05:30
|
|
|
model.handleSegments(at: currentTime)
|
|
|
|
}
|
2022-02-17 01:53:11 +05:30
|
|
|
}
|
|
|
|
|
2022-02-17 02:40:57 +05:30
|
|
|
timeObserverThrottle.execute {
|
2023-06-08 01:55:10 +05:30
|
|
|
self.model.updateWatch(time: self.currentTime)
|
2022-02-17 01:53:11 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func stopClientUpdates() {
|
2022-06-30 04:13:41 +05:30
|
|
|
clientTimer.pause()
|
2022-02-17 01:53:11 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
private func updateControlsIsPlaying() {
|
2022-11-25 02:06:05 +05:30
|
|
|
guard model.activeBackend == .mpv else { return }
|
2022-02-17 01:53:11 +05:30
|
|
|
DispatchQueue.main.async { [weak self] in
|
2022-11-25 02:06:05 +05:30
|
|
|
self?.controls.isPlaying = self?.isPlaying ?? false
|
2022-02-17 01:53:11 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func handle(_ event: UnsafePointer<mpv_event>!) {
|
2022-06-25 18:44:16 +05:30
|
|
|
logger.info(.init(stringLiteral: "RECEIVED event: \(String(cString: mpv_event_name(event.pointee.event_id)))"))
|
|
|
|
|
2022-02-17 01:53:11 +05:30
|
|
|
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))
|
2022-06-17 15:57:01 +05:30
|
|
|
logger.info(.init(stringLiteral: "\(String(cString: (logmsg!.pointee.prefix)!)), "
|
2022-02-17 01:53:11 +05:30
|
|
|
+ "\(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
|
2022-06-18 18:09:49 +05:30
|
|
|
isSeeking = false
|
2022-03-28 00:52:13 +05:30
|
|
|
|
|
|
|
onFileLoaded?()
|
|
|
|
startClientUpdates()
|
|
|
|
onFileLoaded = nil
|
|
|
|
|
2022-06-18 18:09:49 +05:30
|
|
|
case MPV_EVENT_PAUSE:
|
2022-07-04 02:48:27 +05:30
|
|
|
DispatchQueue.main.async { [weak self] in self?.handleEndOfFile() }
|
2022-06-25 18:44:16 +05:30
|
|
|
isPlaying = false
|
2022-06-30 04:13:41 +05:30
|
|
|
networkStateTimer.start()
|
2022-06-18 18:09:49 +05:30
|
|
|
|
2022-02-28 02:01:17 +05:30
|
|
|
case MPV_EVENT_UNPAUSE:
|
2022-06-25 18:44:16 +05:30
|
|
|
isPlaying = true
|
2022-02-28 02:01:17 +05:30
|
|
|
isLoadingVideo = false
|
2022-06-18 18:09:49 +05:30
|
|
|
isSeeking = false
|
2022-06-30 04:13:41 +05:30
|
|
|
networkStateTimer.start()
|
2022-06-18 18:09:49 +05:30
|
|
|
|
2022-09-01 22:25:38 +05:30
|
|
|
case MPV_EVENT_VIDEO_RECONFIG:
|
|
|
|
model.updateAspectRatio()
|
|
|
|
|
2022-06-18 18:09:49 +05:30
|
|
|
case MPV_EVENT_SEEK:
|
|
|
|
isSeeking = true
|
2022-02-28 02:01:17 +05:30
|
|
|
|
2022-02-17 01:53:11 +05:30
|
|
|
case MPV_EVENT_END_FILE:
|
2022-11-11 02:50:44 +05:30
|
|
|
let reason = event!.pointee.data.load(as: mpv_end_file_reason.self)
|
|
|
|
|
|
|
|
if reason != MPV_END_FILE_REASON_STOP {
|
|
|
|
DispatchQueue.main.async { [weak self] in
|
|
|
|
guard let self else { return }
|
|
|
|
NavigationModel.shared.presentAlert(title: "Error while opening file")
|
|
|
|
self.model.closeCurrentItem(finished: true)
|
|
|
|
self.getTimeUpdates()
|
|
|
|
self.eofPlaybackModeAction()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
DispatchQueue.main.async { [weak self] in self?.handleEndOfFile() }
|
|
|
|
}
|
2022-02-17 01:53:11 +05:30
|
|
|
|
|
|
|
default:
|
2022-06-17 15:57:01 +05:30
|
|
|
logger.info(.init(stringLiteral: "UNHANDLED event: \(String(cString: mpv_event_name(event.pointee.event_id)))"))
|
2022-02-17 01:53:11 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-25 18:44:16 +05:30
|
|
|
func handleEndOfFile() {
|
2022-07-04 02:48:27 +05:30
|
|
|
guard client.eofReached else {
|
2022-02-17 01:53:11 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-28 22:48:49 +05:30
|
|
|
getTimeUpdates()
|
2022-07-11 03:54:56 +05:30
|
|
|
eofPlaybackModeAction()
|
2022-02-17 01:53:11 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func setNeedsDrawing(_ needsDrawing: Bool) {
|
|
|
|
client?.setNeedsDrawing(needsDrawing)
|
|
|
|
}
|
2022-03-27 17:12:20 +05:30
|
|
|
|
|
|
|
func setSize(_ width: Double, _ height: Double) {
|
2022-06-18 18:09:49 +05:30
|
|
|
client?.setSize(width, height)
|
2022-03-27 17:12:20 +05:30
|
|
|
}
|
2022-06-08 02:50:24 +05:30
|
|
|
|
|
|
|
func addVideoTrack(_ url: URL) {
|
2022-06-18 18:09:49 +05:30
|
|
|
client?.addVideoTrack(url)
|
2022-06-08 02:50:24 +05:30
|
|
|
}
|
|
|
|
|
2022-07-05 22:50:25 +05:30
|
|
|
func addSubTrack(_ url: URL) {
|
|
|
|
client?.removeSubs()
|
|
|
|
client?.addSubTrack(url)
|
|
|
|
}
|
|
|
|
|
2022-06-08 02:50:24 +05:30
|
|
|
func setVideoToAuto() {
|
2022-06-18 18:09:49 +05:30
|
|
|
client?.setVideoToAuto()
|
2022-06-08 02:50:24 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func setVideoToNo() {
|
2022-06-18 18:09:49 +05:30
|
|
|
client?.setVideoToNo()
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateNetworkState() {
|
2022-11-25 02:06:05 +05:30
|
|
|
DispatchQueue.main.async { [weak self] in
|
2023-02-24 22:49:55 +05:30
|
|
|
guard let self, let client = self.client else { return }
|
|
|
|
self.networkState.pausedForCache = client.pausedForCache
|
|
|
|
self.networkState.cacheDuration = client.cacheDuration
|
|
|
|
self.networkState.bufferingState = client.bufferingState
|
2022-06-18 18:09:49 +05:30
|
|
|
}
|
|
|
|
|
2022-06-25 05:02:21 +05:30
|
|
|
if !networkState.needsUpdates {
|
2022-06-30 04:13:41 +05:30
|
|
|
networkStateTimer.pause()
|
2022-06-18 18:09:49 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-25 05:09:29 +05:30
|
|
|
func setNeedsNetworkStateUpdates(_ needsUpdates: Bool) {
|
|
|
|
if needsUpdates {
|
2022-06-30 04:13:41 +05:30
|
|
|
networkStateTimer.start()
|
2022-06-25 05:09:29 +05:30
|
|
|
} else {
|
2022-06-30 04:13:41 +05:30
|
|
|
networkStateTimer.pause()
|
2022-06-25 05:09:29 +05:30
|
|
|
}
|
2022-06-08 02:50:24 +05:30
|
|
|
}
|
2022-08-21 02:01:03 +05:30
|
|
|
|
|
|
|
func startMusicMode() {
|
|
|
|
setVideoToNo()
|
|
|
|
}
|
|
|
|
|
|
|
|
func stopMusicMode() {
|
|
|
|
addVideoTrackFromStream()
|
|
|
|
setVideoToAuto()
|
|
|
|
|
|
|
|
controls.resetTimer()
|
|
|
|
}
|
|
|
|
|
|
|
|
func addVideoTrackFromStream() {
|
|
|
|
if let videoTrackURL = model.stream?.videoAsset?.url,
|
|
|
|
tracks < 2
|
|
|
|
{
|
|
|
|
logger.info("adding video track")
|
|
|
|
addVideoTrack(videoTrackURL)
|
|
|
|
}
|
|
|
|
|
|
|
|
setVideoToAuto()
|
|
|
|
}
|
|
|
|
|
|
|
|
func didChangeTo() {
|
2022-09-01 00:50:12 +05:30
|
|
|
setNeedsDrawing(model.presentingPlayer)
|
2022-08-21 02:01:03 +05:30
|
|
|
|
|
|
|
if model.musicMode {
|
|
|
|
startMusicMode()
|
|
|
|
} else {
|
|
|
|
stopMusicMode()
|
|
|
|
}
|
|
|
|
}
|
2022-02-17 01:53:11 +05:30
|
|
|
}
|