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-26 11:37:24 +00:00
|
|
|
loadStream(video.defaultStreamForProfile(state.profile), loadBest: state.profile.defaultStreamResolution == .hd720pFirstThenBest)
|
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-18 10:17:01 +00:00
|
|
|
if stream != state.nextStream {
|
2021-06-15 16:35:21 +00:00
|
|
|
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 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-18 10:17:01 +00:00
|
|
|
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-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] = []
|
|
|
|
|
2021-06-18 10:17:01 +00:00
|
|
|
if state.nextStream != nil {
|
2021-06-15 16:35:21 +00:00
|
|
|
items.append(actionsMenu)
|
|
|
|
}
|
|
|
|
|
2021-06-19 20:10:14 +00:00
|
|
|
items.append(playbackRateMenu)
|
2021-06-15 16:35:21 +00:00
|
|
|
items.append(streamingQualityMenu)
|
|
|
|
|
2021-06-16 19:12:41 +00:00
|
|
|
#if os(tvOS)
|
|
|
|
controller.transportBarCustomMenuItems = items
|
2021-06-17 22:43:29 +00:00
|
|
|
|
2021-06-18 10:17:01 +00:00
|
|
|
if let skip = skipSegmentAction {
|
|
|
|
if controller.contextualActions.isEmpty {
|
|
|
|
controller.contextualActions = [skip]
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
controller.contextualActions = []
|
2021-06-17 22:43:29 +00:00
|
|
|
}
|
2021-06-18 10:17:01 +00:00
|
|
|
#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-18 10:17:01 +00:00
|
|
|
UIAction(title: "Cancel loading \(state.nextStream.description) stream") { _ in
|
2021-06-15 16:35:21 +00:00
|
|
|
DispatchQueue.main.async {
|
2021-06-18 10:17:01 +00:00
|
|
|
state.nextStream.cancelLoadingAssets()
|
|
|
|
state.cancelLoadingStream(state.nextStream)
|
2021-06-15 16:35:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-06-17 22:43:29 +00:00
|
|
|
|
|
|
|
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-19 20:10:14 +00:00
|
|
|
|
|
|
|
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 18:05:02 +00:00
|
|
|
}
|