diff --git a/Model/Applications/InvidiousAPI.swift b/Model/Applications/InvidiousAPI.swift index 1931d95d..f04e85ad 100644 --- a/Model/Applications/InvidiousAPI.swift +++ b/Model/Applications/InvidiousAPI.swift @@ -509,6 +509,7 @@ final class InvidiousAPI: Service, ObservableObject, VideosAPI { } return SingleAssetStream( + instance: account.instance, avAsset: AVURLAsset(url: streamURL), resolution: Stream.Resolution.from(resolution: stream["resolution"].string ?? ""), kind: .stream, @@ -538,6 +539,7 @@ final class InvidiousAPI: Service, ObservableObject, VideosAPI { } return Stream( + instance: account.instance, audioAsset: AVURLAsset(url: audioAssetURL), videoAsset: AVURLAsset(url: videoAssetURL), 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] { if let hlsURL = content.dictionaryValue["hlsUrl"]?.url { - return [Stream(hlsURL: hlsURL)] + return [Stream(instance: account.instance, hlsURL: hlsURL)] } return [] diff --git a/Model/Applications/PipedAPI.swift b/Model/Applications/PipedAPI.swift index 2ac47cc2..27d421d3 100644 --- a/Model/Applications/PipedAPI.swift +++ b/Model/Applications/PipedAPI.swift @@ -517,7 +517,7 @@ final class PipedAPI: Service, ObservableObject, VideosAPI { var streams = [Stream]() if let hlsURL = content.dictionaryValue["hls"]?.url { - streams.append(Stream(hlsURL: hlsURL)) + streams.append(Stream(instance: account.instance, hlsURL: hlsURL)) } let audioStreams = content @@ -560,6 +560,7 @@ final class PipedAPI: Service, ObservableObject, VideosAPI { if videoOnly { streams.append( Stream( + instance: account.instance, audioAsset: audioAsset, videoAsset: videoAsset, resolution: resolution, @@ -569,7 +570,12 @@ final class PipedAPI: Service, ObservableObject, VideosAPI { ) } else { streams.append( - SingleAssetStream(avAsset: videoAsset, resolution: resolution, kind: .stream) + SingleAssetStream( + instance: account.instance, + avAsset: videoAsset, + resolution: resolution, + kind: .stream + ) ) } } diff --git a/Model/CommentsModel.swift b/Model/CommentsModel.swift index 74045af7..8dbc3fc1 100644 --- a/Model/CommentsModel.swift +++ b/Model/CommentsModel.swift @@ -34,7 +34,7 @@ final class CommentsModel: ObservableObject { firstPage = page.isNil || page!.isEmpty - player.accounts.api.comments(video.videoID, page: page)? + player.playerAPI.comments(video.videoID, page: page)? .load() .onSuccess { [weak self] response in if let page: CommentsPage = response.typedContent() { diff --git a/Model/HistoryModel.swift b/Model/HistoryModel.swift index 0eca943b..f015d3a2 100644 --- a/Model/HistoryModel.swift +++ b/Model/HistoryModel.swift @@ -13,7 +13,7 @@ extension PlayerModel { 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 { return } diff --git a/Model/Player/PlayerModel.swift b/Model/Player/PlayerModel.swift index ceef946e..418b992d 100644 --- a/Model/Player/PlayerModel.swift +++ b/Model/Player/PlayerModel.swift @@ -651,7 +651,7 @@ final class PlayerModel: ObservableObject { DispatchQueue.main.asyncAfter(deadline: .now() + 3) { [weak self] in 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 } self.autoplayItem = newItem self.updateRemoteCommandCenter() diff --git a/Model/Player/PlayerQueue.swift b/Model/Player/PlayerQueue.swift index 3dd9b26f..1744dc80 100644 --- a/Model/Player/PlayerQueue.swift +++ b/Model/Player/PlayerQueue.swift @@ -56,20 +56,32 @@ extension PlayerModel { preservedTime = currentItem.playbackTime 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 } - self?.videoBeingOpened = nil - if video.streams.isEmpty { - self?.loadAvailableStreams(video) + self.videoBeingOpened = nil + + guard let playerInstance = self.playerInstance else { return } + let streamsInstance = video.streams.compactMap(\.instance).first + + if video.streams.isEmpty || streamsInstance != playerInstance { + self.loadAvailableStreams(video) } else { - guard let instance = self?.accounts.current?.instance ?? InstancesModel.forPlayer ?? InstancesModel.all.first else { return } - self?.availableStreams = self?.streamsWithInstance(instance: instance, streams: video.streams) ?? video.streams + self.availableStreams = self.streamsWithInstance(instance: playerInstance, streams: video.streams) } } } + var playerInstance: Instance? { + InstancesModel.forPlayer ?? accounts.current?.instance ?? InstancesModel.all.first + } + + var playerAPI: VideosAPI { + playerInstance?.anonymous ?? accounts.api + } + var qualityProfile: QualityProfile? { qualityProfileSelection ?? QualityProfilesModel.shared.automaticProfile } @@ -135,7 +147,7 @@ extension PlayerModel { currentItem.playbackTime = 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) } } @@ -178,7 +190,7 @@ extension PlayerModel { } 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 } videoDetailsLoadHandler(newItem.video, newItem) @@ -239,7 +251,7 @@ extension PlayerModel { func loadQueueVideoDetails(_ item: PlayerQueueItem) { 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 }) { self.queue[index] = newItem } diff --git a/Model/Player/PlayerStreams.swift b/Model/Player/PlayerStreams.swift index 2cd8957a..37a83f7a 100644 --- a/Model/Player/PlayerStreams.swift +++ b/Model/Player/PlayerStreams.swift @@ -18,13 +18,10 @@ extension PlayerModel { func loadAvailableStreams(_ video: Video) { availableStreams = [] - guard let playerInstance = InstancesModel.forPlayer ?? InstancesModel.all.first else { - return - } + guard let playerInstance = playerInstance else { return } logger.info("loading streams from \(playerInstance.description)") - - fetchStreams(playerInstance.anonymous.video(video.videoID), instance: playerInstance, video: video) + fetchStreams(playerAPI.video(video.videoID), instance: playerInstance, video: video) } private func fetchStreams( diff --git a/Model/SingleAssetStream.swift b/Model/SingleAssetStream.swift index 266ff7c5..8f3ba5a9 100644 --- a/Model/SingleAssetStream.swift +++ b/Model/SingleAssetStream.swift @@ -4,9 +4,9 @@ import Foundation final class SingleAssetStream: Stream { 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 - 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) } } diff --git a/Shared/OpenURLHandler.swift b/Shared/OpenURLHandler.swift index 4499c959..29a94c64 100644 --- a/Shared/OpenURLHandler.swift +++ b/Shared/OpenURLHandler.swift @@ -101,7 +101,7 @@ struct OpenURLHandler { Windows.main.open() #endif - accounts.api.video(id) + player.playerAPI.video(id) .load() .onSuccess { response in if let video: Video = response.typedContent() { diff --git a/Shared/Views/ShareButton.swift b/Shared/Views/ShareButton.swift index 36dfcb50..f935ec70 100644 --- a/Shared/Views/ShareButton.swift +++ b/Shared/Views/ShareButton.swift @@ -27,7 +27,7 @@ struct ShareButton: View { private var instanceActions: some View { Group { - if let url = accounts.api.shareURL(contentItem) { + if let url = player.playerAPI.shareURL(contentItem) { Button(labelForShareURL(accounts.app.name)) { shareAction(url) } @@ -35,7 +35,7 @@ struct ShareButton: View { if contentItemIsPlayerCurrentVideo { Button(labelForShareURL(accounts.app.name, withTime: true)) { shareAction( - accounts.api.shareURL( + player.playerAPI.shareURL( contentItem, time: player.backend.currentTime )!