yattee/Apple TV/PlayerViewController.swift

153 lines
4.8 KiB
Swift
Raw Normal View History

2021-06-14 18:05:02 +00:00
import AVKit
import Foundation
2021-06-15 16:35:21 +00:00
import Logging
2021-06-14 18:05:02 +00:00
import SwiftUI
struct PlayerViewController: UIViewControllerRepresentable {
2021-06-15 16:35:21 +00:00
let logger = Logger(label: "net.arekf.Pearvidious.pvc")
2021-06-14 18:05:02 +00:00
2021-06-15 21:21:57 +00:00
@ObservedObject private var state: PlayerState
2021-06-14 18:05:02 +00:00
2021-06-15 21:21:57 +00:00
var video: Video
2021-06-14 18:05:02 +00:00
init(video: Video) {
self.video = video
2021-06-15 21:21:57 +00:00
state = PlayerState(video)
2021-06-14 18:05:02 +00:00
2021-06-16 19:12:41 +00:00
loadStream(video.defaultStream, loadBest: true)
2021-06-14 18:05:02 +00:00
}
2021-06-16 19:12:41 +00:00
fileprivate func loadStream(_ stream: Stream?, loadBest: Bool = false) {
2021-06-15 16:35:21 +00:00
if stream != state.streamToLoad {
state.loadStream(stream)
2021-06-15 21:21:57 +00:00
addTracksAndLoadAssets(stream!, loadBest: loadBest)
2021-06-15 16:35:21 +00:00
}
}
2021-06-14 18:05:02 +00:00
2021-06-16 19:12:41 +00:00
fileprivate func addTracksAndLoadAssets(_ stream: Stream, loadBest: Bool = false) {
2021-06-15 16:35:21 +00:00
logger.info("adding tracks and loading assets for: \(stream.type), \(stream.description)")
2021-06-14 18:05:02 +00:00
2021-06-15 16:35:21 +00:00
stream.assets.forEach { asset in
asset.loadValuesAsynchronously(forKeys: ["playable"]) {
handleAssetLoad(stream, type: asset == stream.videoAsset ? .video : .audio, loadBest: loadBest)
2021-06-14 18:05:02 +00:00
}
}
2021-06-15 16:35:21 +00:00
}
2021-06-14 18:05:02 +00:00
2021-06-16 19:12:41 +00:00
fileprivate func addTrack(_ asset: AVURLAsset, stream: Stream, type: AVMediaType? = nil) {
2021-06-15 21:21:57 +00:00
let types: [AVMediaType] = stream.type == .adaptive ? [type!] : [.video, .audio]
2021-06-14 18:05:02 +00:00
2021-06-15 21:21:57 +00:00
types.forEach { type in
guard let assetTrack = asset.tracks(withMediaType: type).first else {
return
}
if let track = state.composition.tracks(withMediaType: type).first {
logger.info("removing \(type) track")
state.composition.removeTrack(track)
}
2021-06-15 16:35:21 +00:00
2021-06-15 21:21:57 +00:00
let track = state.composition.addMutableTrack(withMediaType: type, preferredTrackID: kCMPersistentTrackID_Invalid)!
2021-06-15 16:35:21 +00:00
2021-06-15 21:21:57 +00:00
try! track.insertTimeRange(
CMTimeRange(start: .zero, duration: CMTime(seconds: video.length, preferredTimescale: 1)),
of: assetTrack,
at: .zero
)
2021-06-15 16:35:21 +00:00
2021-06-15 21:21:57 +00:00
logger.info("inserted \(type) track")
}
2021-06-14 18:05:02 +00:00
}
2021-06-16 19:12:41 +00:00
fileprivate func handleAssetLoad(_ stream: Stream, type: AVMediaType, loadBest: Bool = false) {
2021-06-15 16:35:21 +00:00
logger.info("handling asset load: \(stream.type), \(stream.description)")
2021-06-15 21:21:57 +00:00
2021-06-15 16:35:21 +00:00
guard stream != state.currentStream else {
logger.warning("IGNORING assets loaded: \(stream.type), \(stream.description)")
return
}
2021-06-15 21:21:57 +00:00
stream.loadedAssets.forEach { asset in
addTrack(asset, stream: stream, type: type)
2021-06-14 18:05:02 +00:00
2021-06-15 16:35:21 +00:00
if stream.assetsLoaded {
2021-06-15 21:21:57 +00:00
DispatchQueue.main.async {
logger.info("ALL assets loaded: \(stream.type), \(stream.description)")
2021-06-14 18:05:02 +00:00
2021-06-16 19:12:41 +00:00
state.playStream(stream)
2021-06-15 16:35:21 +00:00
}
2021-06-14 18:05:02 +00:00
2021-06-15 16:35:21 +00:00
if loadBest {
loadBestStream()
}
2021-06-14 18:05:02 +00:00
}
}
}
2021-06-16 19:12:41 +00:00
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 18:05:02 +00:00
2021-06-16 19:12:41 +00:00
#if os(tvOS)
controller.transportBarCustomMenuItems = [streamingQualityMenu]
#endif
2021-06-14 18:05:02 +00:00
controller.modalPresentationStyle = .fullScreen
2021-06-15 21:21:57 +00:00
controller.player = state.player
2021-06-14 18:05:02 +00:00
return controller
}
2021-06-16 19:12:41 +00:00
func updateUIViewController(_ controller: StreamAVPlayerViewController, context _: Context) {
2021-06-15 16:35:21 +00:00
var items: [UIMenuElement] = []
if state.streamToLoad != nil {
items.append(actionsMenu)
}
items.append(streamingQualityMenu)
2021-06-16 19:12:41 +00:00
#if os(tvOS)
controller.transportBarCustomMenuItems = items
#endif
2021-06-14 18:05:02 +00:00
}
2021-06-16 19:12:41 +00:00
fileprivate var streamingQualityMenu: UIMenu {
2021-06-15 16:35:21 +00:00
UIMenu(title: "Streaming quality", image: UIImage(systemName: "waveform"), children: streamingQualityMenuActions)
2021-06-14 18:05:02 +00:00
}
2021-06-16 19:12:41 +00:00
fileprivate var streamingQualityMenuActions: [UIAction] {
2021-06-14 18:05:02 +00:00
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-15 21:21:57 +00:00
guard state.currentStream != stream else {
return
2021-06-14 18:05:02 +00:00
}
2021-06-15 21:21:57 +00:00
loadStream(stream)
2021-06-14 18:05:02 +00:00
}
}
}
2021-06-15 16:35:21 +00:00
2021-06-16 19:12:41 +00:00
fileprivate var actionsMenu: UIMenu {
2021-06-15 16:35:21 +00:00
UIMenu(title: "Actions", image: UIImage(systemName: "bolt.horizontal.fill"), children: [cancelLoadingAction])
}
2021-06-16 19:12:41 +00:00
fileprivate var cancelLoadingAction: UIAction {
2021-06-15 16:35:21 +00:00
UIAction(title: "Cancel loading \(state.streamToLoad.description) stream") { _ in
DispatchQueue.main.async {
state.streamToLoad.cancelLoadingAssets()
state.cancelLoadingStream(state.streamToLoad)
}
}
}
2021-06-14 18:05:02 +00:00
}