forked from viamrobotics/rdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmedia_test.go
215 lines (182 loc) · 5.57 KB
/
media_test.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
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
package gostream
import (
"context"
"image"
"image/color"
"sync"
"sync/atomic"
"testing"
"github.com/pion/mediadevices/pkg/prop"
"go.viam.com/test"
"go.viam.com/rdk/rimage"
)
// WrappedImage wraps an image.Image and includes a bool flag to track release status.
type WrappedImage struct {
Image image.Image
released atomic.Bool
t *testing.T
}
// Implement the image.Image interface for WrappedImage.
func (wi *WrappedImage) ColorModel() color.Model {
if wi.released.Load() {
wi.t.Fatalf("ColorModel method accessed after release")
}
return wi.Image.ColorModel()
}
func (wi *WrappedImage) Bounds() image.Rectangle {
if wi.released.Load() {
wi.t.Fatalf("Bounds method accessed after release")
}
return wi.Image.Bounds()
}
func (wi *WrappedImage) At(x, y int) color.Color {
if wi.released.Load() {
wi.t.Fatalf("At method accessed after release")
}
return wi.Image.At(x, y)
}
type imageSource struct {
WrappedImages []*WrappedImage
idx int
mu sync.Mutex
}
// Returns the next image or nil if there are no more images left. This should never error.
func (is *imageSource) Read(_ context.Context) (image.Image, func(), error) {
is.mu.Lock()
defer is.mu.Unlock()
if is.idx >= len(is.WrappedImages) {
return nil, func() {}, nil
}
wrappedImg := is.WrappedImages[is.idx]
release := func() {
wrappedImg.released.Store(true)
}
is.idx++
return wrappedImg, release, nil
}
func (is *imageSource) Close(_ context.Context) error {
return nil
}
func createWrappedImage(t *testing.T, c color.Color) *WrappedImage {
w, h := 640, 480
img := image.NewRGBA(image.Rect(0, 0, w, h))
for x := 0; x < w; x++ {
for y := 0; y < h; y++ {
img.Set(x, y, c)
}
}
return &WrappedImage{Image: img, t: t}
}
func TestReadMedia(t *testing.T) {
colors := []*WrappedImage{
createWrappedImage(t, rimage.Red),
createWrappedImage(t, rimage.Blue),
createWrappedImage(t, rimage.Green),
createWrappedImage(t, rimage.Yellow),
createWrappedImage(t, rimage.Purple),
createWrappedImage(t, rimage.Cyan),
}
imgSource := &imageSource{WrappedImages: colors}
videoSrc := NewVideoSource(imgSource, prop.Video{})
// Test all images are returned in order.
for i, expected := range colors {
actual, release, err := ReadMedia(context.Background(), videoSrc)
test.That(t, err, test.ShouldBeNil)
test.That(t, actual, test.ShouldNotBeNil)
for j, color := range colors {
if color == expected {
continue
}
if actual == color {
t.Logf("did not expect actual color to equal other color at %d when expecting %d", j, i)
}
}
test.That(t, actual, test.ShouldEqual, expected)
// Call release and check if it sets the flag
release()
w := actual.(*WrappedImage)
test.That(t, w.released.Load(), test.ShouldBeTrue)
}
}
// Test that image comparison should fail if two images are not the same.
func TestImageComparison(t *testing.T) {
imgSource := &imageSource{WrappedImages: []*WrappedImage{createWrappedImage(t, rimage.Red)}}
videoSrc := NewVideoSource(imgSource, prop.Video{})
pink := createWrappedImage(t, rimage.Pink)
red, release, err := ReadMedia(context.Background(), videoSrc)
test.That(t, err, test.ShouldBeNil)
test.That(t, red, test.ShouldNotEqual, pink)
// Call release and check if it sets the flag
release()
w := red.(*WrappedImage)
test.That(t, w.released.Load(), test.ShouldBeTrue)
}
// TestMultipleConsumers tests concurrent consumption of images from imageSource via stream.Next().
func TestStreamMultipleConsumers(t *testing.T) {
colors := []*WrappedImage{
createWrappedImage(t, rimage.Red),
createWrappedImage(t, rimage.Blue),
createWrappedImage(t, rimage.Green),
createWrappedImage(t, rimage.Yellow),
createWrappedImage(t, rimage.Purple),
createWrappedImage(t, rimage.Cyan),
}
imgSource := &imageSource{WrappedImages: colors}
videoSrc := NewVideoSource(imgSource, prop.Video{})
stream, err := videoSrc.Stream(context.Background())
test.That(t, err, test.ShouldBeNil)
numConsumers := 3
var wg sync.WaitGroup
wg.Add(numConsumers)
// Coordinates index accesses to images
var mu sync.Mutex
j := 0
for i := 0; i < numConsumers; i++ {
go func(consumerId int) {
defer wg.Done()
for {
mu.Lock()
if j >= len(colors) {
mu.Unlock()
break
}
currIndex := j
j++
mu.Unlock()
t.Logf("Consumer %d is processing image %d\n", consumerId, currIndex)
actual, release, err := stream.Next(context.Background())
test.That(t, err, test.ShouldBeNil)
test.That(t, actual, test.ShouldNotBeNil)
test.That(t, release, test.ShouldNotBeNil)
// Release the image and check if it was released
t.Logf("Consumer %d releasing image %d\n", consumerId, currIndex+1)
release()
}
}(i)
}
wg.Wait()
videoSrc.Close(context.Background())
// Verify that all images have been released
for i, wrappedImg := range imgSource.WrappedImages {
t.Logf("Image at index %d.", i)
test.That(t, wrappedImg.released.Load(), test.ShouldBeTrue)
}
}
func TestStreamWithoutNext(t *testing.T) {
colors := []*WrappedImage{createWrappedImage(t, rimage.Red)}
imgSource := &imageSource{WrappedImages: colors}
videoSrc := NewVideoSource(imgSource, prop.Video{})
// Start stream
stream, err := videoSrc.Stream(context.Background())
test.That(t, err, test.ShouldBeNil)
// Get one frame
_, release, err := stream.Next(context.Background())
test.That(t, err, test.ShouldBeNil)
release()
// Close stream
stream.Close(context.Background())
// Spin up stream and close without calling Next
stream, err = videoSrc.Stream(context.Background())
test.That(t, err, test.ShouldBeNil)
stream.Close(context.Background())
}