-
Notifications
You must be signed in to change notification settings - Fork 15
/
video_transforms.go
48 lines (41 loc) · 1.15 KB
/
video_transforms.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
package gostream
import (
"context"
"image"
"github.com/disintegration/imaging"
"github.com/pion/mediadevices/pkg/prop"
"go.uber.org/multierr"
)
type resizeVideoSource struct {
src VideoSource
stream VideoStream
width, height int
}
// NewResizeVideoSource returns a source that resizes images to the set dimensions.
func NewResizeVideoSource(src VideoSource, width, height int) VideoSource {
rvs := &resizeVideoSource{
src: src,
stream: NewEmbeddedVideoStream(src),
width: width,
height: height,
}
return NewVideoSource(rvs, prop.Video{
Width: rvs.width,
Height: rvs.height,
})
}
// Read returns a resized image to Width x Height dimensions.
func (rvs resizeVideoSource) Read(ctx context.Context) (image.Image, func(), error) {
img, release, err := rvs.stream.Next(ctx)
if err != nil {
return nil, nil, err
}
if release != nil {
defer release()
}
return imaging.Resize(img, rvs.width, rvs.height, imaging.NearestNeighbor), func() {}, nil
}
// Close closes the underlying source.
func (rvs resizeVideoSource) Close(ctx context.Context) error {
return multierr.Combine(rvs.stream.Close(ctx), rvs.src.Close(ctx))
}