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

154 lines
4.7 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-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-17 00:42:41 +05:30
loadStream(video.defaultStream, loadBest: true)
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)
}
func makeUIViewController(context _: Context) -> StreamAVPlayerViewController {
let controller = StreamAVPlayerViewController()
controller.state = state
2021-06-14 23:35:02 +05:30
2021-06-17 00:42:41 +05:30
#if os(tvOS)
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-06-17 00:42:41 +05:30
func updateUIViewController(_ controller: StreamAVPlayerViewController, 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)
}
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-14 23:35:02 +05:30
}