Files
zeekay e9292da479 refactor: own the module path, and move the KV client to hanzokv/go
This fork lived at github.com/hanzoai/livekit but still declared
`module github.com/livekit/livekit-server`, so all 505 self-references pointed
at upstream. A fork that does not own its module path is the exact thing the
owned-module-paths rule exists to prevent: every import line in every consumer
carries someone else's brand, and `go get` resolves to their code, not ours.

  module + 505 self-imports  github.com/livekit/livekit-server -> github.com/hanzoai/livekit
  KV client                  github.com/redis/go-redis/v9      -> github.com/hanzokv/go/v9
  message bus                github.com/livekit/psrpc          -> github.com/hanzoai/psrpc

psrpc had to be forked (hanzoai/psrpc, Apache-2.0) because its bus is typed
against the KV client — the same shape redsync had for hanzoai/git.

DELIBERATELY NOT FORKED: github.com/livekit/{protocol,mediatransportutil,
mageutil} keep their addresses. Only `protocol` still reaches go-redis, through
a single file (redis/redis.go) out of 305. Forking an 11.5MB core wire-protocol
library that this server must track in lockstep — and that upstream pushed
yesterday — to change one file trades a tidy dependency graph for permanent
divergence on the fastest-moving dependency here. So one indirect
github.com/redis/go-redis entry remains, via livekit/protocol/redis, and that
is the correct place for it to live.
2026-07-26 08:14:23 -07:00

140 lines
3.8 KiB
Go

// Copyright 2023 LiveKit, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package videolayerselector
import (
dd "github.com/hanzoai/livekit/pkg/sfu/rtpextension/dependencydescriptor"
"github.com/livekit/protocol/logger"
)
type FrameChain struct {
logger logger.Logger
decisions *SelectorDecisionCache
broken bool
chainIdx int
active bool
updatingActive bool
expectFrames []uint64
}
func NewFrameChain(decisions *SelectorDecisionCache, chainIdx int, logger logger.Logger) *FrameChain {
return &FrameChain{
logger: logger,
decisions: decisions,
broken: true,
chainIdx: chainIdx,
active: false,
}
}
func (fc *FrameChain) OnFrame(extFrameNum uint64, fd *dd.FrameDependencyTemplate) bool {
if !fc.active {
return false
}
if len(fd.ChainDiffs) <= fc.chainIdx {
fc.logger.Warnw("invalid frame chain diff", nil, "chanIdx", fc.chainIdx, "frame", extFrameNum, "fd", fd)
return fc.broken
}
// A decodable frame with frame_chain_fdiff equal to 0 indicates that the Chain is intact.
if fd.ChainDiffs[fc.chainIdx] == 0 {
if fc.broken {
fc.broken = false
// fc.logger.Debugw("frame chain intact", "chanIdx", fc.chainIdx, "frame", extFrameNum)
}
fc.expectFrames = fc.expectFrames[:0]
return true
}
if fc.broken {
return false
}
prevFrameInChain := extFrameNum - uint64(fd.ChainDiffs[fc.chainIdx])
sd, err := fc.decisions.GetDecision(prevFrameInChain)
if err != nil {
fc.logger.Debugw("could not get decision", "err", err, "chanIdx", fc.chainIdx, "frame", extFrameNum, "prevFrame", prevFrameInChain)
}
var intact bool
switch sd {
case selectorDecisionForwarded:
intact = true
case selectorDecisionUnknown:
// If the previous frame is unknown, means it has not arrived but could be recovered by NACK / out-of-order arrival,
// set up a expected callback here to determine if the chain is broken or intact
if fc.decisions.ExpectDecision(prevFrameInChain, fc.OnExpectFrameChanged) {
intact = true
fc.expectFrames = append(fc.expectFrames, prevFrameInChain)
}
}
if !intact {
fc.broken = true
// fc.logger.Debugw("frame chain broken", "chanIdx", fc.chainIdx, "sd", sd, "frame", extFrameNum, "prevFrame", prevFrameInChain)
}
return intact
}
func (fc *FrameChain) OnExpectFrameChanged(frameNum uint64, decision selectorDecision) {
if fc.broken {
return
}
for i, f := range fc.expectFrames {
if f == frameNum {
if decision != selectorDecisionForwarded {
fc.broken = true
// fc.logger.Debugw("frame chain broken", "chanIdx", fc.chainIdx, "sd", decision, "frame", frameNum)
}
fc.expectFrames[i] = fc.expectFrames[len(fc.expectFrames)-1]
fc.expectFrames = fc.expectFrames[:len(fc.expectFrames)-1]
break
}
}
}
func (fc *FrameChain) Broken() bool {
return fc.broken
}
func (fc *FrameChain) BeginUpdateActive() {
fc.updatingActive = false
}
func (fc *FrameChain) UpdateActive(active bool) {
fc.updatingActive = fc.updatingActive || active
}
func (fc *FrameChain) EndUpdateActive() {
active := fc.updatingActive
fc.updatingActive = false
if active == fc.active {
return
}
// if the chain transit from inactive to active, reset broken to wait a decodable SWITCH frame
if !fc.active {
fc.broken = true
fc.logger.Debugw("frame chain broken by inactive", "chanIdx", fc.chainIdx)
}
fc.active = active
}