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

149 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-16 02:51:57 +05:30
loadStream(video.defaultStream, loadBest: false)
2021-06-14 23:35:02 +05:30
}
2021-06-16 02:51:57 +05:30
func loadStream(_ stream: Stream?, loadBest: Bool = false) {
2021-06-15 22:05:21 +05:30
if stream != state.streamToLoad {
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-15 22:05:21 +05:30
func loadBestStream() {
guard state.currentStream != video.bestStream else {
return
}
2021-06-14 23:35:02 +05:30
2021-06-15 22:05:21 +05:30
loadStream(video.bestStream)
}
2021-06-14 23:35:02 +05:30
2021-06-15 22:05:21 +05:30
func addTracksAndLoadAssets(_ stream: Stream, loadBest: Bool = false) {
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-16 02:51:57 +05:30
func addTrack(_ asset: AVURLAsset, stream: Stream, type: AVMediaType? = nil) {
let types: [AVMediaType] = stream.type == .adaptive ? [type!] : [.video, .audio]
2021-06-14 23:35:02 +05:30
2021-06-16 02:51:57 +05:30
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 22:05:21 +05:30
2021-06-16 02:51:57 +05:30
let track = state.composition.addMutableTrack(withMediaType: type, preferredTrackID: kCMPersistentTrackID_Invalid)!
2021-06-15 22:05:21 +05:30
2021-06-16 02:51:57 +05:30
try! track.insertTimeRange(
CMTimeRange(start: .zero, duration: CMTime(seconds: video.length, preferredTimescale: 1)),
of: assetTrack,
at: .zero
)
2021-06-15 22:05:21 +05:30
2021-06-16 02:51:57 +05:30
logger.info("inserted \(type) track")
}
2021-06-14 23:35:02 +05:30
}
2021-06-15 22:05:21 +05:30
func handleAssetLoad(_ stream: Stream, type: AVMediaType, loadBest: Bool = false) {
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-16 02:51:57 +05:30
state.loadStreamIntoPlayer(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
}
}
}
func makeUIViewController(context _: Context) -> AVPlayerViewController {
let controller = AVPlayerViewController()
controller.transportBarCustomMenuItems = [streamingQualityMenu]
controller.modalPresentationStyle = .fullScreen
2021-06-16 02:51:57 +05:30
controller.player = state.player
2021-06-15 22:05:21 +05:30
controller.player?.automaticallyWaitsToMinimizeStalling = true
2021-06-14 23:35:02 +05:30
return controller
}
func updateUIViewController(_ controller: AVPlayerViewController, context _: Context) {
2021-06-15 22:05:21 +05:30
var items: [UIMenuElement] = []
if state.streamToLoad != nil {
items.append(actionsMenu)
}
items.append(streamingQualityMenu)
controller.transportBarCustomMenuItems = items
2021-06-14 23:35:02 +05:30
}
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
}
var streamingQualityMenuActions: [UIAction] {
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
var actionsMenu: UIMenu {
UIMenu(title: "Actions", image: UIImage(systemName: "bolt.horizontal.fill"), children: [cancelLoadingAction])
}
var cancelLoadingAction: UIAction {
UIAction(title: "Cancel loading \(state.streamToLoad.description) stream") { _ in
DispatchQueue.main.async {
state.streamToLoad.cancelLoadingAssets()
state.cancelLoadingStream(state.streamToLoad)
}
}
}
2021-06-14 23:35:02 +05:30
}