forked from viktorstrate/mactrix
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessageVideoView.swift
More file actions
129 lines (113 loc) · 4.41 KB
/
MessageVideoView.swift
File metadata and controls
129 lines (113 loc) · 4.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import AVKit
import MatrixRustSDK
import Models
import OSLog
import SwiftUI
struct MessageVideoView: View {
@Environment(AppState.self) private var appState
let content: VideoMessageContent
@State private var fileHandle: MediaFileHandle?
@State private var video: AVPlayer?
@State private var generatedThumbnail: Image?
var aspectRatio: CGFloat? {
guard let info = content.info,
let height = info.height,
let width = info.width else { return nil }
return CGFloat(width) / CGFloat(height)
}
var maxHeight: CGFloat {
guard let height = content.info?.height else { return 300 }
return min(CGFloat(height), 300)
}
func loadVideo() async {
guard let client = appState.matrixClient?.client else { return }
do {
let handle = try await client.getMediaFile(
mediaSource: content.source,
filename: content.filename,
mimeType: content.info?.mimetype ?? "",
useCache: true,
tempDir: NSTemporaryDirectory()
)
fileHandle = handle
let path = try handle.path()
let url = URL(filePath: path, directoryHint: .notDirectory)
video = AVPlayer(url: url)
video?.play()
} catch {
Logger.viewCycle.error("Failed to load video: \(error)")
}
}
private func generateThumbnail() async {
guard let client = appState.matrixClient?.client else { return }
let cacheKey = NSString(string: "thumb:" + content.source.url())
if let cached = MatrixClient.imageCache.object(forKey: cacheKey) {
generatedThumbnail = Image(nsImage: cached)
return
}
do {
let handle = try await client.getMediaFile(
mediaSource: content.source,
filename: content.filename,
mimeType: content.info?.mimetype ?? "",
useCache: true,
tempDir: NSTemporaryDirectory()
)
fileHandle = handle
let path = try handle.path()
let url = URL(filePath: path, directoryHint: .notDirectory)
let asset = AVURLAsset(url: url)
let generator = AVAssetImageGenerator(asset: asset)
generator.appliesPreferredTrackTransform = true
generator.maximumSize = CGSize(width: 600, height: 600)
let cgImage = try await generator.image(at: .zero).image
let nsImage = NSImage(cgImage: cgImage, size: NSSize(width: cgImage.width, height: cgImage.height))
MatrixClient.imageCache.setObject(nsImage, forKey: cacheKey)
generatedThumbnail = Image(nsImage: nsImage)
} catch {
Logger.viewCycle.error("Failed to generate video thumbnail: \(error)")
}
}
@ViewBuilder
var thumbnailView: some View {
if let thumbnailSource = content.info?.thumbnailSource {
MatrixImageView(mediaSource: thumbnailSource, mimeType: content.info?.thumbnailInfo?.mimetype)
} else if let generatedThumbnail {
generatedThumbnail.resizable().scaledToFit()
} else {
Rectangle().fill(Color.gray.opacity(0.3))
}
}
var body: some View {
VStack {
if let video {
TimelineVideoPlayer(videoPlayer: video)
.cornerRadius(6)
} else {
Button(action: { Task { await loadVideo() } }) {
thumbnailView
.overlay {
Image(systemName: "play.fill")
.resizable()
.foregroundStyle(.white)
.shadow(radius: 4)
.frame(width: 48, height: 48)
.opacity(0.9)
}
}
.buttonStyle(.plain)
}
if let caption = content.caption, !caption.isEmpty {
Text(caption.formatAsMarkdown)
.textSelection(.enabled)
}
}
.frame(maxHeight: maxHeight)
.aspectRatio(aspectRatio, contentMode: .fit)
.task(id: content.source.url(), priority: .utility) {
if content.info?.thumbnailSource == nil {
await generateThumbnail()
}
}
}
}