damus

nostr ios client
git clone git://jb55.com/damus
Log | Files | Refs | README | LICENSE

commit 2ff12cdfa687abf3de3e563fb25ccb434c742b47
parent b7b7d65612fb2f131ff70d2e1cd3b6b28dfb1868
Author: William Casarin <jb55@jb55.com>
Date:   Tue, 20 Jun 2023 15:56:55 +0200

video: switch videoplayer to use detached tasks

Diffstat:
Mdamus/Components/ImageCarousel.swift | 1+
Mdamus/Util/EventCache.swift | 2++
Mdamus/Views/Video/VideoPlayer.swift | 22+++++++++-------------
3 files changed, 12 insertions(+), 13 deletions(-)

diff --git a/damus/Components/ImageCarousel.swift b/damus/Components/ImageCarousel.swift @@ -53,6 +53,7 @@ enum ImageShape { } // MARK: - Image Carousel +@MainActor struct ImageCarousel: View { var urls: [MediaUrl] diff --git a/damus/Util/EventCache.swift b/damus/Util/EventCache.swift @@ -204,6 +204,7 @@ class EventCache { return image_metadata[url.absoluteString.lowercased()] } + @MainActor func lookup_media_size(url: URL) -> CGSize? { if let img_meta = lookup_img_metadata(url: url) { return img_meta.meta.dim?.size @@ -216,6 +217,7 @@ class EventCache { video_meta[url.absoluteString] = meta } + @MainActor func get_video_player_model(url: URL) -> VideoPlayerModel { if let model = video_meta[url.absoluteString] { return model diff --git a/damus/Views/Video/VideoPlayer.swift b/damus/Views/Video/VideoPlayer.swift @@ -39,6 +39,7 @@ enum VideoHandler { case onStateChanged((VideoState) -> Void) } +@MainActor public class VideoPlayerModel: ObservableObject { @Published var autoReplay: Bool = true @Published var muted: Bool = true @@ -164,20 +165,11 @@ public extension VideoPlayer { } } -@available(iOS 13, *) -public extension VideoPlayer { - - -} - func get_video_size(player: AVPlayer) async -> CGSize? { - let res = await withCheckedContinuation { continuation in - DispatchQueue.global().async { - let size = player.currentImage?.size - continuation.resume(returning: size) - } + let res = Task.detached(priority: .background) { + return player.currentImage?.size } - return res + return await res.value } func video_has_audio(player: AVPlayer) async -> Bool { @@ -290,13 +282,16 @@ extension VideoPlayer: UIViewRepresentable { self.videoPlayer = videoPlayer } + @MainActor func startObserver(uiView: VideoPlayerView) { guard observer == nil else { return } observer = uiView.addPeriodicTimeObserver(forInterval: .init(seconds: 0.25, preferredTimescale: 60)) { [weak self, unowned uiView] time in guard let `self` = self else { return } - self.videoPlayer.model.time = time + Task { @MainActor in + self.videoPlayer.model.time = time + } self.observerTime = time self.updateBuffer(uiView: uiView) @@ -318,6 +313,7 @@ extension VideoPlayer: UIViewRepresentable { self.observerBuffer = nil } + @MainActor func updateBuffer(uiView: VideoPlayerView) { let bufferProgress = uiView.bufferProgress guard bufferProgress != observerBuffer else { return }