-
Notifications
You must be signed in to change notification settings - Fork 8
/
rtsp_test.go
504 lines (454 loc) · 20.4 KB
/
rtsp_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
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
package viamrtsp
import (
"context"
"encoding/base64"
"errors"
"image"
"sync"
"testing"
"time"
"github.com/bluenviron/gortsplib/v4"
"github.com/bluenviron/gortsplib/v4/pkg/base"
"github.com/bluenviron/gortsplib/v4/pkg/description"
"github.com/bluenviron/gortsplib/v4/pkg/format"
"github.com/bluenviron/gortsplib/v4/pkg/rtptime"
"github.com/bluenviron/mediacommon/pkg/codecs/h264"
"github.com/koron/go-ssdp"
"github.com/pion/rtp"
"github.com/viam-modules/viamrtsp/viamupnp"
"go.viam.com/rdk/components/camera"
"go.viam.com/rdk/components/camera/rtppassthrough"
"go.viam.com/rdk/logging"
"go.viam.com/rdk/resource"
"go.viam.com/rdk/rimage/transform"
"go.viam.com/test"
"go.viam.com/utils"
)
func TestRTSPCamera(t *testing.T) {
SetLibAVLogLevelFatal()
logger := logging.NewTestLogger(t)
bURL, err := base.ParseURL("rtsp://127.0.0.1:32512")
test.That(t, err, test.ShouldBeNil)
t.Run("H264", func(t *testing.T) {
forma := &format.H264{
PayloadTyp: 96,
PacketizationMode: 1,
SPS: []uint8{
0x67, 0x64, 0x00, 0x15, 0xac, 0xb2, 0x03, 0xc1,
0x1f, 0xd6, 0x02, 0xdc, 0x08, 0x08, 0x16, 0x94,
0x00, 0x00, 0x03, 0x00, 0x04, 0x00, 0x00, 0x03,
0x00, 0xf0, 0x3c, 0x58, 0xb9, 0x20,
},
PPS: []uint8{0x68, 0xeb, 0xc3, 0xcb, 0x22, 0xc0},
}
t.Run("init", func(t *testing.T) {
h, closeFunc := newH264ServerHandler(t, forma, bURL, logger)
test.That(t, h.s.Start(), test.ShouldBeNil)
timeoutCtx, timeoutCancel := context.WithTimeout(context.Background(), time.Second*10)
defer timeoutCancel()
config := resource.NewEmptyConfig(camera.Named("foo"), ModelAgnostic)
config.ConvertedAttributes = &Config{Address: "rtsp://" + h.s.RTSPAddress}
rtspCam, err := NewRTSPCamera(timeoutCtx, nil, config, logger)
test.That(t, err, test.ShouldBeNil)
defer func() { test.That(t, rtspCam.Close(context.Background()), test.ShouldBeNil) }()
test.That(t, rtspCam.Name().Name, test.ShouldEqual, "foo")
closeFunc()
})
t.Run("GetImage", func(t *testing.T) {
h, closeFunc := newH264ServerHandler(t, forma, bURL, logger)
defer closeFunc()
test.That(t, h.s.Start(), test.ShouldBeNil)
timeoutCtx, timeoutCancel := context.WithTimeout(context.Background(), time.Second*10)
defer timeoutCancel()
config := resource.NewEmptyConfig(camera.Named("foo"), ModelAgnostic)
config.ConvertedAttributes = &Config{Address: "rtsp://" + h.s.RTSPAddress}
rtspCam, err := NewRTSPCamera(timeoutCtx, nil, config, logger)
test.That(t, err, test.ShouldBeNil)
defer func() { test.That(t, rtspCam.Close(context.Background()), test.ShouldBeNil) }()
imageTimeoutCtx, imageTimeoutCancel := context.WithTimeout(context.Background(), time.Second*10)
defer imageTimeoutCancel()
var im image.Image
for imageTimeoutCtx.Err() == nil {
img, f, err := camera.ReadImage(imageTimeoutCtx, rtspCam)
if err != nil {
continue
}
f()
if img != nil {
im = img
break
}
}
test.That(t, imageTimeoutCtx.Err(), test.ShouldBeNil)
test.That(t, im.Bounds(), test.ShouldResemble, image.Rect(0, 0, 480, 270))
})
t.Run("SubscribeRTP", func(t *testing.T) {
t.Run("RTPPassthrough config variations", func(t *testing.T) {
cases := []struct {
name string
rtpPassthrough *bool
expectSubscribeError error
}{
{"when RTPPassthrough = true", func() *bool { b := true; return &b }(), nil},
{"when RTPPassthrough = false", func() *bool { b := false; return &b }(), ErrH264PassthroughNotEnabled},
{"when RTPPassthrough is not specified", nil, nil},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
// Server and camera setup
h, closeFunc := newH264ServerHandler(t, forma, bURL, logger)
defer closeFunc()
test.That(t, h.s.Start(), test.ShouldBeNil)
timeoutCtx, timeoutCancel := context.WithTimeout(context.Background(), 10*time.Second)
defer timeoutCancel()
config := resource.NewEmptyConfig(camera.Named("foo"), ModelAgnostic)
config.ConvertedAttributes = &Config{
Address: "rtsp://" + h.s.RTSPAddress,
RTPPassthrough: tc.rtpPassthrough,
}
rtspCam, err := NewRTSPCamera(timeoutCtx, nil, config, logger)
test.That(t, err, test.ShouldBeNil)
defer func() { test.That(t, rtspCam.Close(context.Background()), test.ShouldBeNil) }()
vcs, ok := rtspCam.(rtppassthrough.Source)
test.That(t, ok, test.ShouldBeTrue)
// Subscription and packet tests
if tc.expectSubscribeError == nil {
cancelCtx, cancel := context.WithCancel(context.Background())
sub, err := vcs.SubscribeRTP(timeoutCtx, 512, func(pkts []*rtp.Packet) {
if len(pkts) > 0 {
logger.Info("got packets")
cancel()
}
})
test.That(t, err, test.ShouldBeNil)
defer func() {
err := vcs.Unsubscribe(context.Background(), sub.ID)
test.That(t, err, test.ShouldBeNil)
}()
select {
case <-timeoutCtx.Done():
t.Log("timed out waiting for packets")
t.FailNow()
case <-cancelCtx.Done():
// We got packets and are happy
}
} else {
_, err := vcs.SubscribeRTP(timeoutCtx, 512, func(_ []*rtp.Packet) {
t.Log("should not happen")
t.FailNow()
})
test.That(t, err, test.ShouldBeError, tc.expectSubscribeError)
}
})
}
})
})
})
}
func TestRTSPConfig(t *testing.T) {
// success
rtspConf := &Config{Address: "rtsp://example.com:5000"}
_, err := rtspConf.Validate("path")
test.That(t, err, test.ShouldBeNil)
// badly formatted rtsp address
rtspConf = &Config{Address: "http://example.com"}
_, err = rtspConf.Validate("path")
test.That(t, err, test.ShouldNotBeNil)
test.That(t, err.Error(), test.ShouldContainSubstring, "unsupported scheme")
// bad intrinsic parameters
rtspConf = &Config{
Address: "rtsp://example.com:5000",
IntrinsicParams: &transform.PinholeCameraIntrinsics{},
}
_, err = rtspConf.Validate("path")
test.That(t, err, test.ShouldNotBeNil)
test.That(t, err, test.ShouldWrap, transform.ErrNoIntrinsics)
// good intrinsic parameters
rtspConf = &Config{
Address: "rtsp://example.com:5000",
IntrinsicParams: &transform.PinholeCameraIntrinsics{
Width: 1,
Height: 2,
Fx: 3,
Fy: 4,
Ppx: 5,
Ppy: 6,
},
}
_, err = rtspConf.Validate("path")
test.That(t, err, test.ShouldBeNil)
// no distortion parameters is OK
rtspConf.DistortionParams = &transform.BrownConrady{}
test.That(t, err, test.ShouldBeNil)
}
// Dedicated test for performance benchmarking.
func TestRTSPCameraPerformance(t *testing.T) {
SetLibAVLogLevelFatal()
logger := logging.NewTestLogger(t)
bURL, err := base.ParseURL("rtsp://127.0.0.1:32512")
test.That(t, err, test.ShouldBeNil)
t.Run("PerformanceTestGetImage", func(t *testing.T) {
forma := &format.H264{
PayloadTyp: 96,
PacketizationMode: 1,
SPS: []uint8{
0x67, 0x64, 0x00, 0x15, 0xac, 0xb2, 0x03, 0xc1,
0x1f, 0xd6, 0x02, 0xdc, 0x08, 0x08, 0x16, 0x94,
0x00, 0x00, 0x03, 0x00, 0x04, 0x00, 0x00, 0x03,
0x00, 0xf0, 0x3c, 0x58, 0xb9, 0x20,
},
PPS: []uint8{0x68, 0xeb, 0xc3, 0xcb, 0x22, 0xc0},
}
h, closeFunc := newH264ServerHandler(t, forma, bURL, logger)
defer closeFunc()
test.That(t, h.s.Start(), test.ShouldBeNil)
timeoutCtx, timeoutCancel := context.WithTimeout(context.Background(), time.Second*10)
defer timeoutCancel()
config := resource.NewEmptyConfig(camera.Named("foo"), ModelAgnostic)
config.ConvertedAttributes = &Config{Address: "rtsp://" + h.s.RTSPAddress}
rtspCam, err := NewRTSPCamera(timeoutCtx, nil, config, logger)
test.That(t, err, test.ShouldBeNil)
defer func() { test.That(t, rtspCam.Close(context.Background()), test.ShouldBeNil) }()
imageTimeoutCtx, imageTimeoutCancel := context.WithTimeout(context.Background(), time.Second*10)
defer imageTimeoutCancel()
const iterations = 100
var im image.Image
var frameAvailable bool
// A loop to keep trying to get the first image until a frame is available.
for {
img, f, err := camera.ReadImage(timeoutCtx, rtspCam)
if err == nil && img != nil {
f()
im = img
frameAvailable = true
break
}
if imageTimeoutCtx.Err() != nil {
t.Fatalf("Timeout waiting for the first frame")
}
}
// Validate the first retrieved image
test.That(t, im.Bounds(), test.ShouldResemble, image.Rect(0, 0, 480, 270))
if !frameAvailable {
t.Fatal("No frame became available before starting the performance test")
}
// Performance testing: Loop over multiple GetImage calls
for range make([]int, iterations) {
img, f, err := camera.ReadImage(timeoutCtx, rtspCam)
test.That(t, err, test.ShouldBeNil)
f()
test.That(t, img.Bounds(), test.ShouldResemble, image.Rect(0, 0, 480, 270))
time.Sleep(50 * time.Millisecond)
}
})
}
type serverHandler struct {
s *gortsplib.Server
wg sync.WaitGroup
media *description.Media
OnConnOpenFunc func(*gortsplib.ServerHandlerOnConnOpenCtx, *serverHandler)
OnConnCloseFunc func(*gortsplib.ServerHandlerOnConnCloseCtx, *serverHandler)
OnSessionOpenFunc func(*gortsplib.ServerHandlerOnSessionOpenCtx, *serverHandler)
OnSessionCloseFunc func(*gortsplib.ServerHandlerOnSessionCloseCtx, *serverHandler)
OnDescribeFunc func(*gortsplib.ServerHandlerOnDescribeCtx, *serverHandler) (*base.Response, *gortsplib.ServerStream, error)
OnAnnounceFunc func(*gortsplib.ServerHandlerOnAnnounceCtx, *serverHandler) (*base.Response, error)
OnSetupFunc func(*gortsplib.ServerHandlerOnSetupCtx, *serverHandler) (*base.Response, *gortsplib.ServerStream, error)
OnPlayFunc func(*gortsplib.ServerHandlerOnPlayCtx, *serverHandler) (*base.Response, error)
OnRecordFunc func(*gortsplib.ServerHandlerOnRecordCtx, *serverHandler) (*base.Response, error)
mu sync.Mutex
stream *gortsplib.ServerStream
}
// called when a connection is opened.
func (sh *serverHandler) OnConnOpen(ctx *gortsplib.ServerHandlerOnConnOpenCtx) {
if sh.OnConnOpenFunc == nil {
return
}
sh.OnConnOpenFunc(ctx, sh)
}
// called when a connection is closed.
func (sh *serverHandler) OnConnClose(ctx *gortsplib.ServerHandlerOnConnCloseCtx) {
if sh.OnConnCloseFunc == nil {
return
}
sh.OnConnCloseFunc(ctx, sh)
}
// called when a session is opened.
func (sh *serverHandler) OnSessionOpen(ctx *gortsplib.ServerHandlerOnSessionOpenCtx) {
if sh.OnSessionOpenFunc == nil {
return
}
sh.OnSessionOpenFunc(ctx, sh)
}
// called when a session is closed.
func (sh *serverHandler) OnSessionClose(ctx *gortsplib.ServerHandlerOnSessionCloseCtx) {
if sh.OnSessionCloseFunc == nil {
return
}
sh.OnSessionCloseFunc(ctx, sh)
}
// called when receiving a DESCRIBE request.
func (sh *serverHandler) OnDescribe(ctx *gortsplib.ServerHandlerOnDescribeCtx) (*base.Response, *gortsplib.ServerStream, error) {
return sh.OnDescribeFunc(ctx, sh)
}
// called when receiving an ANNOUNCE request.
func (sh *serverHandler) OnAnnounce(ctx *gortsplib.ServerHandlerOnAnnounceCtx) (*base.Response, error) {
return sh.OnAnnounceFunc(ctx, sh)
}
// called when receiving a SETUP request.
func (sh *serverHandler) OnSetup(ctx *gortsplib.ServerHandlerOnSetupCtx) (*base.Response, *gortsplib.ServerStream, error) {
return sh.OnSetupFunc(ctx, sh)
}
// called when receiving a PLAY request.
func (sh *serverHandler) OnPlay(ctx *gortsplib.ServerHandlerOnPlayCtx) (*base.Response, error) {
return sh.OnPlayFunc(ctx, sh)
}
// called when receiving a RECORD request.
func (sh *serverHandler) OnRecord(ctx *gortsplib.ServerHandlerOnRecordCtx) (*base.Response, error) {
return sh.OnRecordFunc(ctx, sh)
}
func newH264ServerHandler(
t *testing.T,
forma *format.H264,
bURL *base.URL,
logger logging.Logger,
) (*serverHandler, func()) {
stopCtx, stopFunc := context.WithCancel(context.Background())
//nolint:lll
h264Base64 := "AAAAAWdkABWs2UHgj+sBbgQEC0oAAAMAAgAAAwB4HixbLAAAAAFo6+PLIsAAAAEGBf//qtxF6b3m2Ui3lizYINkj7u94MjY0IC0gY29yZSAxNjQgcjMxMDggMzFlMTlmOSAtIEguMjY0L01QRUctNCBBVkMgY29kZWMgLSBDb3B5bGVmdCAyMDAzLTIwMjMgLSBodHRwOi8vd3d3LnZpZGVvbGFuLm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aSh3Jg7LSyWYjwh5h2z1BkMtI70WrByNVpM/4T7MDbOrIAKI754SehKnoR6KcUFPNuB822EeLBrmepwYlazXCZw9zEjfgv6p926GWp91aihKejMxEi0iRtBa8WPPEnQX9b/n5E3m6sNZzpUwBQl+w/crvehVS3Y2b+p8kIyVOMrVNdRiVHZ3MzGRO6A0KOEfgiU3klIJLMeR/fL55X/NrRi6noRxQngACe3ZelEAG69D5Uy90+2SIQUh42+y/mMTciu9KMETpPt0PV6Fmp3pt+zH5yo/olNHZiZWf1ou712PVsly1vzX+AZgMzvLUWd38ksQpfuOQj9w12vFyT16XH0ruPTyXIhvWEQDfKqvyq0uXqLNwawVI01QZEk4R3UCEjRZGgz6bn+394KqQziqNPIAAAlvvLgRRzOXlgIIi+bhx9ukpKsNBj2s4QOFVV6RU0Ur3q0mtkEFRRim6gqRvWI0DHOBgeBtWT+SUWASA6vb0HfsktyuHoHrTgIeOGDn0C4bkCQOzN5U9D7LpKP1+wGhN2Vyn96MYFPX4xPEIhagrzEK/A1RS6kbEgAAKP17yobsMoFjJdT5y0o0lHV6ZTG2zss7+8ZFyeSk5BgKPEFfHtAxLMaAppsZpccygmABfBOUVz6HXuyCs40JvsKa78mhUirkd0lXXGwexp1Cyaw11QOaVgxpZUV77CABmO+UESL5NPur+AA6W1f/48tG8XA6bMTEHaJh5Ep7hgjxMs+CWnHGlIy9DpaQjLa4lzUvZr+SRBU+URuhv/FWj+h3p+N8yCFp22DNcba2oaKCkFaHbFbXMDG6uPg0hUf9PJlD2TedajGWRIVPn8za76tcY5mKhI9x/5nUG4HWYumHeTourcELQ=="
h := &serverHandler{
media: &description.Media{
Type: description.MediaTypeVideo,
Formats: []format.Format{forma},
},
OnSessionCloseFunc: func(_ *gortsplib.ServerHandlerOnSessionCloseCtx, sh *serverHandler) {
logger.Debug("OnSessionCloseFunc")
sh.mu.Lock()
defer sh.mu.Unlock()
},
OnDescribeFunc: func(_ *gortsplib.ServerHandlerOnDescribeCtx, sh *serverHandler) (*base.Response, *gortsplib.ServerStream, error) {
logger.Debug("OnDescribeFunc")
defer logger.Debug("OnDescribeFunc DONE")
sh.mu.Lock()
defer sh.mu.Unlock()
d := &description.Session{
BaseURL: bURL,
Title: "123456",
Medias: []*description.Media{sh.media},
}
sh.stream = gortsplib.NewServerStream(sh.s, d)
logger.Debug("sh.stream.Description().Medias[0].Formats[0]: %#v ", sh.stream.Description().Medias[0].Formats[0])
return &base.Response{StatusCode: base.StatusOK}, sh.stream, nil
},
OnAnnounceFunc: func(_ *gortsplib.ServerHandlerOnAnnounceCtx, _ *serverHandler) (*base.Response, error) {
logger.Debug("OnAnnounceFunc")
t.Log("should not be called")
t.FailNow()
return nil, errors.New("should not be called")
},
OnSetupFunc: func(_ *gortsplib.ServerHandlerOnSetupCtx, sh *serverHandler) (*base.Response, *gortsplib.ServerStream, error) {
logger.Debug("OnSetupFunc")
return &base.Response{StatusCode: base.StatusOK}, sh.stream, nil
},
// This will play an MJpeg video which only has frames which are red squares
// This is so that the result of GetImage is determanistic
OnPlayFunc: func(_ *gortsplib.ServerHandlerOnPlayCtx, sh *serverHandler) (*base.Response, error) {
logger.Debug("OnPlayFunc")
sh.wg.Add(1)
utils.ManagedGo(func() {
rtpEnc, err := forma.CreateEncoder()
if err != nil {
t.Log(err.Error())
t.FailNow()
}
rtpTime := &rtptime.Encoder{ClockRate: forma.ClockRate()}
err = rtpTime.Initialize()
if err != nil {
t.Log(err.Error())
t.FailNow()
}
start := time.Now()
// setup a ticker to sleep between frames
ticker := time.NewTicker(200 * time.Millisecond)
defer ticker.Stop()
b, err := base64.StdEncoding.DecodeString(h264Base64)
test.That(t, err, test.ShouldBeNil)
aus, err := h264.AnnexBUnmarshal(b)
test.That(t, err, test.ShouldBeNil)
// Continuously send packets until the server is stopped
for range ticker.C {
if stopCtx.Err() != nil {
return
}
sh.mu.Lock()
if sh.stream == nil {
sh.mu.Unlock()
return
}
sh.mu.Unlock()
pkts, err := rtpEnc.Encode(aus)
if err != nil {
t.Log(err.Error())
t.FailNow()
}
// get current timestamp
ts := rtpTime.Encode(time.Since(start))
// write packets to the server
for _, pkt := range pkts {
pkt.Timestamp = ts
sh.mu.Lock()
err = sh.stream.WritePacketRTP(sh.media, pkt)
sh.mu.Unlock()
if err != nil {
logger.Debug(err.Error())
return
}
}
}
}, sh.wg.Done)
return &base.Response{StatusCode: base.StatusOK}, nil
},
OnRecordFunc: func(_ *gortsplib.ServerHandlerOnRecordCtx, _ *serverHandler) (*base.Response, error) {
logger.Debug("OnRecordFunc")
t.Log("should not be called")
t.FailNow()
return nil, errors.New("should not be called")
},
}
h.s = &gortsplib.Server{
Handler: h,
RTSPAddress: "127.0.0.1:32512",
}
return h, func() {
stopFunc()
h.s.Close()
test.That(t, h.s.Wait(), test.ShouldBeError, errors.New("terminated"))
h.wg.Wait()
}
}
func TestUPNPStuff(t *testing.T) {
ctx := context.Background()
logger := logging.NewTestLogger(t)
ctx = context.WithValue(ctx,
viamupnp.FindAllTestKey,
[]viamupnp.UPNPDevice{
{ssdp.Service{Location: "http://eliot:12312/asd.xml"}, nil},
},
)
u, err := base.ParseURL("rtsp://a:b@UPNP_DISCOVER/abc")
test.That(t, err, test.ShouldBeNil)
test.That(t, u.Host, test.ShouldEqual, "UPNP_DISCOVER")
c := Config{
Address: "rtsp://a:b@foo/abc",
}
u, err = c.parseAndFixAddress(ctx, logger)
test.That(t, err, test.ShouldBeNil)
test.That(t, u.Host, test.ShouldEqual, "foo")
c.Address = "rtsp://a:b@UPNP_DISCOVER/abc"
u, err = c.parseAndFixAddress(ctx, logger)
test.That(t, err, test.ShouldBeNil)
test.That(t, u.Host, test.ShouldEqual, "eliot")
c.Address = "rtsp://a:b@UPNP_DISCOVER:1234/abc"
u, err = c.parseAndFixAddress(ctx, logger)
test.That(t, err, test.ShouldBeNil)
test.That(t, u.Host, test.ShouldEqual, "eliot:1234")
}