1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-15 06:40:32 +05:30
yattee/Apple TV/PlayerViewController.swift

185 lines
5.8 KiB
Swift
Raw Normal View History

2021-06-14 23:35:02 +05:30
import AVKit
import Foundation
2021-06-15 22:05:21 +05:30
import Logging
2021-06-14 23:35:02 +05:30
import SwiftUI
struct PlayerViewController: UIViewControllerRepresentable {
2021-07-12 02:22:49 +05:30
#if os(tvOS)
typealias PlayerController = StreamAVPlayerViewController
#else
typealias PlayerController = AVPlayerViewController
#endif
2021-06-15 22:05:21 +05:30
let logger = Logger(label: "net.arekf.Pearvidious.pvc")
2021-06-14 23:35:02 +05:30
2021-06-16 02:51:57 +05:30
@ObservedObject private var state: PlayerState
2021-06-14 23:35:02 +05:30
2021-06-16 02:51:57 +05:30
var video: Video
2021-06-14 23:35:02 +05:30
init(video: Video) {
self.video = video
2021-06-16 02:51:57 +05:30
state = PlayerState(video)
2021-06-14 23:35:02 +05:30
2021-06-26 17:07:24 +05:30
loadStream(video.defaultStreamForProfile(state.profile), loadBest: state.profile.defaultStreamResolution == .hd720pFirstThenBest)
2021-06-14 23:35:02 +05:30
}
2021-06-17 00:42:41 +05:30
fileprivate func loadStream(_ stream: Stream?, loadBest: Bool = false) {
2021-06-18 15:47:01 +05:30
if stream != state.nextStream {
2021-06-15 22:05:21 +05:30
state.loadStream(stream)
2021-06-16 02:51:57 +05:30
addTracksAndLoadAssets(stream!, loadBest: loadBest)
2021-06-15 22:05:21 +05:30
}
}
2021-06-14 23:35:02 +05:30
2021-06-17 00:42:41 +05:30
fileprivate func addTracksAndLoadAssets(_ stream: Stream, loadBest: Bool = false) {
2021-06-15 22:05:21 +05:30
logger.info("adding tracks and loading assets for: \(stream.type), \(stream.description)")
2021-06-14 23:35:02 +05:30
2021-06-15 22:05:21 +05:30
stream.assets.forEach { asset in
asset.loadValuesAsynchronously(forKeys: ["playable"]) {
handleAssetLoad(stream, type: asset == stream.videoAsset ? .video : .audio, loadBest: loadBest)
2021-06-14 23:35:02 +05:30
}
}
2021-06-15 22:05:21 +05:30
}
2021-06-14 23:35:02 +05:30
2021-06-17 00:42:41 +05:30
fileprivate func handleAssetLoad(_ stream: Stream, type: AVMediaType, loadBest: Bool = false) {
2021-06-15 22:05:21 +05:30
logger.info("handling asset load: \(stream.type), \(stream.description)")
2021-06-16 02:51:57 +05:30
2021-06-15 22:05:21 +05:30
guard stream != state.currentStream else {
logger.warning("IGNORING assets loaded: \(stream.type), \(stream.description)")
return
}
2021-06-16 02:51:57 +05:30
stream.loadedAssets.forEach { asset in
addTrack(asset, stream: stream, type: type)
2021-06-14 23:35:02 +05:30
2021-06-15 22:05:21 +05:30
if stream.assetsLoaded {
2021-06-16 02:51:57 +05:30
DispatchQueue.main.async {
logger.info("ALL assets loaded: \(stream.type), \(stream.description)")
2021-06-14 23:35:02 +05:30
2021-06-17 00:42:41 +05:30
state.playStream(stream)
2021-06-15 22:05:21 +05:30
}
2021-06-14 23:35:02 +05:30
2021-06-15 22:05:21 +05:30
if loadBest {
loadBestStream()
}
2021-06-14 23:35:02 +05:30
}
}
}
2021-06-18 15:47:01 +05:30
fileprivate func addTrack(_ asset: AVURLAsset, stream: Stream, type: AVMediaType? = nil) {
let types: [AVMediaType] = stream.type == .adaptive ? [type!] : [.video, .audio]
types.forEach { state.addTrackToNextComposition(asset, type: $0) }
}
2021-06-17 00:42:41 +05:30
fileprivate func loadBestStream() {
guard state.currentStream != video.bestStream else {
return
}
loadStream(video.bestStream)
}
2021-07-12 02:22:49 +05:30
func makeUIViewController(context _: Context) -> PlayerController {
let controller = PlayerController()
2021-06-14 23:35:02 +05:30
2021-06-17 00:42:41 +05:30
#if os(tvOS)
2021-07-12 02:22:49 +05:30
controller.state = state
2021-06-17 00:42:41 +05:30
controller.transportBarCustomMenuItems = [streamingQualityMenu]
#endif
2021-06-14 23:35:02 +05:30
controller.modalPresentationStyle = .fullScreen
2021-06-16 02:51:57 +05:30
controller.player = state.player
2021-06-14 23:35:02 +05:30
return controller
}
2021-07-12 02:22:49 +05:30
func updateUIViewController(_ controller: PlayerController, context _: Context) {
2021-06-15 22:05:21 +05:30
var items: [UIMenuElement] = []
2021-06-18 15:47:01 +05:30
if state.nextStream != nil {
2021-06-15 22:05:21 +05:30
items.append(actionsMenu)
}
2021-06-20 01:40:14 +05:30
items.append(playbackRateMenu)
2021-06-15 22:05:21 +05:30
items.append(streamingQualityMenu)
2021-06-17 00:42:41 +05:30
#if os(tvOS)
controller.transportBarCustomMenuItems = items
2021-06-18 04:13:29 +05:30
2021-06-18 15:47:01 +05:30
if let skip = skipSegmentAction {
if controller.contextualActions.isEmpty {
controller.contextualActions = [skip]
}
} else {
controller.contextualActions = []
2021-06-18 04:13:29 +05:30
}
2021-06-18 15:47:01 +05:30
#endif
2021-06-14 23:35:02 +05:30
}
2021-06-17 00:42:41 +05:30
fileprivate var streamingQualityMenu: UIMenu {
2021-06-15 22:05:21 +05:30
UIMenu(title: "Streaming quality", image: UIImage(systemName: "waveform"), children: streamingQualityMenuActions)
2021-06-14 23:35:02 +05:30
}
2021-06-17 00:42:41 +05:30
fileprivate var streamingQualityMenuActions: [UIAction] {
2021-06-14 23:35:02 +05:30
video.selectableStreams.map { stream in
let image = self.state.currentStream == stream ? UIImage(systemName: "checkmark") : nil
return UIAction(title: stream.description, image: image) { _ in
2021-06-16 02:51:57 +05:30
guard state.currentStream != stream else {
return
2021-06-14 23:35:02 +05:30
}
2021-06-16 02:51:57 +05:30
loadStream(stream)
2021-06-14 23:35:02 +05:30
}
}
}
2021-06-15 22:05:21 +05:30
2021-06-17 00:42:41 +05:30
fileprivate var actionsMenu: UIMenu {
2021-06-15 22:05:21 +05:30
UIMenu(title: "Actions", image: UIImage(systemName: "bolt.horizontal.fill"), children: [cancelLoadingAction])
}
2021-06-17 00:42:41 +05:30
fileprivate var cancelLoadingAction: UIAction {
2021-06-18 15:47:01 +05:30
UIAction(title: "Cancel loading \(state.nextStream.description) stream") { _ in
2021-06-15 22:05:21 +05:30
DispatchQueue.main.async {
2021-06-18 15:47:01 +05:30
state.nextStream.cancelLoadingAssets()
state.cancelLoadingStream(state.nextStream)
2021-06-15 22:05:21 +05:30
}
}
}
2021-06-18 04:13:29 +05:30
private var skipSegmentAction: UIAction? {
if state.currentSegment == nil {
return nil
}
return UIAction(title: "Skip \(state.currentSegment!.title())") { _ in
DispatchQueue.main.async {
state.player.seek(to: state.currentSegment!.skipTo)
}
}
}
2021-06-20 01:40:14 +05:30
private var playbackRateMenu: UIMenu {
UIMenu(title: "Playback rate", image: UIImage(systemName: playbackRateMenuImageSystemName), children: playbackRateMenuActions)
}
private var playbackRateMenuImageSystemName: String {
if [0.0, 1.0].contains(state.player.rate) {
return "speedometer"
}
return state.player.rate < 1.0 ? "tortoise.fill" : "hare.fill"
}
private var playbackRateMenuActions: [UIAction] {
PlayerState.availablePlaybackRates.map { rate in
let image = state.currentRate == Float(rate) ? UIImage(systemName: "checkmark") : nil
return UIAction(title: "\(rate)x", image: image) { _ in
DispatchQueue.main.async {
state.setPlayerRate(Float(rate))
}
}
}
}
2021-06-14 23:35:02 +05:30
}