feat: add bitrate measurement and quality presets
- sender tracks payload bytes; engine measures stream bitrate over a sliding window and exposes it in Status - GUI status label and tray tooltip show the live bitrate (e.g. 36 Mbps) - add a Preset dropdown (Low/Medium/High/Ultra) that sets quality+fps together and applies live via SetConfig Verified: 36 Mbps shown for Medium (quality 70 @ 30fps) matches the wire measurement.
This commit is contained in:
@@ -10,6 +10,7 @@ package flinger
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"image"
|
||||
"image/color"
|
||||
"io"
|
||||
@@ -92,6 +93,9 @@ type Status struct {
|
||||
Frames int64
|
||||
Dropped int64
|
||||
Conns int
|
||||
// Bitrate is the measured stream bandwidth in bits per second, averaged
|
||||
// over the previous measurement window.
|
||||
Bitrate int64
|
||||
// Err is the most recent runtime error encountered (capture, encode,
|
||||
// packet or audio), or nil if the stream is healthy.
|
||||
Err error
|
||||
@@ -111,9 +115,16 @@ type Engine struct {
|
||||
start time.Time
|
||||
stop chan struct{}
|
||||
|
||||
frames atomic.Int64
|
||||
frames atomic.Int64
|
||||
|
||||
errMu sync.RWMutex
|
||||
lastErr error
|
||||
|
||||
// bitrate tracking
|
||||
bitMu sync.Mutex
|
||||
bitLast time.Time
|
||||
bitBytes int64
|
||||
bitrate int64
|
||||
}
|
||||
|
||||
// setErr records the most recent runtime error. Pass nil to clear it.
|
||||
@@ -254,15 +265,48 @@ func (e *Engine) Status() Status {
|
||||
e.errMu.RLock()
|
||||
err := e.lastErr
|
||||
e.errMu.RUnlock()
|
||||
|
||||
return Status{
|
||||
Running: e.stop != nil,
|
||||
Frames: e.frames.Load(),
|
||||
Dropped: e.sender.Dropped(),
|
||||
Conns: e.sender.NumConns(),
|
||||
Bitrate: e.measureBitrate(),
|
||||
Err: err,
|
||||
}
|
||||
}
|
||||
|
||||
// bitrateWindow is the sliding window over which bitrate is averaged.
|
||||
const bitrateWindow = 2 * time.Second
|
||||
|
||||
// measureBitrate computes the current stream bitrate (bits/sec) over a
|
||||
// sliding window. It is called from Status.
|
||||
func (e *Engine) measureBitrate() int64 {
|
||||
e.bitMu.Lock()
|
||||
defer e.bitMu.Unlock()
|
||||
|
||||
now := time.Now()
|
||||
bytes := e.sender.BytesSent()
|
||||
|
||||
if e.bitLast.IsZero() {
|
||||
e.bitLast = now
|
||||
e.bitBytes = bytes
|
||||
return 0
|
||||
}
|
||||
|
||||
elapsed := now.Sub(e.bitLast)
|
||||
if elapsed < bitrateWindow {
|
||||
return e.bitrate
|
||||
}
|
||||
|
||||
// Bytes accumulated since the previous sample.
|
||||
delta := bytes - e.bitBytes
|
||||
e.bitrate = int64(float64(delta*8) / elapsed.Seconds())
|
||||
e.bitLast = now
|
||||
e.bitBytes = bytes
|
||||
return e.bitrate
|
||||
}
|
||||
|
||||
// audioLoop reads raw PCM and emits WAVE packets. start is the shared
|
||||
// reference clock used by the video loop so audio and video timestamps stay
|
||||
// aligned on the receiver.
|
||||
@@ -375,10 +419,11 @@ func (e *Engine) statsLoop() {
|
||||
select {
|
||||
case <-tick.C:
|
||||
st := e.Status()
|
||||
rate := formatBitrate(st.Bitrate)
|
||||
if st.Dropped > 0 {
|
||||
log.Printf("flinger: %d frames, %d dropped, %d conns", st.Frames, st.Dropped, st.Conns)
|
||||
log.Printf("flinger: %d frames, %d dropped, %d conns, %s", st.Frames, st.Dropped, st.Conns, rate)
|
||||
} else {
|
||||
log.Printf("flinger: %d frames, %d conns", st.Frames, st.Conns)
|
||||
log.Printf("flinger: %d frames, %d conns, %s", st.Frames, st.Conns, rate)
|
||||
}
|
||||
case <-e.stop:
|
||||
return
|
||||
@@ -386,6 +431,18 @@ func (e *Engine) statsLoop() {
|
||||
}
|
||||
}
|
||||
|
||||
// formatBitrate renders a bits/sec value in a human-readable form.
|
||||
func formatBitrate(bps int64) string {
|
||||
switch {
|
||||
case bps >= 1_000_000:
|
||||
return fmt.Sprintf("%.1f Mbps", float64(bps)/1_000_000)
|
||||
case bps >= 1_000:
|
||||
return fmt.Sprintf("%.0f kbps", float64(bps)/1_000)
|
||||
default:
|
||||
return fmt.Sprintf("%d bps", bps)
|
||||
}
|
||||
}
|
||||
|
||||
// frameSource abstracts the frame source: real capture or the test pattern.
|
||||
type frameSource interface {
|
||||
Next() (*capture.VideoFrame, error)
|
||||
|
||||
Reference in New Issue
Block a user