Skip to content

Commit 0b84558

Browse files
Quick104claude
andcommitted
Merge branch 'feat/1321-stream-bitrate-policy' into feat/1323-local-stream-bitrate-policy
Bring in #1322's fix that lets start and replan try other versions when the bitrate limit refuses one. Keep this branch's local and remote fields where earlier carried-forward commits overlap, and combine the playback docs paragraph so it covers both locations and the version fallback. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
2 parents 086d21e + 14c924f commit 0b84558

6 files changed

Lines changed: 92 additions & 8 deletions

File tree

‎docs/playback-api.md‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,12 @@ When an administrator sets a positive bitrate limit for the stream's location
9090
bitrate fits. Otherwise the server plans
9191
a lower-bitrate transcode, including an audio and mux-overhead budget. A
9292
client's lower bandwidth preference still wins. If no compliant encode route
93-
exists, the decision is terminal with `bitrate_policy_unavailable`; it never
94-
falls back to the oversized original. The selected limit is frozen on a new
95-
playback attempt and reused through replans, so later policy edits do not
96-
interrupt it. `server_remote_stream_bitrate_policy_v1` and
93+
exists, the server tries the item's other versions as it does for 4K and HDR
94+
refusals. When none fits, the decision is terminal with
95+
`bitrate_policy_unavailable`; it never falls back to the oversized original.
96+
The selected limit is frozen on a new playback attempt and reused through
97+
replans, so later policy edits do not interrupt it.
98+
`server_remote_stream_bitrate_policy_v1` and
9799
`server_local_stream_bitrate_policy_v1` in `features` advertise this behavior.
98100

99101
The web Watch Party player requires this capability and sends `false`. When a

‎internal/api/handlers/playback_v3.go‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5686,6 +5686,7 @@ const (
56865686
terminalHDRTranscodeUnsupportedV3 = playback.TerminalHDRTranscodeUnsupportedV3
56875687
terminalSubtitleConversionUnsupportedV3 = "subtitle_conversion_unsupported"
56885688
terminalSubtitleUnavailableInVersionV3 = "subtitle_unavailable_in_version"
5689+
terminalBitratePolicyUnavailableV3 = playback.TerminalBitratePolicyUnavailableV3
56895690
)
56905691

56915692
// terminalAllowsAlternateFileV3 reports whether a refusal is the kind another
@@ -5699,12 +5700,16 @@ const (
56995700
// the exact reason this gate exists — a bitmap subtitle needing burn-in that an
57005701
// HDR source cannot support while an SDR alternate can. Leaving the new reason
57015702
// out silently retired that fallback and refused playback outright.
5703+
//
5704+
// bitrate_policy_unavailable belongs here for the same reason: it replaces the
5705+
// 4K and HDR refusals of a version that exceeds the remote bitrate limit, and
5706+
// a lower-bitrate version may fit that limit.
57025707
func terminalAllowsAlternateFileV3(terminal *playback.TerminalV3) bool {
57035708
if terminal == nil {
57045709
return false
57055710
}
57065711
switch terminal.Reason {
5707-
case terminalNoAlternateVersionV3, terminalHDRTranscodeUnsupportedV3, terminalSubtitleConversionUnsupportedV3:
5712+
case terminalNoAlternateVersionV3, terminalHDRTranscodeUnsupportedV3, terminalSubtitleConversionUnsupportedV3, terminalBitratePolicyUnavailableV3:
57085713
return true
57095714
default:
57105715
return false
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package handlers
2+
3+
import (
4+
"encoding/json"
5+
"net/http"
6+
"net/http/httptest"
7+
"strings"
8+
"testing"
9+
10+
"github.com/Silo-Server/silo-server/internal/access"
11+
"github.com/Silo-Server/silo-server/internal/clientip"
12+
"github.com/Silo-Server/silo-server/internal/models"
13+
"github.com/Silo-Server/silo-server/internal/playback"
14+
)
15+
16+
// A remote bitrate cap that the requested 4K version cannot meet must still
17+
// let the start fall back to a version that fits the cap.
18+
func TestHandleStartPlaybackV3RemoteBitrateCapTriesCompliantAlternate(t *testing.T) {
19+
source := v3HandlerFixtureFile(t)
20+
source.Resolution = "2160p"
21+
source.Bitrate = 32_000
22+
source.VideoTracks[0].Width = 3840
23+
source.VideoTracks[0].Height = 2160
24+
source.VideoTracks[0].Level = 51
25+
source.VideoTracks[0].Bitrate = 32_000
26+
alternateValue := *source
27+
alternate := &alternateValue
28+
alternate.ID = 84
29+
alternate.Resolution = "1080p"
30+
alternate.Bitrate = 8_000
31+
alternate.VideoTracks = append([]models.VideoTrack(nil), source.VideoTracks...)
32+
alternate.VideoTracks[0].Width = 1920
33+
alternate.VideoTracks[0].Height = 1080
34+
alternate.VideoTracks[0].Level = 41
35+
alternate.VideoTracks[0].Bitrate = 7_800
36+
37+
handler := NewPlaybackHandler(playback.NewSessionManager(0, 0), testPlaybackFileResolver{file: source})
38+
handler.FileVersionFetcher = testPlaybackFileVersionFetcher{byContent: map[string][]*models.MediaFile{source.ContentID: {source, alternate}}}
39+
handler.SettingsRepo = &mutablePlaybackSettingsV3{values: map[string]string{"allow_4k_transcode": "false"}}
40+
handler.PlaybackConfig = playbackTestConfig("", "")
41+
handler.ItemAccess = allowAllPlaybackItemAccess{}
42+
43+
start := v3HandlerStartRequest()
44+
start.QualityPreference = "auto"
45+
start.ClientPlaybackContext.Deliveries[playback.DeliveryClassHLSV3] = playback.DeliveryCapabilityV3{Enabled: true, SupportedOnDevice: true}
46+
ctx := access.SetScope(newAuthorizedPlaybackContext(), access.Scope{UserID: 1, ProfileID: "profile-1", MaxRemoteStreamBitrateKbps: 10_000})
47+
ctx = clientip.SetContext(ctx, "203.0.113.7")
48+
rr := httptest.NewRecorder()
49+
handler.HandleStartPlayback(rr, httptest.NewRequest(http.MethodPost, "/api/v1/playback/start", strings.NewReader(marshalV3StartRequest(t, start))).WithContext(ctx))
50+
51+
var response playback.DecisionResponseV3
52+
if rr.Code != http.StatusCreated || json.Unmarshal(rr.Body.Bytes(), &response) != nil {
53+
t.Fatalf("status=%d body=%s", rr.Code, rr.Body.String())
54+
}
55+
if response.PlaybackPlan == nil || response.PlaybackPlan.EffectiveMediaFileID != alternate.ID {
56+
t.Fatalf("expected the compliant 1080p alternate, got terminal=%#v plan=%#v", response.Terminal, response.PlaybackPlan)
57+
}
58+
}

‎internal/playback/plan_v3.go‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,10 @@ func PlanPlaybackV3(input PlannerInputV3) (result PlannerResultV3) {
211211
compliant := result.Plan != nil &&
212212
(result.PlayMethod == PlayTranscode || file.IsAudioOnly() && result.TranscodeAudio)
213213
if result.Terminal != nil || !compliant {
214-
result = terminalPlannerResultV3("bitrate_policy_unavailable", "This stream exceeds the server bitrate limit, and no compliant transcoding route is available.", false)
214+
// Keep a transient blocker (such as a missing toolchain)
215+
// retryable after naming the bitrate limit as the refusal.
216+
retryable := result.Terminal != nil && result.Terminal.Retryable
217+
result = terminalPlannerResultV3(TerminalBitratePolicyUnavailableV3, "This stream exceeds the server bitrate limit, and no compliant transcoding route is available.", retryable)
215218
}
216219
}()
217220
}
@@ -856,7 +859,7 @@ func planAudioOnlyV3(input PlannerInputV3, file *models.MediaFile, source Source
856859
if input.ServerBitrateCapKbps > 0 {
857860
targetAudioBitrateKbps = min(targetAudioBitrateKbps, input.ServerBitrateCapKbps*95/100)
858861
if targetAudioBitrateKbps < 32 {
859-
return terminalPlannerResultV3("bitrate_policy_unavailable", "The server bitrate limit is too low for a playable audio stream.", false)
862+
return terminalPlannerResultV3(TerminalBitratePolicyUnavailableV3, "The server bitrate limit is too low for a playable audio stream.", false)
860863
}
861864
}
862865
applyAudioOnlyAACConversionV3(&plan, targetAudioChannels, targetAudioBitrateKbps, bandwidthCapExceeded)
@@ -1020,7 +1023,7 @@ func planVideoTranscodeV3(input PlannerInputV3, base PlanV3, source SourceDescri
10201023
budget := optionalValueV3(input.Request.BandwidthCapKbps) * 95 / 100
10211024
targetAudioBitrateKbps = min(defaultAudioBitrateKbps, max(64, budget/4))
10221025
if budget-targetAudioBitrateKbps < 64 {
1023-
return terminalPlannerResultV3("bitrate_policy_unavailable", "The server bitrate limit is too low for a playable video stream.", false)
1026+
return terminalPlannerResultV3(TerminalBitratePolicyUnavailableV3, "The server bitrate limit is too low for a playable video stream.", false)
10241027
}
10251028
quality.BitrateKbps = min(quality.BitrateKbps, budget-targetAudioBitrateKbps)
10261029
}

‎internal/playback/protocol_v3.go‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,10 @@ const (
257257
TerminalDVConversionUnsupportedV3 = "dv_conversion_unsupported"
258258
)
259259

260+
// TerminalBitratePolicyUnavailableV3 reports that no route fits the
261+
// administrator's remote per-stream bitrate limit for this version.
262+
const TerminalBitratePolicyUnavailableV3 = "bitrate_policy_unavailable"
263+
260264
type SubtitleModeV3 string
261265

262266
const (

‎internal/playback/stream_bitrate_policy_test.go‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,15 @@ func TestServerBitrateCapTooLowFailsClosed(t *testing.T) {
128128
t.Fatalf("expected bitrate policy terminal: %s", ExplainPlannerResultV3(result))
129129
}
130130
}
131+
132+
func TestServerBitrateCapKeepsTransientBlockerRetryable(t *testing.T) {
133+
input := bitratePolicyFixtureV3()
134+
input.ServerBitrateCapKbps = 4_000
135+
input.HLSVideoRegistry = func() *TransformationRegistryV3 {
136+
return NewTransformationRegistryV3(nil)
137+
}
138+
result := PlanPlaybackV3(input)
139+
if result.Terminal == nil || result.Terminal.Reason != TerminalBitratePolicyUnavailableV3 || !result.Terminal.Retryable {
140+
t.Fatalf("expected retryable bitrate policy terminal: %s", ExplainPlannerResultV3(result))
141+
}
142+
}

0 commit comments

Comments
 (0)