/
random-color.go
147 lines (127 loc) · 3.89 KB
/
random-color.go
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
package videostreams
import (
"context"
"image"
"image/color"
"image/draw"
"math/rand"
"net/http"
"strconv"
"syscall"
"time"
"github.com/labstack/echo/v4"
"github.com/pkg/errors"
"github.com/sargassum-world/godest/handling"
"golang.org/x/sync/errgroup"
"github.com/sargassum-world/pslive/internal/clients/mjpeg"
"github.com/sargassum-world/pslive/internal/clients/videostreams"
)
// Color generation
func newRandomColor() color.RGBA {
const ceiling = 256
//nolint:gosec // We don't need cryptographically-secure random number generation here
return color.RGBA{
R: uint8(rand.Intn(ceiling)),
G: uint8(rand.Intn(ceiling)),
B: uint8(rand.Intn(ceiling)),
A: ceiling - 1,
}
}
// Image generation
func newUniformImage(width, height int, co color.Color) image.Image {
im := image.NewRGBA(image.Rect(0, 0, width, height))
fillImage(im, co)
return im
}
func fillImage(im draw.Image, co color.Color) {
draw.Draw(im, im.Bounds(), &image.Uniform{co}, image.Point{}, draw.Src)
}
func newCurrentFrame(im image.Image) *videostreams.ImageFrame {
return &videostreams.ImageFrame{
Im: im,
Meta: &videostreams.Metadata{
ReceiveTime: time.Now(),
},
}
}
// Query Parameter Parsing
func parseIntParam(raw, name string, defaultValue int) (parsed int, err error) {
parsed = defaultValue
if raw != "" {
if parsed, err = strconv.Atoi(raw); err != nil {
return 0, errors.Wrapf(err, "invalid %s parameter %s", name, raw)
}
}
return parsed, err
}
func parseImageDimensions(rawWidth, rawHeight string) (width, height int, err error) {
width = 800
if width, err = parseIntParam(rawWidth, "width", width); err != nil {
return 0, 0, err
}
height = 600
if height, err = parseIntParam(rawHeight, "height", height); err != nil {
return 0, 0, err
}
return width, height, nil
}
// Handlers
func (h *Handlers) HandleRandomColorFrameGet() echo.HandlerFunc {
return func(c echo.Context) (err error) {
// Parse params
width, height, err := parseImageDimensions(c.QueryParam("width"), c.QueryParam("height"))
if err != nil {
return errors.Wrap(err, "couldn't parse image dimension query parameter")
}
const defaultQuality = 20
quality, err := parseIntParam(c.QueryParam("quality"), "quality", defaultQuality)
if err != nil {
return errors.Wrap(err, "couldn't parse image quality query parameter")
}
// Generate data
frame := newCurrentFrame(newUniformImage(width, height, newRandomColor()))
frame.Meta.Settings.JPEGEncodeQuality = quality
jpeg, err := frame.AsJPEGFrame()
if err != nil {
return errors.Wrap(err, "couldn't jpeg-encode image")
}
// Produce output
return c.Blob(http.StatusOK, "image/jpeg", jpeg.Im)
}
}
func (h *Handlers) HandleRandomColorStreamGet() echo.HandlerFunc {
return func(c echo.Context) (err error) {
// Parse params
width, height, err := parseImageDimensions(c.QueryParam("width"), c.QueryParam("height"))
if err != nil {
return errors.Wrap(err, "couldn't parse image dimension query parameter")
}
const defaultQuality = 20
quality, err := parseIntParam(c.QueryParam("quality"), "quality", defaultQuality)
if err != nil {
return errors.Wrap(err, "couldn't parse image quality query parameter")
}
frames := make(chan videostreams.Frame)
// TODO: also make it dependent on the server's context!
eg, egctx := errgroup.WithContext(c.Request().Context())
eg.Go(func() error {
const interval = 1 * time.Second
return handling.Repeat(egctx, interval, func() (done bool, err error) {
// Generate data
frame := newCurrentFrame(newUniformImage(width, height, newRandomColor()))
frame.Meta.Settings.JPEGEncodeQuality = quality
frames <- frame
return false, nil
})
})
eg.Go(func() error {
// Produce output
return mjpeg.SendFrameStream(egctx, c.Response().Writer, frames)
})
if err := handling.Except(eg.Wait(), context.Canceled, syscall.EPIPE); err != nil {
return err
}
close(frames)
return nil
}
}