Fix player instance setting (fix #260)

This commit is contained in:
Arkadiusz Fal 2022-08-16 23:16:35 +02:00
parent 16d96b8ad2
commit bca909aab6
10 changed files with 42 additions and 25 deletions

View File

@ -509,6 +509,7 @@ final class InvidiousAPI: Service, ObservableObject, VideosAPI {
} }
return SingleAssetStream( return SingleAssetStream(
instance: account.instance,
avAsset: AVURLAsset(url: streamURL), avAsset: AVURLAsset(url: streamURL),
resolution: Stream.Resolution.from(resolution: stream["resolution"].string ?? ""), resolution: Stream.Resolution.from(resolution: stream["resolution"].string ?? ""),
kind: .stream, kind: .stream,
@ -538,6 +539,7 @@ final class InvidiousAPI: Service, ObservableObject, VideosAPI {
} }
return Stream( return Stream(
instance: account.instance,
audioAsset: AVURLAsset(url: audioAssetURL), audioAsset: AVURLAsset(url: audioAssetURL),
videoAsset: AVURLAsset(url: videoAssetURL), videoAsset: AVURLAsset(url: videoAssetURL),
resolution: Stream.Resolution.from(resolution: videoStream["resolution"].stringValue), resolution: Stream.Resolution.from(resolution: videoStream["resolution"].stringValue),
@ -550,7 +552,7 @@ final class InvidiousAPI: Service, ObservableObject, VideosAPI {
private func extractHLSStreams(from content: JSON) -> [Stream] { private func extractHLSStreams(from content: JSON) -> [Stream] {
if let hlsURL = content.dictionaryValue["hlsUrl"]?.url { if let hlsURL = content.dictionaryValue["hlsUrl"]?.url {
return [Stream(hlsURL: hlsURL)] return [Stream(instance: account.instance, hlsURL: hlsURL)]
} }
return [] return []

View File

@ -517,7 +517,7 @@ final class PipedAPI: Service, ObservableObject, VideosAPI {
var streams = [Stream]() var streams = [Stream]()
if let hlsURL = content.dictionaryValue["hls"]?.url { if let hlsURL = content.dictionaryValue["hls"]?.url {
streams.append(Stream(hlsURL: hlsURL)) streams.append(Stream(instance: account.instance, hlsURL: hlsURL))
} }
let audioStreams = content let audioStreams = content
@ -560,6 +560,7 @@ final class PipedAPI: Service, ObservableObject, VideosAPI {
if videoOnly { if videoOnly {
streams.append( streams.append(
Stream( Stream(
instance: account.instance,
audioAsset: audioAsset, audioAsset: audioAsset,
videoAsset: videoAsset, videoAsset: videoAsset,
resolution: resolution, resolution: resolution,
@ -569,7 +570,12 @@ final class PipedAPI: Service, ObservableObject, VideosAPI {
) )
} else { } else {
streams.append( streams.append(
SingleAssetStream(avAsset: videoAsset, resolution: resolution, kind: .stream) SingleAssetStream(
instance: account.instance,
avAsset: videoAsset,
resolution: resolution,
kind: .stream
)
) )
} }
} }

View File

@ -34,7 +34,7 @@ final class CommentsModel: ObservableObject {
firstPage = page.isNil || page!.isEmpty firstPage = page.isNil || page!.isEmpty
player.accounts.api.comments(video.videoID, page: page)? player.playerAPI.comments(video.videoID, page: page)?
.load() .load()
.onSuccess { [weak self] response in .onSuccess { [weak self] response in
if let page: CommentsPage = response.typedContent() { if let page: CommentsPage = response.typedContent() {

View File

@ -13,7 +13,7 @@ extension PlayerModel {
return return
} }
accounts.api.video(id).load().onSuccess { [weak self] response in playerAPI.video(id).load().onSuccess { [weak self] response in
guard let video: Video = response.typedContent() else { guard let video: Video = response.typedContent() else {
return return
} }

View File

@ -651,7 +651,7 @@ final class PlayerModel: ObservableObject {
DispatchQueue.main.asyncAfter(deadline: .now() + 3) { [weak self] in DispatchQueue.main.asyncAfter(deadline: .now() + 3) { [weak self] in
guard let self = self else { return } guard let self = self else { return }
self.accounts.api.loadDetails(item, completionHandler: { newItem in self.playerAPI.loadDetails(item, completionHandler: { newItem in
guard newItem.videoID == self.autoplayItem?.videoID else { return } guard newItem.videoID == self.autoplayItem?.videoID else { return }
self.autoplayItem = newItem self.autoplayItem = newItem
self.updateRemoteCommandCenter() self.updateRemoteCommandCenter()

View File

@ -56,20 +56,32 @@ extension PlayerModel {
preservedTime = currentItem.playbackTime preservedTime = currentItem.playbackTime
DispatchQueue.main.async { [weak self] in DispatchQueue.main.async { [weak self] in
guard let video = self?.currentVideo else { guard let self = self else { return }
guard let video = self.currentVideo else {
return return
} }
self?.videoBeingOpened = nil
if video.streams.isEmpty { self.videoBeingOpened = nil
self?.loadAvailableStreams(video)
guard let playerInstance = self.playerInstance else { return }
let streamsInstance = video.streams.compactMap(\.instance).first
if video.streams.isEmpty || streamsInstance != playerInstance {
self.loadAvailableStreams(video)
} else { } else {
guard let instance = self?.accounts.current?.instance ?? InstancesModel.forPlayer ?? InstancesModel.all.first else { return } self.availableStreams = self.streamsWithInstance(instance: playerInstance, streams: video.streams)
self?.availableStreams = self?.streamsWithInstance(instance: instance, streams: video.streams) ?? video.streams
} }
} }
} }
var playerInstance: Instance? {
InstancesModel.forPlayer ?? accounts.current?.instance ?? InstancesModel.all.first
}
var playerAPI: VideosAPI {
playerInstance?.anonymous ?? accounts.api
}
var qualityProfile: QualityProfile? { var qualityProfile: QualityProfile? {
qualityProfileSelection ?? QualityProfilesModel.shared.automaticProfile qualityProfileSelection ?? QualityProfilesModel.shared.automaticProfile
} }
@ -135,7 +147,7 @@ extension PlayerModel {
currentItem.playbackTime = time currentItem.playbackTime = time
let playTime = currentItem.shouldRestartPlaying ? CMTime.zero : time let playTime = currentItem.shouldRestartPlaying ? CMTime.zero : time
accounts.api.loadDetails(currentItem, failureHandler: videoLoadFailureHandler) { newItem in playerAPI.loadDetails(currentItem, failureHandler: videoLoadFailureHandler) { newItem in
self.playItem(newItem, at: playTime) self.playItem(newItem, at: playTime)
} }
} }
@ -178,7 +190,7 @@ extension PlayerModel {
} }
if loadDetails { if loadDetails {
accounts.api.loadDetails(item, failureHandler: videoLoadFailureHandler) { [weak self] newItem in playerAPI.loadDetails(item, failureHandler: videoLoadFailureHandler) { [weak self] newItem in
guard let self = self else { return } guard let self = self else { return }
videoDetailsLoadHandler(newItem.video, newItem) videoDetailsLoadHandler(newItem.video, newItem)
@ -239,7 +251,7 @@ extension PlayerModel {
func loadQueueVideoDetails(_ item: PlayerQueueItem) { func loadQueueVideoDetails(_ item: PlayerQueueItem) {
guard !accounts.current.isNil, !item.hasDetailsLoaded else { return } guard !accounts.current.isNil, !item.hasDetailsLoaded else { return }
accounts.api.loadDetails(item, completionHandler: { newItem in playerAPI.loadDetails(item, completionHandler: { newItem in
if let index = self.queue.firstIndex(where: { $0.id == item.id }) { if let index = self.queue.firstIndex(where: { $0.id == item.id }) {
self.queue[index] = newItem self.queue[index] = newItem
} }

View File

@ -18,13 +18,10 @@ extension PlayerModel {
func loadAvailableStreams(_ video: Video) { func loadAvailableStreams(_ video: Video) {
availableStreams = [] availableStreams = []
guard let playerInstance = InstancesModel.forPlayer ?? InstancesModel.all.first else { guard let playerInstance = playerInstance else { return }
return
}
logger.info("loading streams from \(playerInstance.description)") logger.info("loading streams from \(playerInstance.description)")
fetchStreams(playerAPI.video(video.videoID), instance: playerInstance, video: video)
fetchStreams(playerInstance.anonymous.video(video.videoID), instance: playerInstance, video: video)
} }
private func fetchStreams( private func fetchStreams(

View File

@ -4,9 +4,9 @@ import Foundation
final class SingleAssetStream: Stream { final class SingleAssetStream: Stream {
var avAsset: AVURLAsset var avAsset: AVURLAsset
init(avAsset: AVURLAsset, resolution: Resolution, kind: Kind, encoding: String = "") { init(instance: Instance? = nil, avAsset: AVURLAsset, resolution: Resolution, kind: Kind, encoding: String = "") {
self.avAsset = avAsset self.avAsset = avAsset
super.init(audioAsset: avAsset, videoAsset: avAsset, resolution: resolution, kind: kind, encoding: encoding) super.init(instance: instance, audioAsset: avAsset, videoAsset: avAsset, resolution: resolution, kind: kind, encoding: encoding)
} }
} }

View File

@ -101,7 +101,7 @@ struct OpenURLHandler {
Windows.main.open() Windows.main.open()
#endif #endif
accounts.api.video(id) player.playerAPI.video(id)
.load() .load()
.onSuccess { response in .onSuccess { response in
if let video: Video = response.typedContent() { if let video: Video = response.typedContent() {

View File

@ -27,7 +27,7 @@ struct ShareButton: View {
private var instanceActions: some View { private var instanceActions: some View {
Group { Group {
if let url = accounts.api.shareURL(contentItem) { if let url = player.playerAPI.shareURL(contentItem) {
Button(labelForShareURL(accounts.app.name)) { Button(labelForShareURL(accounts.app.name)) {
shareAction(url) shareAction(url)
} }
@ -35,7 +35,7 @@ struct ShareButton: View {
if contentItemIsPlayerCurrentVideo { if contentItemIsPlayerCurrentVideo {
Button(labelForShareURL(accounts.app.name, withTime: true)) { Button(labelForShareURL(accounts.app.name, withTime: true)) {
shareAction( shareAction(
accounts.api.shareURL( player.playerAPI.shareURL(
contentItem, contentItem,
time: player.backend.currentTime time: player.backend.currentTime
)! )!