Skip to content

Take control over your live stream video by running it yourself. Streaming + chat out of the box.

fix(metrics): retain segment timing for CMCD event clients

Gabe Kangas19d1d261663d
diff --git a/webserver/handlers/cmcdCollector.go b/webserver/handlers/cmcdCollector.go
index 1d20ad90b4..8267f34488 100644
--- a/webserver/handlers/cmcdCollector.go
+++ b/webserver/handlers/cmcdCollector.go
@@ -38,12 +38,16 @@ func (h *Handlers) ReportCmcd(w http.ResponseWriter, r *http.Request) {
viewerID := utils.GenerateClientIDFromRequest(r)
registered := false
+ reportsDownloadDuration := false
for _, keys := range reports {
if len(keys) == 0 {
continue
}
h.registerCMCDKeys(cmcdClientID(r, keys), viewerID, keys)
registered = true
+ if cmcdReportsDownloadDuration(keys) {
+ reportsDownloadDuration = true
+ }
}
if !registered {
@@ -51,15 +55,20 @@ func (h *Handlers) ReportCmcd(w http.ResponseWriter, r *http.Request) {
return
}
- // A beaconing player's media requests come from the same client, so
- // suppress the lower-fidelity server-derived observation for it even
- // if it doesn't decorate its media requests. Only a request that
- // actually carried a report earns the suppression.
- h.metrics.RegisterSelfReportingClient(viewerID)
+ // Keep server timing for event-only clients. They report playback state
+ // and latency but cannot measure each HLS segment's transfer duration.
+ if reportsDownloadDuration {
+ h.metrics.RegisterSelfReportingClient(viewerID)
+ }
w.WriteHeader(http.StatusOK)
}
+func cmcdReportsDownloadDuration(keys map[string]any) bool {
+ ttlb, ok := cmcdNumber(keys, "ttlb")
+ return ok && ttlb > 0
+}
+
// parseCmcdReports extracts CMCD reports from a collector request: a JSON
// object, a JSON array of objects, a CMCD query parameter, or the four
// CMCD transmission headers.
diff --git a/webserver/handlers/hlsMetrics_test.go b/webserver/handlers/hlsMetrics_test.go
index 9ff103079e..9f510e7878 100644
--- a/webserver/handlers/hlsMetrics_test.go
+++ b/webserver/handlers/hlsMetrics_test.go
@@ -169,6 +169,18 @@ func TestParseCmcdReportsHeaders(t *testing.T) {
}
}
+func TestCMCDReportsDownloadDuration(t *testing.T) {
+ if cmcdReportsDownloadDuration(map[string]any{"e": "t", "ltc": 4200}) {
+ t.Error("event-only CMCD must retain server segment timing")
+ }
+ if cmcdReportsDownloadDuration(map[string]any{"ttlb": 0.0}) {
+ t.Error("zero TTLB is not a download measurement")
+ }
+ if !cmcdReportsDownloadDuration(map[string]any{"ttlb": 250.0}) {
+ t.Error("positive TTLB must replace server segment timing")
+ }
+}
+
func TestSegmentSpeedSample(t *testing.T) {
const oneMB = 1024 * 1024