2024-02-13 17:49:27 +00:00
|
|
|
// Package test contains test utilities.
|
|
|
|
package test
|
2023-10-31 13:19:04 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/bluenviron/mediamtx/internal/conf"
|
|
|
|
"github.com/bluenviron/mediamtx/internal/defs"
|
|
|
|
"github.com/bluenviron/mediamtx/internal/logger"
|
|
|
|
"github.com/bluenviron/mediamtx/internal/stream"
|
|
|
|
"github.com/bluenviron/mediamtx/internal/unit"
|
|
|
|
)
|
|
|
|
|
2024-02-13 17:49:27 +00:00
|
|
|
// SourceTester is a static source tester.
|
|
|
|
type SourceTester struct {
|
2023-10-31 13:19:04 +00:00
|
|
|
ctx context.Context
|
|
|
|
ctxCancel func()
|
|
|
|
stream *stream.Stream
|
2024-10-04 22:49:44 +00:00
|
|
|
reader stream.Reader
|
2023-10-31 13:19:04 +00:00
|
|
|
|
|
|
|
Unit chan unit.Unit
|
|
|
|
done chan struct{}
|
|
|
|
}
|
|
|
|
|
2024-02-13 17:49:27 +00:00
|
|
|
// NewSourceTester allocates a SourceTester.
|
2024-06-18 20:10:26 +00:00
|
|
|
func NewSourceTester(
|
|
|
|
createFunc func(defs.StaticSourceParent) defs.StaticSource,
|
|
|
|
resolvedSource string,
|
|
|
|
conf *conf.Path,
|
|
|
|
) *SourceTester {
|
2023-10-31 13:19:04 +00:00
|
|
|
ctx, ctxCancel := context.WithCancel(context.Background())
|
|
|
|
|
2024-02-13 17:49:27 +00:00
|
|
|
t := &SourceTester{
|
2023-10-31 13:19:04 +00:00
|
|
|
ctx: ctx,
|
|
|
|
ctxCancel: ctxCancel,
|
|
|
|
Unit: make(chan unit.Unit),
|
|
|
|
done: make(chan struct{}),
|
|
|
|
}
|
|
|
|
|
|
|
|
s := createFunc(t)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
s.Run(defs.StaticSourceRunParams{ //nolint:errcheck
|
2024-06-18 20:10:26 +00:00
|
|
|
Context: ctx,
|
|
|
|
ResolvedSource: resolvedSource,
|
|
|
|
Conf: conf,
|
2023-10-31 13:19:04 +00:00
|
|
|
})
|
|
|
|
close(t.done)
|
|
|
|
}()
|
|
|
|
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes the tester.
|
2024-02-13 17:49:27 +00:00
|
|
|
func (t *SourceTester) Close() {
|
2023-10-31 13:19:04 +00:00
|
|
|
t.ctxCancel()
|
2024-10-04 22:49:44 +00:00
|
|
|
t.stream.RemoveReader(t.reader)
|
2023-10-31 13:19:04 +00:00
|
|
|
<-t.done
|
|
|
|
}
|
|
|
|
|
|
|
|
// Log implements StaticSourceParent.
|
2024-02-13 17:49:27 +00:00
|
|
|
func (t *SourceTester) Log(_ logger.Level, _ string, _ ...interface{}) {
|
2023-10-31 13:19:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetReady implements StaticSourceParent.
|
2024-02-13 17:49:27 +00:00
|
|
|
func (t *SourceTester) SetReady(req defs.PathSourceStaticSetReadyReq) defs.PathSourceStaticSetReadyRes {
|
2023-10-31 13:19:04 +00:00
|
|
|
t.stream, _ = stream.New(
|
2024-10-04 22:49:44 +00:00
|
|
|
512,
|
2023-10-31 13:19:04 +00:00
|
|
|
1460,
|
|
|
|
req.Desc,
|
|
|
|
req.GenerateRTPPackets,
|
|
|
|
t,
|
|
|
|
)
|
|
|
|
|
2024-10-04 22:49:44 +00:00
|
|
|
t.reader = NilLogger
|
2024-03-19 13:01:14 +00:00
|
|
|
|
2024-10-04 22:49:44 +00:00
|
|
|
t.stream.AddReader(t.reader, req.Desc.Medias[0], req.Desc.Medias[0].Formats[0], func(u unit.Unit) error {
|
2023-10-31 13:19:04 +00:00
|
|
|
t.Unit <- u
|
|
|
|
close(t.Unit)
|
|
|
|
return nil
|
|
|
|
})
|
2024-10-04 22:49:44 +00:00
|
|
|
|
|
|
|
t.stream.StartReader(t.reader)
|
2023-10-31 13:19:04 +00:00
|
|
|
|
|
|
|
return defs.PathSourceStaticSetReadyRes{
|
|
|
|
Stream: t.stream,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetNotReady implements StaticSourceParent.
|
2024-02-13 17:49:27 +00:00
|
|
|
func (t *SourceTester) SetNotReady(_ defs.PathSourceStaticSetNotReadyReq) {
|
2023-10-31 13:19:04 +00:00
|
|
|
}
|