-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhttpsource_test.go
More file actions
225 lines (200 loc) Β· 5.51 KB
/
httpsource_test.go
File metadata and controls
225 lines (200 loc) Β· 5.51 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
package main
import (
"bytes"
"fmt"
"image"
"image/color"
"image/jpeg"
"net/http"
"net/http/httptest"
"sync"
"testing"
"time"
)
// serveMJPEG writes n JPEG frames as a multipart/x-mixed-replace response.
func serveMJPEG(w http.ResponseWriter, frames [][]byte) {
w.Header().Set("Content-Type", "multipart/x-mixed-replace; boundary=frame")
for _, frame := range frames {
fmt.Fprintf(w, "--frame\r\nContent-Type: image/jpeg\r\nContent-Length: %d\r\n\r\n", len(frame))
w.Write(frame)
fmt.Fprint(w, "\r\n")
if f, ok := w.(http.Flusher); ok {
f.Flush()
}
}
// Write closing boundary so the reader gets EOF cleanly.
fmt.Fprint(w, "--frame--\r\n")
}
func TestHTTPSourceParseFrames(t *testing.T) {
frame := testJPEG(320, 240)
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Serve frames with a small delay so the poll loop can observe them
// before the stream ends and Clear() is called.
w.Header().Set("Content-Type", "multipart/x-mixed-replace; boundary=frame")
flusher, _ := w.(http.Flusher)
for i := 0; i < 10; i++ {
fmt.Fprintf(w, "--frame\r\nContent-Type: image/jpeg\r\nContent-Length: %d\r\n\r\n", len(frame))
if _, err := w.Write(frame); err != nil {
return
}
fmt.Fprint(w, "\r\n")
if flusher != nil {
flusher.Flush()
}
time.Sleep(20 * time.Millisecond)
}
}))
defer ts.Close()
buf := &FrameBuffer{}
buf.SetQuality(80)
mgr := NewHTTPSourceManager(ts.URL, buf)
// Run in goroutine; it will exit when the stream ends and retries.
done := make(chan struct{})
go func() {
defer close(done)
mgr.Run()
}()
// Wait for frames to arrive.
deadline := time.After(3 * time.Second)
for {
if buf.Latest() != nil {
break
}
select {
case <-deadline:
t.Fatal("timed out waiting for frames")
default:
time.Sleep(10 * time.Millisecond)
}
}
// Verify we got a valid JPEG.
got := buf.Latest()
if len(got) < 2 || got[0] != 0xFF || got[1] != 0xD8 {
t.Error("frame is not a valid JPEG")
}
mgr.Shutdown()
<-done
}
func TestHTTPSourceReconnect(t *testing.T) {
var mu sync.Mutex
callCount := 0
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
mu.Lock()
callCount++
n := callCount
mu.Unlock()
frames := [][]byte{testJPEG(320, 240)}
if n == 1 {
// First connection: serve one frame then close.
serveMJPEG(w, frames)
return
}
// Second connection: serve frames and keep open until client disconnects.
w.Header().Set("Content-Type", "multipart/x-mixed-replace; boundary=frame")
flusher, _ := w.(http.Flusher)
for i := 0; i < 50; i++ {
fmt.Fprintf(w, "--frame\r\nContent-Type: image/jpeg\r\nContent-Length: %d\r\n\r\n", len(frames[0]))
if _, err := w.Write(frames[0]); err != nil {
return
}
fmt.Fprint(w, "\r\n")
if flusher != nil {
flusher.Flush()
}
time.Sleep(50 * time.Millisecond)
}
}))
defer ts.Close()
buf := &FrameBuffer{}
buf.SetQuality(80)
mgr := NewHTTPSourceManager(ts.URL, buf)
done := make(chan struct{})
go func() {
defer close(done)
mgr.Run()
}()
// Wait for the first connection to complete and the status to transition
// through connecting β connected β disconnected β connecting β connected.
deadline := time.After(10 * time.Second)
for {
mu.Lock()
n := callCount
mu.Unlock()
if n >= 2 && buf.Status() == StatusConnected {
break
}
select {
case <-deadline:
t.Fatalf("timed out waiting for reconnect (calls=%d, status=%s)", callCount, buf.Status())
default:
time.Sleep(50 * time.Millisecond)
}
}
mgr.Shutdown()
<-done
}
func TestHTTPSourceCrop(t *testing.T) {
// Create a frame with black bars on left/right (pillarbox).
black := color.RGBA{0, 0, 0, 255}
bright := color.RGBA{200, 200, 200, 255}
img := makeImage(1920, 1080, black, bright, image.Rect(520, 0, 1400, 1080))
var jpegBuf bytes.Buffer
jpeg.Encode(&jpegBuf, img, &jpeg.Options{Quality: 90})
pillarboxFrame := jpegBuf.Bytes()
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Serve several identical pillarbox frames so crop stabilizes.
frames := make([][]byte, 5)
for i := range frames {
frames[i] = pillarboxFrame
}
serveMJPEG(w, frames)
}))
defer ts.Close()
buf := &FrameBuffer{}
buf.SetQuality(80)
mgr := NewHTTPSourceManager(ts.URL, buf)
done := make(chan struct{})
go func() {
defer close(done)
mgr.Run()
}()
deadline := time.After(3 * time.Second)
for {
if buf.Latest() != nil {
break
}
select {
case <-deadline:
t.Fatal("timed out waiting for frames")
default:
time.Sleep(10 * time.Millisecond)
}
}
// Raw should be original 1920x1080.
raw := buf.LatestRaw()
rawImg, err := jpeg.Decode(bytes.NewReader(raw))
if err != nil {
t.Fatalf("failed to decode raw frame: %v", err)
}
rawBounds := rawImg.Bounds()
if rawBounds.Dx() != 1920 || rawBounds.Dy() != 1080 {
t.Errorf("raw frame size = %dx%d, want 1920x1080", rawBounds.Dx(), rawBounds.Dy())
}
// Cropped should be narrower than 1920.
cropped := buf.Latest()
croppedImg, err := jpeg.Decode(bytes.NewReader(cropped))
if err != nil {
t.Fatalf("failed to decode cropped frame: %v", err)
}
croppedBounds := croppedImg.Bounds()
if croppedBounds.Dx() >= 1920 {
t.Errorf("cropped frame width = %d, expected less than 1920", croppedBounds.Dx())
}
// CropRect should reflect the detected content area.
cropRect := buf.CropRect()
if cropRect.Min.X == 0 && cropRect.Max.X == 1920 {
t.Error("crop rect spans full width, expected pillarbox crop")
}
mgr.Shutdown()
<-done
}