Skip to content

Commit

Permalink
fix video size detection, and audio track detection to work with HLS
Browse files Browse the repository at this point in the history
  • Loading branch information
fishcakeday committed Sep 26, 2023
1 parent bc89721 commit dc863f8
Showing 1 changed file with 27 additions and 15 deletions.
42 changes: 27 additions & 15 deletions damus/Views/Video/DamusVideoPlayerViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,15 @@ import Combine
import Foundation
import SwiftUI

func get_video_size(player: AVPlayer) async -> CGSize? {
let res = Task.detached(priority: .background) {
return player.currentImage?.size
}
return await res.value
}

func video_has_audio(player: AVPlayer) async -> Bool {
let tracks = try? await player.currentItem?.asset.load(.tracks)
return tracks?.filter({ t in t.mediaType == .audio }).first != nil
do {
let hasAudibleTracks = ((try await player.currentItem?.asset.loadMediaSelectionGroup(for: .audible)) != nil)
let tracks = try? await player.currentItem?.asset.load(.tracks)
let hasAudioTrack = tracks?.filter({ t in t.mediaType == .audio }).first != nil // Deal with odd cases of audio only MOV
return hasAudibleTracks || hasAudioTrack
} catch {
return false
}
}

@MainActor
Expand All @@ -38,6 +37,8 @@ final class DamusVideoPlayerViewModel: ObservableObject {

private var cancellables = Set<AnyCancellable>()

private var videoSizeObserver: NSKeyValueObservation?

private var is_scrolled_into_view = false {
didSet {
if is_scrolled_into_view && !oldValue {
Expand Down Expand Up @@ -78,19 +79,26 @@ final class DamusVideoPlayerViewModel: ObservableObject {
model_id == self?.id ? self?.player.play() : self?.player.pause()
}
.store(in: &cancellables)

setupVideoSizeObserver()
}

private func setupVideoSizeObserver() {
videoSizeObserver = player.currentItem?.observe(\.presentationSize, options: [.new], changeHandler: { [weak self] (playerItem, change) in
guard let self = self else { return }
if let newSize = change.newValue, newSize != .zero {
DispatchQueue.main.async {
self.video_size = newSize // Update the bound value
}
}
})
}

private func load() async {
if let meta = controller.metadata(for: url) {
has_audio = meta.has_audio
video_size = meta.size
} else {
has_audio = await video_has_audio(player: player)
if let video_size = await get_video_size(player: player) {
self.video_size = video_size
let meta = VideoMetadata(has_audio: has_audio, size: video_size)
controller.set_metadata(meta, url: url)
}
}

is_loading = false
Expand All @@ -114,4 +122,8 @@ final class DamusVideoPlayerViewModel: ObservableObject {
player.seek(to: CMTime.zero)
player.play()
}

deinit {
videoSizeObserver?.invalidate()
}
}

0 comments on commit dc863f8

Please sign in to comment.