-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp_native_media.go
More file actions
291 lines (266 loc) · 8.42 KB
/
Copy pathapp_native_media.go
File metadata and controls
291 lines (266 loc) · 8.42 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package main
import (
"encoding/base64"
"errors"
"fmt"
"math"
"os"
"strconv"
"TDrive/backend/media"
"TDrive/backend/nativeplayer"
"github.com/wailsapp/wails/v2/pkg/runtime"
)
type NativeMediaResult struct {
Token string `json:"token"`
Name string `json:"name"`
ThumbnailURL string `json:"thumbnail_url"`
HTMLControls bool `json:"html_controls"`
Info media.LogicalFile `json:"info"`
}
type nativeMediaSession struct {
player *nativeplayer.Player
}
// OpenNativeMedia opens the same tokenized loopback stream as OpenMedia, then
// hands it to the native player. The returned token owns both resources.
func (a *App) OpenNativeMedia(msgID int, rect nativeplayer.Rect) (NativeMediaResult, error) {
if a.engine == nil {
return NativeMediaResult{}, fmt.Errorf("backend not ready")
}
if !rect.Valid() {
return NativeMediaResult{}, fmt.Errorf("invalid video viewport")
}
opened, err := a.engine.MediaService().Open(a.ctx, a.ActiveChannelID(), int64(msgID))
if err != nil {
return NativeMediaResult{}, err
}
if err := nativeplayer.PreflightDecode(a.ctx, opened.URL); err != nil {
_ = a.engine.MediaService().CloseSession(opened.Token)
if errors.Is(err, nativeplayer.ErrDecoderUnsafe) {
return NativeMediaResult{}, fmt.Errorf("the native decoder crashed while checking this video, so playback was blocked for safety")
}
return NativeMediaResult{}, err
}
token := opened.Token
htmlControls := nativeHTMLControlsEnabled() && nativeplayer.SupportsHTMLControls()
opts := nativeplayer.Options{
UseHTMLControls: htmlControls,
OnState: func(state nativeplayer.State) {
a.emitNativeMediaState(token, state)
},
}
player, err := nativeplayer.Start(a.ctx, opened.URL, rect, opts)
if err != nil {
_ = a.engine.MediaService().CloseSession(opened.Token)
if errors.Is(err, nativeplayer.ErrUnsupported) {
return NativeMediaResult{}, fmt.Errorf("native playback is not available on this platform yet")
}
return NativeMediaResult{}, err
}
a.nativeMediaMu.Lock()
if a.nativeMedia == nil {
a.nativeMedia = make(map[string]*nativeMediaSession)
}
a.nativeMedia[opened.Token] = &nativeMediaSession{player: player}
a.nativeMediaMu.Unlock()
return NativeMediaResult{
Token: opened.Token,
Name: opened.Name,
ThumbnailURL: opened.ThumbnailURL,
HTMLControls: htmlControls,
Info: opened.Info,
}, nil
}
func (a *App) ResizeNativeMedia(token string, rect nativeplayer.Rect) error {
session := a.nativeMediaSession(token)
if session == nil || session.player == nil {
return nil
}
return session.player.Resize(rect)
}
func (a *App) NativeMediaCommand(token string, command []string) error {
if err := validateNativeMediaCommand(command); err != nil {
return err
}
session := a.nativeMediaSession(token)
if session == nil || session.player == nil {
return nil
}
return session.player.Command(command...)
}
func (a *App) CloseNativeMedia(token string) error {
if token == "" {
return nil
}
session := a.takeNativeMediaSession(token)
if session != nil && session.player != nil {
_ = session.player.Close()
}
if a.engine != nil {
return a.engine.MediaService().CloseSession(token)
}
return nil
}
func (a *App) closeAllNativeMedia() {
a.nativeMediaMu.Lock()
sessions := a.nativeMedia
a.nativeMedia = nil
a.nativeMediaMu.Unlock()
for token, session := range sessions {
if session != nil && session.player != nil {
_ = session.player.Close()
}
if a.engine != nil {
_ = a.engine.MediaService().CloseSession(token)
}
}
}
func (a *App) emitNativeMediaState(token string, state nativeplayer.State) {
if a.ctx == nil || token == "" {
return
}
buffered := make([]map[string]float64, 0, len(state.Buffered))
for _, item := range state.Buffered {
if item.End <= item.Start {
continue
}
buffered = append(buffered, map[string]float64{
"start": item.Start,
"end": item.End,
})
}
runtime.EventsEmit(a.ctx, "native_media_state", map[string]any{
"token": token,
"paused": state.Paused,
"current_time": state.CurrentTime,
"duration": state.Duration,
"buffered": buffered,
"volume": state.Volume,
"muted": state.Muted,
"rate": state.Rate,
"loading": state.Loading,
})
}
func (a *App) nativeMediaSession(token string) *nativeMediaSession {
if token == "" {
return nil
}
a.nativeMediaMu.Lock()
defer a.nativeMediaMu.Unlock()
return a.nativeMedia[token]
}
func nativeHTMLControlsEnabled() bool {
return os.Getenv("TDRIVE_NATIVE_VIDEO_FALLBACK") != "1"
}
func (a *App) takeNativeMediaSession(token string) *nativeMediaSession {
if token == "" {
return nil
}
a.nativeMediaMu.Lock()
defer a.nativeMediaMu.Unlock()
session := a.nativeMedia[token]
delete(a.nativeMedia, token)
return session
}
func validateNativeMediaCommand(command []string) error {
if len(command) == 0 {
return fmt.Errorf("native media command is empty")
}
switch command[0] {
case "cycle":
if len(command) != 2 {
return fmt.Errorf("invalid native media cycle command")
}
switch command[1] {
case "pause", "mute":
return nil
default:
return fmt.Errorf("unsupported native media cycle target")
}
case "seek":
if len(command) != 3 {
return fmt.Errorf("invalid native media seek command")
}
value, err := strconv.ParseFloat(command[1], 64)
if err != nil || math.IsNaN(value) || math.IsInf(value, 0) {
return fmt.Errorf("invalid native media seek offset")
}
switch command[2] {
case "relative":
if math.Abs(value) > 3600 {
return fmt.Errorf("native media relative seek is too large")
}
case "absolute":
if value < 0 || value > 24*3600 {
return fmt.Errorf("native media absolute seek is out of range")
}
default:
return fmt.Errorf("unsupported native media seek mode")
}
return nil
case "set":
if len(command) != 3 {
return fmt.Errorf("invalid native media set command")
}
switch command[1] {
case "volume":
value, err := strconv.ParseFloat(command[2], 64)
if err != nil || math.IsNaN(value) || math.IsInf(value, 0) || value < 0 || value > 100 {
return fmt.Errorf("invalid native media volume")
}
return nil
case "speed":
value, err := strconv.ParseFloat(command[2], 64)
if err != nil || math.IsNaN(value) || math.IsInf(value, 0) || value < 0.25 || value > 4 {
return fmt.Errorf("invalid native media speed")
}
return nil
case "mute":
if command[2] != "yes" && command[2] != "no" {
return fmt.Errorf("invalid native media mute value")
}
return nil
default:
return fmt.Errorf("unsupported native media set target")
}
default:
return fmt.Errorf("unsupported native media command")
}
}
// ShowNativeSeekThumbnail paints a seek-preview thumbnail over the native video.
// imageBase64 is the raw base64 of a JPEG/PNG frame the frontend already holds;
// rect is the desired preview box in CSS pixels. It is only meaningful on the
// Windows/Linux fallback (where HTML can't draw over the video) and is a no-op
// on platforms whose player does not implement an overlay.
func (a *App) ShowNativeSeekThumbnail(token string, imageBase64 string, rect nativeplayer.Rect) error {
session := a.nativeMediaSession(token)
if os.Getenv("TDRIVE_MEDIA_THUMB_DEBUG") == "1" {
fmt.Fprintf(os.Stderr, "[seek-overlay] App.ShowNativeSeekThumbnail token=%q b64len=%d hasPlayer=%v\n", token, len(imageBase64), session != nil && session.player != nil)
}
if session == nil || session.player == nil {
return nil
}
data, err := base64.StdEncoding.DecodeString(imageBase64)
if err != nil {
return fmt.Errorf("native seek thumbnail: decode image: %w", err)
}
return session.player.ShowSeekThumbnail(data, rect)
}
// MoveNativeSeekThumbnail moves the already-painted seek-preview overlay without
// re-uploading or re-decoding the frame. Windows calls this on every scrub move;
// keeping it separate from ShowNativeSeekThumbnail avoids doing JPEG decode and
// GDI bitmap upload work just to follow the cursor.
func (a *App) MoveNativeSeekThumbnail(token string, rect nativeplayer.Rect) error {
session := a.nativeMediaSession(token)
if session == nil || session.player == nil {
return nil
}
return session.player.MoveSeekThumbnail(rect)
}
// HideNativeSeekThumbnail hides the seek-preview overlay for the session.
func (a *App) HideNativeSeekThumbnail(token string) error {
session := a.nativeMediaSession(token)
if session == nil || session.player == nil {
return nil
}
return session.player.HideSeekThumbnail()
}