-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_graphics.go
More file actions
938 lines (876 loc) · 30.8 KB
/
api_graphics.go
File metadata and controls
938 lines (876 loc) · 30.8 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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
package control
import (
"encoding/json"
"image"
"io"
"mime"
"net/http"
"strconv"
"time"
"github.com/zsiec/switchframe/server/control/httperr"
"github.com/zsiec/switchframe/server/graphics"
"github.com/zsiec/switchframe/server/stinger"
"github.com/zsiec/switchframe/server/transition"
)
// graphicsFrameRequest is the JSON body for the graphics frame upload endpoint.
type graphicsFrameRequest struct {
Width int `json:"width"`
Height int `json:"height"`
Template string `json:"template"`
RGBA []byte `json:"rgba"`
}
// registerGraphicsRoutes registers graphics and stinger API routes on the given mux.
func (a *API) registerGraphicsRoutes(mux *http.ServeMux) {
if a.stingerStore != nil {
mux.HandleFunc("GET /api/stinger/list", a.handleStingerList)
mux.HandleFunc("DELETE /api/stinger/{name}", a.handleStingerDelete)
mux.HandleFunc("POST /api/stinger/{name}/cut-point", a.handleStingerCutPoint)
mux.HandleFunc("POST /api/stinger/{name}/upload", a.handleStingerUpload)
}
if a.compositor == nil {
return
}
mux.HandleFunc("POST /api/graphics", a.handleGraphicsAddLayer)
mux.HandleFunc("GET /api/graphics", a.handleGraphicsStatus)
mux.HandleFunc("DELETE /api/graphics/{id}", a.handleGraphicsRemoveLayer)
mux.HandleFunc("POST /api/graphics/{id}/frame", a.handleGraphicsFrame)
mux.HandleFunc("POST /api/graphics/{id}/on", timedHandlerWithPath(a.cmdQueue, "graphics_on", a.seqTracker, []string{"id"}, a.handleGraphicsOnInner))
mux.HandleFunc("POST /api/graphics/{id}/off", timedHandlerWithPath(a.cmdQueue, "graphics_off", a.seqTracker, []string{"id"}, a.handleGraphicsOffInner))
mux.HandleFunc("POST /api/graphics/{id}/auto-on", a.handleGraphicsAutoOn)
mux.HandleFunc("POST /api/graphics/{id}/auto-off", a.handleGraphicsAutoOff)
mux.HandleFunc("PUT /api/graphics/dsk-fade-duration", timedHandler(a.cmdQueue, "dsk_fade_duration_set", a.seqTracker, a.handleSetDSKFadeDurationInner))
mux.HandleFunc("POST /api/graphics/{id}/animate", a.handleGraphicsAnimate)
mux.HandleFunc("POST /api/graphics/{id}/animate/stop", a.handleGraphicsAnimateStop)
mux.HandleFunc("PUT /api/graphics/{id}/rect", a.handleGraphicsLayerRect)
mux.HandleFunc("PUT /api/graphics/{id}/zorder", a.handleGraphicsLayerZOrder)
mux.HandleFunc("POST /api/graphics/{id}/fly-in", a.handleGraphicsFlyIn)
mux.HandleFunc("POST /api/graphics/{id}/fly-out", a.handleGraphicsFlyOut)
mux.HandleFunc("POST /api/graphics/{id}/fly-on", a.handleGraphicsFlyOn)
mux.HandleFunc("POST /api/graphics/{id}/slide", a.handleGraphicsSlide)
mux.HandleFunc("POST /api/graphics/{id}/image", a.handleGraphicsImageUpload)
mux.HandleFunc("GET /api/graphics/{id}/image", a.handleGraphicsImageGet)
mux.HandleFunc("DELETE /api/graphics/{id}/image", a.handleGraphicsImageDelete)
mux.HandleFunc("POST /api/graphics/{id}/text-animate", a.handleGraphicsTextAnimStart)
mux.HandleFunc("POST /api/graphics/{id}/text-animate/stop", a.handleGraphicsTextAnimStop)
mux.HandleFunc("POST /api/graphics/{id}/ticker", a.handleGraphicsTickerStart)
mux.HandleFunc("POST /api/graphics/{id}/ticker/stop", a.handleGraphicsTickerStop)
mux.HandleFunc("PUT /api/graphics/{id}/ticker/text", a.handleGraphicsTickerText)
if a.sequenceEngine != nil {
mux.HandleFunc("POST /api/graphics/{id}/sequence", a.handleGraphicsSequenceUpload)
mux.HandleFunc("DELETE /api/graphics/{id}/sequence", a.handleGraphicsSequenceDelete)
mux.HandleFunc("PUT /api/graphics/{id}/sequence/speed", a.handleGraphicsSequenceSpeed)
}
}
// parseLayerID extracts the layer ID from the URL path parameter.
func parseLayerID(r *http.Request) (int, error) {
s := r.PathValue("id")
id, err := strconv.Atoi(s)
if err != nil {
return 0, err
}
return id, nil
}
// handleGraphicsAddLayer creates a new graphics layer.
func (a *API) handleGraphicsAddLayer(w http.ResponseWriter, r *http.Request) {
a.setLastOperator(r)
id, err := a.compositor.AddLayer()
if err != nil {
httperr.WriteErr(w, errorStatus(err), err)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
_ = json.NewEncoder(w).Encode(map[string]int{"id": id})
}
// handleGraphicsRemoveLayer removes a graphics layer by ID.
func (a *API) handleGraphicsRemoveLayer(w http.ResponseWriter, r *http.Request) {
a.setLastOperator(r)
id, err := parseLayerID(r)
if err != nil {
httperr.Write(w, http.StatusBadRequest, "invalid layer id")
return
}
if err := a.compositor.RemoveLayer(id); err != nil {
httperr.WriteErr(w, errorStatus(err), err)
return
}
w.WriteHeader(http.StatusNoContent)
}
// handleGraphicsOnInner activates a layer immediately (CUT ON).
// body is the pre-read request body provided by timedHandlerWithPath.
func (a *API) handleGraphicsOnInner(w http.ResponseWriter, r *http.Request, _ []byte) {
a.setLastOperator(r)
id, err := parseLayerID(r)
if err != nil {
httperr.Write(w, http.StatusBadRequest, "invalid layer id")
return
}
if err := a.compositor.On(id); err != nil {
httperr.WriteErr(w, errorStatus(err), err)
return
}
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(a.compositor.Status())
}
// handleGraphicsOffInner deactivates a layer immediately (CUT OFF).
// body is the pre-read request body provided by timedHandlerWithPath.
func (a *API) handleGraphicsOffInner(w http.ResponseWriter, r *http.Request, _ []byte) {
a.setLastOperator(r)
id, err := parseLayerID(r)
if err != nil {
httperr.Write(w, http.StatusBadRequest, "invalid layer id")
return
}
if err := a.compositor.Off(id); err != nil {
httperr.WriteErr(w, errorStatus(err), err)
return
}
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(a.compositor.Status())
}
// handleGraphicsAutoOn starts a fade-in transition (AUTO ON). The fade rate
// is read from Compositor.DSKFadeDurationMs at trigger time so mid-fade
// rate changes do not retime an in-flight ramp.
func (a *API) handleGraphicsAutoOn(w http.ResponseWriter, r *http.Request) {
a.setLastOperator(r)
id, err := parseLayerID(r)
if err != nil {
httperr.Write(w, http.StatusBadRequest, "invalid layer id")
return
}
dur := time.Duration(a.compositor.DSKFadeDurationMs()) * time.Millisecond
if err := a.compositor.AutoOn(id, dur); err != nil {
httperr.WriteErr(w, errorStatus(err), err)
return
}
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(a.compositor.Status())
}
// handleGraphicsAutoOff starts a fade-out transition (AUTO OFF) at the
// configured DSK fade rate.
func (a *API) handleGraphicsAutoOff(w http.ResponseWriter, r *http.Request) {
a.setLastOperator(r)
id, err := parseLayerID(r)
if err != nil {
httperr.Write(w, http.StatusBadRequest, "invalid layer id")
return
}
dur := time.Duration(a.compositor.DSKFadeDurationMs()) * time.Millisecond
if err := a.compositor.AutoOff(id, dur); err != nil {
httperr.WriteErr(w, errorStatus(err), err)
return
}
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(a.compositor.Status())
}
// dskFadeDurationRequest is the JSON body for PUT /api/graphics/dsk-fade-duration.
type dskFadeDurationRequest struct {
DurationMs int `json:"durationMs"`
}
// handleSetDSKFadeDurationInner sets the DSK on/off fade duration.
// body is the pre-read request body provided by timedHandler.
func (a *API) handleSetDSKFadeDurationInner(w http.ResponseWriter, r *http.Request, body []byte) {
a.setLastOperator(r)
var req dskFadeDurationRequest
if err := json.Unmarshal(body, &req); err != nil {
httperr.Write(w, http.StatusBadRequest, "invalid json")
return
}
if err := a.compositor.SetDSKFadeDuration(req.DurationMs); err != nil {
httperr.WriteErr(w, errorStatus(err), err)
return
}
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(a.enrichedState())
}
// handleGraphicsStatus returns the current graphics compositor state.
func (a *API) handleGraphicsStatus(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(a.compositor.Status())
}
// handleGraphicsFrame receives an RGBA overlay frame for a specific layer.
func (a *API) handleGraphicsFrame(w http.ResponseWriter, r *http.Request) {
id, err := parseLayerID(r)
if err != nil {
httperr.Write(w, http.StatusBadRequest, "invalid layer id")
return
}
body := io.LimitReader(r.Body, 16*1024*1024)
var req graphicsFrameRequest
if err := json.NewDecoder(body).Decode(&req); err != nil {
httperr.Write(w, http.StatusBadRequest, "invalid json")
return
}
if req.Width <= 0 || req.Height <= 0 {
httperr.Write(w, http.StatusBadRequest, "width and height must be positive")
return
}
if req.Width > 3840 || req.Height > 2160 {
httperr.Write(w, http.StatusBadRequest, "resolution exceeds 4K limit")
return
}
expected := req.Width * req.Height * 4
if len(req.RGBA) != expected {
httperr.Write(w, http.StatusBadRequest, "rgba data size mismatch")
return
}
if err := a.compositor.SetOverlay(id, req.RGBA, req.Width, req.Height, req.Template); err != nil {
httperr.WriteErr(w, errorStatus(err), err)
return
}
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(a.compositor.Status())
}
// animateRequest is the JSON body for the graphics animation endpoint.
type animateRequest struct {
Mode string `json:"mode"`
MinAlpha float64 `json:"minAlpha"`
MaxAlpha float64 `json:"maxAlpha"`
SpeedHz float64 `json:"speedHz"`
ToRect *graphics.RectState `json:"toRect,omitempty"`
ToAlpha *float64 `json:"toAlpha,omitempty"`
DurationMs int `json:"durationMs,omitempty"`
Easing string `json:"easing,omitempty"`
}
// handleGraphicsAnimate starts an animation on a specific layer.
func (a *API) handleGraphicsAnimate(w http.ResponseWriter, r *http.Request) {
a.setLastOperator(r)
id, err := parseLayerID(r)
if err != nil {
httperr.Write(w, http.StatusBadRequest, "invalid layer id")
return
}
var req animateRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
httperr.Write(w, http.StatusBadRequest, "invalid json")
return
}
if req.Mode != "pulse" && req.Mode != "transition" {
httperr.Write(w, http.StatusBadRequest, "mode must be \"pulse\" or \"transition\"")
return
}
if req.Mode == "pulse" {
if req.MinAlpha < 0 || req.MinAlpha > 1 || req.MaxAlpha < 0 || req.MaxAlpha > 1 {
httperr.Write(w, http.StatusBadRequest, "alpha values must be in [0,1]")
return
}
if req.MinAlpha >= req.MaxAlpha {
httperr.Write(w, http.StatusBadRequest, "minAlpha must be less than maxAlpha")
return
}
if req.SpeedHz <= 0 || req.SpeedHz > 10 {
httperr.Write(w, http.StatusBadRequest, "speedHz must be in (0,10]")
return
}
}
if req.Mode == "transition" {
if req.ToRect == nil && req.ToAlpha == nil {
httperr.Write(w, http.StatusBadRequest, "transition mode requires at least one of toRect or toAlpha")
return
}
if req.DurationMs <= 0 {
httperr.Write(w, http.StatusBadRequest, "durationMs must be positive for transition mode")
return
}
}
cfg := graphics.AnimationConfig{
Mode: req.Mode,
MinAlpha: req.MinAlpha,
MaxAlpha: req.MaxAlpha,
SpeedHz: req.SpeedHz,
ToRect: req.ToRect,
ToAlpha: req.ToAlpha,
DurationMs: req.DurationMs,
Easing: req.Easing,
}
if err := a.compositor.Animate(id, cfg); err != nil {
httperr.WriteErr(w, errorStatus(err), err)
return
}
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(a.compositor.Status())
}
// handleGraphicsAnimateStop stops any running animation on a layer.
func (a *API) handleGraphicsAnimateStop(w http.ResponseWriter, r *http.Request) {
a.setLastOperator(r)
id, err := parseLayerID(r)
if err != nil {
httperr.Write(w, http.StatusBadRequest, "invalid layer id")
return
}
if err := a.compositor.StopAnimation(id); err != nil {
httperr.WriteErr(w, errorStatus(err), err)
return
}
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(a.compositor.Status())
}
// rectUpdateRequest is the JSON body for updating a layer's rect.
type rectUpdateRequest struct {
X int `json:"x"`
Y int `json:"y"`
Width int `json:"width"`
Height int `json:"height"`
}
// handleGraphicsLayerRect updates a layer's position rectangle.
func (a *API) handleGraphicsLayerRect(w http.ResponseWriter, r *http.Request) {
a.setLastOperator(r)
id, err := parseLayerID(r)
if err != nil {
httperr.Write(w, http.StatusBadRequest, "invalid layer id")
return
}
var req rectUpdateRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
httperr.Write(w, http.StatusBadRequest, "invalid json")
return
}
if req.Width <= 0 || req.Height <= 0 {
httperr.Write(w, http.StatusBadRequest, "width and height must be positive")
return
}
// Even-align for YUV420 chroma compatibility (matches fast-control validation).
rect := image.Rect(req.X&^1, req.Y&^1, (req.X&^1)+(req.Width&^1), (req.Y&^1)+(req.Height&^1))
if err := a.compositor.SetLayerRect(id, rect); err != nil {
httperr.WriteErr(w, errorStatus(err), err)
return
}
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(a.compositor.Status())
}
// zorderUpdateRequest is the JSON body for updating a layer's z-order.
type zorderUpdateRequest struct {
ZOrder int `json:"zOrder"`
}
// handleGraphicsLayerZOrder updates a layer's z-order.
func (a *API) handleGraphicsLayerZOrder(w http.ResponseWriter, r *http.Request) {
a.setLastOperator(r)
id, err := parseLayerID(r)
if err != nil {
httperr.Write(w, http.StatusBadRequest, "invalid layer id")
return
}
var req zorderUpdateRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
httperr.Write(w, http.StatusBadRequest, "invalid json")
return
}
if err := a.compositor.SetLayerZOrder(id, req.ZOrder); err != nil {
httperr.WriteErr(w, errorStatus(err), err)
return
}
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(a.compositor.Status())
}
// flyRequest is the JSON body for fly-in/fly-out endpoints.
type flyRequest struct {
Direction string `json:"direction"`
DurationMs int `json:"durationMs"`
}
// slideRequest is the JSON body for the slide endpoint.
type slideRequest struct {
X int `json:"x"`
Y int `json:"y"`
Width int `json:"width"`
Height int `json:"height"`
DurationMs int `json:"durationMs"`
Easing string `json:"easing,omitempty"`
}
// handleGraphicsFlyIn animates a layer from off-screen to its current position.
func (a *API) handleGraphicsFlyIn(w http.ResponseWriter, r *http.Request) {
a.setLastOperator(r)
id, err := parseLayerID(r)
if err != nil {
httperr.Write(w, http.StatusBadRequest, "invalid layer id")
return
}
var req flyRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
httperr.Write(w, http.StatusBadRequest, "invalid json")
return
}
if req.Direction != "left" && req.Direction != "right" && req.Direction != "top" && req.Direction != "bottom" {
httperr.Write(w, http.StatusBadRequest, "direction must be \"left\", \"right\", \"top\", or \"bottom\"")
return
}
if req.DurationMs <= 0 {
req.DurationMs = 500
}
if err := a.compositor.FlyIn(id, req.Direction, req.DurationMs); err != nil {
httperr.WriteErr(w, errorStatus(err), err)
return
}
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(a.compositor.Status())
}
// handleGraphicsFlyOut animates a layer from its current position to off-screen.
func (a *API) handleGraphicsFlyOut(w http.ResponseWriter, r *http.Request) {
a.setLastOperator(r)
id, err := parseLayerID(r)
if err != nil {
httperr.Write(w, http.StatusBadRequest, "invalid layer id")
return
}
var req flyRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
httperr.Write(w, http.StatusBadRequest, "invalid json")
return
}
if req.Direction != "left" && req.Direction != "right" && req.Direction != "top" && req.Direction != "bottom" {
httperr.Write(w, http.StatusBadRequest, "direction must be \"left\", \"right\", \"top\", or \"bottom\"")
return
}
if req.DurationMs <= 0 {
req.DurationMs = 500
}
if err := a.compositor.FlyOut(id, req.Direction, req.DurationMs); err != nil {
httperr.WriteErr(w, errorStatus(err), err)
return
}
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(a.compositor.Status())
}
// handleGraphicsFlyOn atomically activates a layer and animates it from off-screen.
func (a *API) handleGraphicsFlyOn(w http.ResponseWriter, r *http.Request) {
a.setLastOperator(r)
id, err := parseLayerID(r)
if err != nil {
httperr.Write(w, http.StatusBadRequest, "invalid layer id")
return
}
var req flyRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
httperr.Write(w, http.StatusBadRequest, "invalid json")
return
}
if req.Direction != "left" && req.Direction != "right" && req.Direction != "top" && req.Direction != "bottom" {
httperr.Write(w, http.StatusBadRequest, "direction must be \"left\", \"right\", \"top\", or \"bottom\"")
return
}
if req.DurationMs <= 0 {
req.DurationMs = 500
}
if err := a.compositor.FlyOn(id, req.Direction, req.DurationMs); err != nil {
httperr.WriteErr(w, errorStatus(err), err)
return
}
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(a.compositor.Status())
}
// handleGraphicsSlide animates a layer from its current position to a new rect.
func (a *API) handleGraphicsSlide(w http.ResponseWriter, r *http.Request) {
a.setLastOperator(r)
id, err := parseLayerID(r)
if err != nil {
httperr.Write(w, http.StatusBadRequest, "invalid layer id")
return
}
var req slideRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
httperr.Write(w, http.StatusBadRequest, "invalid json")
return
}
if req.DurationMs <= 0 {
req.DurationMs = 500
}
toRect := image.Rect(req.X, req.Y, req.X+req.Width, req.Y+req.Height)
if err := a.compositor.SlideLayer(id, toRect, req.DurationMs); err != nil {
httperr.WriteErr(w, errorStatus(err), err)
return
}
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(a.compositor.Status())
}
// handleGraphicsImageUpload stores a PNG image on a layer.
func (a *API) handleGraphicsImageUpload(w http.ResponseWriter, r *http.Request) {
a.setLastOperator(r)
id, err := parseLayerID(r)
if err != nil {
httperr.Write(w, http.StatusBadRequest, "invalid layer id")
return
}
// Parse multipart form (max 16MB)
if err := r.ParseMultipartForm(16 << 20); err != nil {
httperr.Write(w, http.StatusBadRequest, "invalid multipart form")
return
}
file, header, err := r.FormFile("image")
if err != nil {
httperr.Write(w, http.StatusBadRequest, "image field required")
return
}
defer func() { _ = file.Close() }()
data, err := io.ReadAll(io.LimitReader(file, 16<<20))
if err != nil {
httperr.Write(w, http.StatusBadRequest, "failed to read image")
return
}
if err := a.compositor.SetImage(id, header.Filename, data); err != nil {
httperr.WriteErr(w, errorStatus(err), err)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
_ = json.NewEncoder(w).Encode(a.compositor.Status())
}
// handleGraphicsImageGet returns the stored PNG for a layer.
func (a *API) handleGraphicsImageGet(w http.ResponseWriter, r *http.Request) {
id, err := parseLayerID(r)
if err != nil {
httperr.Write(w, http.StatusBadRequest, "invalid layer id")
return
}
name, data, err := a.compositor.GetImage(id)
if err != nil {
httperr.WriteErr(w, errorStatus(err), err)
return
}
w.Header().Set("Content-Type", "image/png")
w.Header().Set("Content-Disposition", mime.FormatMediaType("inline", map[string]string{"filename": name}))
_, _ = w.Write(data)
}
// handleGraphicsImageDelete removes the stored image from a layer.
func (a *API) handleGraphicsImageDelete(w http.ResponseWriter, r *http.Request) {
a.setLastOperator(r)
id, err := parseLayerID(r)
if err != nil {
httperr.Write(w, http.StatusBadRequest, "invalid layer id")
return
}
if err := a.compositor.DeleteImage(id); err != nil {
httperr.WriteErr(w, errorStatus(err), err)
return
}
w.WriteHeader(http.StatusNoContent)
}
// textAnimRequest is the JSON body for starting a text animation.
type textAnimRequest struct {
Mode string `json:"mode"`
Text string `json:"text"`
FontSize float64 `json:"fontSize"`
Bold bool `json:"bold"`
CharsPerSec float64 `json:"charsPerSec"`
WordDelayMs int `json:"wordDelayMs"`
FadeDurationMs int `json:"fadeDurationMs"`
Width int `json:"width"`
Height int `json:"height"`
}
// handleGraphicsTextAnimStart starts a text animation on a layer.
func (a *API) handleGraphicsTextAnimStart(w http.ResponseWriter, r *http.Request) {
a.setLastOperator(r)
if a.textAnimEngine == nil {
httperr.Write(w, http.StatusNotImplemented, "text animation engine not available")
return
}
id, err := parseLayerID(r)
if err != nil {
httperr.Write(w, http.StatusBadRequest, "invalid layer id")
return
}
var req textAnimRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
httperr.Write(w, http.StatusBadRequest, "invalid json")
return
}
if req.Text == "" {
httperr.Write(w, http.StatusBadRequest, "text is required")
return
}
if req.Mode != "typewriter" && req.Mode != "fade-word" {
httperr.Write(w, http.StatusBadRequest, "mode must be \"typewriter\" or \"fade-word\"")
return
}
cfg := graphics.TextAnimationConfig{
Mode: req.Mode,
Text: req.Text,
FontSize: req.FontSize,
Bold: req.Bold,
CharsPerSec: req.CharsPerSec,
WordDelayMs: req.WordDelayMs,
FadeDurationMs: req.FadeDurationMs,
Width: req.Width,
Height: req.Height,
}
if err := a.textAnimEngine.Start(id, cfg); err != nil {
httperr.WriteErr(w, errorStatus(err), err)
return
}
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(a.compositor.Status())
}
// handleGraphicsTextAnimStop stops the text animation on a layer.
func (a *API) handleGraphicsTextAnimStop(w http.ResponseWriter, r *http.Request) {
a.setLastOperator(r)
if a.textAnimEngine == nil {
httperr.Write(w, http.StatusNotImplemented, "text animation engine not available")
return
}
id, err := parseLayerID(r)
if err != nil {
httperr.Write(w, http.StatusBadRequest, "invalid layer id")
return
}
if err := a.textAnimEngine.Stop(id); err != nil {
httperr.WriteErr(w, errorStatus(err), err)
return
}
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(a.compositor.Status())
}
// tickerRequest is the JSON body for starting a ticker.
type tickerRequest struct {
Text string `json:"text"`
FontSize float64 `json:"fontSize"`
Speed float64 `json:"speed"`
Bold bool `json:"bold"`
Loop bool `json:"loop"`
Height int `json:"height"`
}
// tickerTextRequest is the JSON body for updating ticker text.
type tickerTextRequest struct {
Text string `json:"text"`
}
// handleGraphicsTickerStart starts a scrolling ticker on a layer.
func (a *API) handleGraphicsTickerStart(w http.ResponseWriter, r *http.Request) {
a.setLastOperator(r)
if a.tickerEngine == nil {
httperr.Write(w, http.StatusNotImplemented, "ticker engine not available")
return
}
id, err := parseLayerID(r)
if err != nil {
httperr.Write(w, http.StatusBadRequest, "invalid layer id")
return
}
var req tickerRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
httperr.Write(w, http.StatusBadRequest, "invalid json")
return
}
if req.Text == "" {
httperr.Write(w, http.StatusBadRequest, "text is required")
return
}
if req.Speed <= 0 {
req.Speed = 100
}
if req.FontSize <= 0 {
req.FontSize = 24
}
cfg := graphics.TickerConfig{
Text: req.Text,
FontSize: req.FontSize,
Speed: req.Speed,
Bold: req.Bold,
Loop: req.Loop,
Height: req.Height,
}
if err := a.tickerEngine.Start(id, cfg); err != nil {
httperr.WriteErr(w, errorStatus(err), err)
return
}
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(a.compositor.Status())
}
// handleGraphicsTickerStop stops the ticker on a layer.
func (a *API) handleGraphicsTickerStop(w http.ResponseWriter, r *http.Request) {
a.setLastOperator(r)
if a.tickerEngine == nil {
httperr.Write(w, http.StatusNotImplemented, "ticker engine not available")
return
}
id, err := parseLayerID(r)
if err != nil {
httperr.Write(w, http.StatusBadRequest, "invalid layer id")
return
}
if err := a.tickerEngine.Stop(id); err != nil {
httperr.WriteErr(w, errorStatus(err), err)
return
}
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(a.compositor.Status())
}
// handleGraphicsTickerText updates the text of a running ticker.
func (a *API) handleGraphicsTickerText(w http.ResponseWriter, r *http.Request) {
a.setLastOperator(r)
if a.tickerEngine == nil {
httperr.Write(w, http.StatusNotImplemented, "ticker engine not available")
return
}
id, err := parseLayerID(r)
if err != nil {
httperr.Write(w, http.StatusBadRequest, "invalid layer id")
return
}
var req tickerTextRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
httperr.Write(w, http.StatusBadRequest, "invalid json")
return
}
if req.Text == "" {
httperr.Write(w, http.StatusBadRequest, "text is required")
return
}
if err := a.tickerEngine.UpdateText(id, req.Text); err != nil {
httperr.WriteErr(w, errorStatus(err), err)
return
}
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(a.compositor.Status())
}
// handleStingerList returns all loaded stinger clip names.
func (a *API) handleStingerList(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(a.stingerStore.List())
}
// handleStingerDelete removes a stinger clip by name.
func (a *API) handleStingerDelete(w http.ResponseWriter, r *http.Request) {
name := r.PathValue("name")
if err := validateStringLen("name", name, MaxNameLen); err != nil {
httperr.Write(w, http.StatusBadRequest, err.Error())
return
}
if err := a.stingerStore.Delete(name); err != nil {
httperr.WriteErr(w, errorStatus(err), err)
return
}
w.WriteHeader(http.StatusNoContent)
}
// stingerCutPointRequest is the JSON body for updating a stinger's cut point.
type stingerCutPointRequest struct {
CutPoint float64 `json:"cutPoint"`
}
// handleStingerCutPoint updates the cut point for a stinger clip.
func (a *API) handleStingerCutPoint(w http.ResponseWriter, r *http.Request) {
name := r.PathValue("name")
if err := validateStringLen("name", name, MaxNameLen); err != nil {
httperr.Write(w, http.StatusBadRequest, err.Error())
return
}
var req stingerCutPointRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
httperr.Write(w, http.StatusBadRequest, "invalid json")
return
}
if err := a.stingerStore.SetCutPoint(name, req.CutPoint); err != nil {
httperr.WriteErr(w, errorStatus(err), err)
return
}
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(map[string]string{"status": "ok"})
}
// handleStingerUpload accepts a zip file upload containing PNG frames for a stinger.
func (a *API) handleStingerUpload(w http.ResponseWriter, r *http.Request) {
name := r.PathValue("name")
if err := validateStringLen("name", name, MaxNameLen); err != nil {
httperr.Write(w, http.StatusBadRequest, err.Error())
return
}
r.Body = http.MaxBytesReader(w, r.Body, 256<<20)
data, err := io.ReadAll(r.Body)
if err != nil {
httperr.Write(w, http.StatusRequestEntityTooLarge, "upload too large (max 256MB)")
return
}
if err := a.stingerStore.Upload(name, data); err != nil {
httperr.WriteErr(w, errorStatus(err), err)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
_ = json.NewEncoder(w).Encode(map[string]string{"status": "ok"})
}
// handleGraphicsSequenceUpload uploads a ZIP of PNGs as an animated sequence on a layer.
func (a *API) handleGraphicsSequenceUpload(w http.ResponseWriter, r *http.Request) {
a.setLastOperator(r)
id, err := parseLayerID(r)
if err != nil {
httperr.Write(w, http.StatusBadRequest, "invalid layer id")
return
}
// Read ZIP body (max 256MB).
data, err := io.ReadAll(io.LimitReader(r.Body, 256<<20))
if err != nil {
httperr.Write(w, http.StatusBadRequest, "failed to read body")
return
}
if len(data) == 0 {
httperr.Write(w, http.StatusBadRequest, "empty body")
return
}
// Source fps is required so we can resample to pipeline fps.
fpsStr := r.URL.Query().Get("fps")
if fpsStr == "" {
httperr.Write(w, http.StatusBadRequest, "fps query parameter required (e.g. ?fps=59.94)")
return
}
sourceFPS, err := strconv.ParseFloat(fpsStr, 64)
if err != nil || sourceFPS <= 0 || sourceFPS > 240 {
httperr.Write(w, http.StatusBadRequest, "fps must be a positive number (e.g. 29.97, 59.94)")
return
}
if err := a.sequenceEngine.UploadAsync(id, data, sourceFPS); err != nil {
httperr.WriteErr(w, errorStatus(err), err)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusAccepted)
_ = json.NewEncoder(w).Encode(a.compositor.Status())
}
// handleGraphicsSequenceDelete removes an animated sequence from a layer.
func (a *API) handleGraphicsSequenceDelete(w http.ResponseWriter, r *http.Request) {
a.setLastOperator(r)
id, err := parseLayerID(r)
if err != nil {
httperr.Write(w, http.StatusBadRequest, "invalid layer id")
return
}
if err := a.sequenceEngine.Delete(id); err != nil {
httperr.WriteErr(w, errorStatus(err), err)
return
}
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(a.compositor.Status())
}
// handleGraphicsSequenceSpeed sets the playback speed for an animated sequence.
func (a *API) handleGraphicsSequenceSpeed(w http.ResponseWriter, r *http.Request) {
a.setLastOperator(r)
id, err := parseLayerID(r)
if err != nil {
httperr.Write(w, http.StatusBadRequest, "invalid layer id")
return
}
var req struct {
Speed float64 `json:"speed"`
}
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
httperr.Write(w, http.StatusBadRequest, "invalid json")
return
}
if err := a.sequenceEngine.SetSpeed(id, req.Speed); err != nil {
httperr.WriteErr(w, errorStatus(err), err)
return
}
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(a.compositor.Status())
}
// clipToStingerData converts a stinger.Clip to transition.StingerData.
func clipToStingerData(clip *stinger.Clip) *transition.StingerData {
frames := make([]transition.StingerFrameData, len(clip.Frames))
for i, f := range clip.Frames {
frames[i] = transition.StingerFrameData{
YUV: f.YUV,
Alpha: f.Alpha,
}
}
return &transition.StingerData{
Frames: frames,
Width: clip.Width,
Height: clip.Height,
CutPoint: clip.CutPoint,
Audio: clip.Audio,
AudioSampleRate: clip.AudioSampleRate,
AudioChannels: clip.AudioChannels,
}
}