Add flinger.SetConfig to update FPS/quality/name/audio/announce without restarting the stream. Fields that require a restart (source, port, stream-index) are preserved. The video loop reads the live config each frame so changes apply immediately, and the GUI Quality/Frame-rate dropdowns push changes to a running engine. Verified live: fps 12->33 and JPEG size 210KB->1.3MB on quality change.
192 lines
4.5 KiB
Go
192 lines
4.5 KiB
Go
package flinger
|
|
|
|
import (
|
|
"errors"
|
|
"net"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// TestEnginePatternStartStop runs the engine with the synthetic pattern source
|
|
// and verifies it produces frames on the wire and stops cleanly.
|
|
func TestEnginePatternStartStop(t *testing.T) {
|
|
cfg := DefaultConfig()
|
|
cfg.Source = "pattern"
|
|
cfg.Port = 19756 // fixed high port for the test
|
|
|
|
eng, err := New(cfg)
|
|
if err != nil {
|
|
t.Fatalf("New: %v", err)
|
|
}
|
|
|
|
eng.Start()
|
|
defer eng.Stop()
|
|
|
|
// Connect a raw receiver and read until the engine reports frames sent.
|
|
conn, err := net.Dial("tcp", "127.0.0.1:"+itoa(eng.sender.Port()))
|
|
if err != nil {
|
|
t.Fatalf("dial: %v", err)
|
|
}
|
|
defer func() { _ = conn.Close() }()
|
|
|
|
// The video loop sends ~30fps; wait for the counter to advance.
|
|
deadline := time.Now().Add(3 * time.Second)
|
|
_ = conn.SetReadDeadline(deadline)
|
|
|
|
// Drain whatever arrives while waiting for the frame counter to move.
|
|
go func() {
|
|
buf := make([]byte, 64*1024)
|
|
for {
|
|
if _, err := conn.Read(buf); err != nil {
|
|
return
|
|
}
|
|
}
|
|
}()
|
|
|
|
for time.Now().Before(deadline) {
|
|
if eng.Status().Frames > 0 {
|
|
break
|
|
}
|
|
time.Sleep(20 * time.Millisecond)
|
|
}
|
|
|
|
if eng.Status().Frames == 0 {
|
|
t.Error("expected frames to be counted")
|
|
}
|
|
}
|
|
|
|
// TestEngineConfigDefaults verifies DefaultConfig is sane.
|
|
func TestEngineConfigDefaults(t *testing.T) {
|
|
c := DefaultConfig()
|
|
if c.Port != 9756 {
|
|
t.Errorf("default port = %d, want 9756", c.Port)
|
|
}
|
|
if c.Source != "screen" {
|
|
t.Errorf("default source = %q, want screen", c.Source)
|
|
}
|
|
if c.Quality < 1 || c.Quality > 100 {
|
|
t.Errorf("default quality %d out of range", c.Quality)
|
|
}
|
|
}
|
|
|
|
// TestNewRejectsBadSource ensures invalid sources fail fast.
|
|
func TestNewRejectsBadSource(t *testing.T) {
|
|
cfg := DefaultConfig()
|
|
cfg.Source = "bogus"
|
|
if _, err := New(cfg); err == nil {
|
|
t.Error("expected error for unknown source")
|
|
}
|
|
}
|
|
|
|
// TestSetErrStatus verifies runtime errors are exposed via Status and can be
|
|
// cleared.
|
|
func TestSetErrStatus(t *testing.T) {
|
|
cfg := DefaultConfig()
|
|
cfg.Source = "pattern"
|
|
cfg.Port = 19757
|
|
|
|
eng, err := New(cfg)
|
|
if err != nil {
|
|
t.Fatalf("New: %v", err)
|
|
}
|
|
|
|
if st := eng.Status(); st.Err != nil {
|
|
t.Fatalf("expected no error initially, got %v", st.Err)
|
|
}
|
|
|
|
sentinel := errors.New("test capture failure")
|
|
eng.setErr(sentinel)
|
|
if st := eng.Status(); st.Err != sentinel {
|
|
t.Errorf("expected sentinel error, got %v", st.Err)
|
|
}
|
|
|
|
eng.setErr(nil)
|
|
if st := eng.Status(); st.Err != nil {
|
|
t.Errorf("expected cleared error, got %v", st.Err)
|
|
}
|
|
}
|
|
|
|
// TestValidate rejects out-of-range values.
|
|
func TestValidate(t *testing.T) {
|
|
bad := []func(*Config){
|
|
func(c *Config) { c.Port = 0 },
|
|
func(c *Config) { c.Port = 70000 },
|
|
func(c *Config) { c.Quality = 0 },
|
|
func(c *Config) { c.Quality = 101 },
|
|
func(c *Config) { c.FPS = 0 },
|
|
func(c *Config) { c.StreamIndex = -1 },
|
|
}
|
|
for i, mutate := range bad {
|
|
c := DefaultConfig()
|
|
mutate(&c)
|
|
if err := c.Validate(); err == nil {
|
|
t.Errorf("case %d: expected validation error", i)
|
|
}
|
|
}
|
|
|
|
if err := DefaultConfig().Validate(); err != nil {
|
|
t.Errorf("default config should validate: %v", err)
|
|
}
|
|
}
|
|
|
|
func itoa(v int) string {
|
|
if v == 0 {
|
|
return "0"
|
|
}
|
|
var buf [20]byte
|
|
i := len(buf)
|
|
for v > 0 {
|
|
i--
|
|
buf[i] = byte('0' + v%10)
|
|
v /= 10
|
|
}
|
|
return string(buf[i:])
|
|
}
|
|
|
|
// TestSetConfig verifies live config updates apply FPS/quality and reject
|
|
// invalid values, while preserving fields that can't change live.
|
|
func TestSetConfig(t *testing.T) {
|
|
cfg := DefaultConfig()
|
|
cfg.Source = "pattern"
|
|
cfg.Port = 19758
|
|
|
|
eng, err := New(cfg)
|
|
if err != nil {
|
|
t.Fatalf("New: %v", err)
|
|
}
|
|
|
|
// Update FPS/quality live.
|
|
newCfg := DefaultConfig()
|
|
newCfg.Source = "screen" // must be ignored (requires restart)
|
|
newCfg.Port = 9999 // must be ignored
|
|
newCfg.Quality = 95
|
|
newCfg.FPS = 60
|
|
if err := eng.SetConfig(newCfg); err != nil {
|
|
t.Fatalf("SetConfig: %v", err)
|
|
}
|
|
|
|
got := eng.getCfg()
|
|
if got.Quality != 95 {
|
|
t.Errorf("quality = %d, want 95", got.Quality)
|
|
}
|
|
if got.FPS != 60 {
|
|
t.Errorf("fps = %d, want 60", got.FPS)
|
|
}
|
|
// Source/port preserved.
|
|
if got.Source != "pattern" {
|
|
t.Errorf("source = %q, want pattern (unchanged live)", got.Source)
|
|
}
|
|
if got.Port != 19758 {
|
|
t.Errorf("port = %d, want 19758 (unchanged live)", got.Port)
|
|
}
|
|
|
|
// Invalid config is rejected and current settings kept.
|
|
if err := eng.SetConfig(Config{Quality: 200}); err == nil {
|
|
t.Error("expected error for invalid quality")
|
|
}
|
|
got = eng.getCfg()
|
|
if got.Quality != 95 {
|
|
t.Errorf("quality changed after rejected update: %d", got.Quality)
|
|
}
|
|
}
|