2022-02-17 01:53:11 +05:30
|
|
|
import CoreMedia
|
|
|
|
import Defaults
|
|
|
|
import Foundation
|
2022-08-28 22:48:49 +05:30
|
|
|
#if !os(macOS)
|
|
|
|
import UIKit
|
|
|
|
#endif
|
2022-02-17 01:53:11 +05:30
|
|
|
|
|
|
|
protocol PlayerBackend {
|
|
|
|
var model: PlayerModel! { get set }
|
|
|
|
var controls: PlayerControlsModel! { get set }
|
2022-06-18 18:09:49 +05:30
|
|
|
var playerTime: PlayerTimeModel! { get set }
|
|
|
|
var networkState: NetworkStateModel! { get set }
|
2022-02-17 01:53:11 +05:30
|
|
|
|
|
|
|
var stream: Stream? { get set }
|
|
|
|
var video: Video? { get set }
|
|
|
|
var currentTime: CMTime? { get }
|
|
|
|
|
|
|
|
var loadedVideo: Bool { get }
|
|
|
|
var isLoadingVideo: Bool { get }
|
|
|
|
|
|
|
|
var isPlaying: Bool { get }
|
2022-06-18 18:09:49 +05:30
|
|
|
var isSeeking: Bool { get }
|
2022-02-17 01:53:11 +05:30
|
|
|
var playerItemDuration: CMTime? { get }
|
|
|
|
|
2022-07-09 05:51:04 +05:30
|
|
|
var aspectRatio: Double { get }
|
2022-08-24 02:59:50 +05:30
|
|
|
var controlsUpdates: Bool { get }
|
2022-07-09 05:51:04 +05:30
|
|
|
|
2022-03-28 00:29:22 +05:30
|
|
|
func bestPlayable(_ streams: [Stream], maxResolution: ResolutionSetting) -> Stream?
|
2022-02-17 01:53:11 +05:30
|
|
|
func canPlay(_ stream: Stream) -> Bool
|
|
|
|
|
|
|
|
func playStream(
|
|
|
|
_ stream: Stream,
|
|
|
|
of video: Video,
|
|
|
|
preservingTime: Bool,
|
|
|
|
upgrading: Bool
|
|
|
|
)
|
|
|
|
|
|
|
|
func play()
|
|
|
|
func pause()
|
|
|
|
func togglePlay()
|
|
|
|
|
|
|
|
func stop()
|
|
|
|
|
2022-08-28 22:48:49 +05:30
|
|
|
func seek(to time: CMTime, seekType: PlayerTimeModel.SeekType, completionHandler: ((Bool) -> Void)?)
|
|
|
|
func seek(to seconds: Double, seekType: PlayerTimeModel.SeekType, completionHandler: ((Bool) -> Void)?)
|
2022-02-17 01:53:11 +05:30
|
|
|
|
|
|
|
func setRate(_ rate: Float)
|
|
|
|
|
|
|
|
func closeItem()
|
|
|
|
|
2022-08-19 04:10:46 +05:30
|
|
|
func closePiP()
|
2022-02-17 01:53:11 +05:30
|
|
|
|
2022-08-21 02:01:03 +05:30
|
|
|
func startMusicMode()
|
|
|
|
func stopMusicMode()
|
|
|
|
|
2022-08-28 22:48:49 +05:30
|
|
|
func getTimeUpdates()
|
|
|
|
func updateControls(completionHandler: (() -> Void)?)
|
2022-02-17 01:53:11 +05:30
|
|
|
func startControlsUpdates()
|
|
|
|
func stopControlsUpdates()
|
|
|
|
|
2022-08-21 02:01:03 +05:30
|
|
|
func didChangeTo()
|
|
|
|
|
2022-06-25 05:09:29 +05:30
|
|
|
func setNeedsNetworkStateUpdates(_ needsUpdates: Bool)
|
2022-06-18 18:09:49 +05:30
|
|
|
|
2022-02-17 01:53:11 +05:30
|
|
|
func setNeedsDrawing(_ needsDrawing: Bool)
|
2022-03-27 17:12:20 +05:30
|
|
|
func setSize(_ width: Double, _ height: Double)
|
2022-02-17 01:53:11 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
extension PlayerBackend {
|
2022-08-28 22:48:49 +05:30
|
|
|
func seek(to time: CMTime, seekType: PlayerTimeModel.SeekType, completionHandler: ((Bool) -> Void)? = nil) {
|
|
|
|
playerTime.registerSeek(at: time, type: seekType, restore: currentTime)
|
|
|
|
seek(to: time, seekType: seekType, completionHandler: completionHandler)
|
2022-02-17 01:53:11 +05:30
|
|
|
}
|
|
|
|
|
2022-08-28 22:48:49 +05:30
|
|
|
func seek(to seconds: Double, seekType: PlayerTimeModel.SeekType, completionHandler: ((Bool) -> Void)? = nil) {
|
|
|
|
let seconds = CMTime.secondsInDefaultTimescale(seconds)
|
|
|
|
playerTime.registerSeek(at: seconds, type: seekType, restore: currentTime)
|
|
|
|
seek(to: seconds, seekType: seekType, completionHandler: completionHandler)
|
2022-02-17 01:53:11 +05:30
|
|
|
}
|
|
|
|
|
2022-08-28 22:48:49 +05:30
|
|
|
func seek(relative time: CMTime, seekType: PlayerTimeModel.SeekType, completionHandler: ((Bool) -> Void)? = nil) {
|
|
|
|
if let currentTime = currentTime, let duration = playerItemDuration {
|
|
|
|
let seekTime = min(max(0, currentTime.seconds + time.seconds), duration.seconds)
|
|
|
|
playerTime.registerSeek(at: .secondsInDefaultTimescale(seekTime), type: seekType, restore: currentTime)
|
|
|
|
seek(to: seekTime, seekType: seekType, completionHandler: completionHandler)
|
|
|
|
}
|
2022-02-17 01:53:11 +05:30
|
|
|
}
|
2022-07-11 03:54:56 +05:30
|
|
|
|
|
|
|
func eofPlaybackModeAction() {
|
|
|
|
switch model.playbackMode {
|
|
|
|
case .queue, .shuffle:
|
|
|
|
if Defaults[.closeLastItemOnPlaybackEnd] {
|
|
|
|
model.prepareCurrentItemForHistory(finished: true)
|
|
|
|
}
|
|
|
|
|
|
|
|
if model.queue.isEmpty {
|
|
|
|
if Defaults[.closeLastItemOnPlaybackEnd] {
|
|
|
|
model.resetQueue()
|
|
|
|
model.hide()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
model.advanceToNextItem()
|
|
|
|
}
|
|
|
|
case .loopOne:
|
2022-08-28 22:48:49 +05:30
|
|
|
model.backend.seek(to: .zero, seekType: .loopRestart) { _ in
|
2022-07-11 03:54:56 +05:30
|
|
|
self.model.play()
|
|
|
|
}
|
|
|
|
case .related:
|
|
|
|
guard let item = model.autoplayItem else { return }
|
|
|
|
model.resetAutoplay()
|
|
|
|
model.advanceToItem(item)
|
|
|
|
}
|
|
|
|
}
|
2022-08-28 22:48:49 +05:30
|
|
|
|
|
|
|
func updateControls(completionHandler: (() -> Void)? = nil) {
|
|
|
|
print("updating controls")
|
|
|
|
|
|
|
|
guard model.presentingPlayer, !model.controls.presentingOverlays else {
|
|
|
|
print("ignored controls update")
|
|
|
|
completionHandler?()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
DispatchQueue.main.async(qos: .userInteractive) {
|
|
|
|
#if !os(macOS)
|
|
|
|
guard UIApplication.shared.applicationState != .background else {
|
|
|
|
print("not performing controls updates in background")
|
|
|
|
completionHandler?()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
self.playerTime.currentTime = self.currentTime ?? .zero
|
|
|
|
self.playerTime.duration = self.playerItemDuration ?? .zero
|
|
|
|
completionHandler?()
|
|
|
|
}
|
|
|
|
}
|
2022-02-17 01:53:11 +05:30
|
|
|
}
|