H265 supoort and codec regression (#3358)

* H265 supoort and codec regression

Support H265 codec.
Add optional codec regression for subscribers don't
support advanced codecs like H265, AV1, VP9.

* restart forwarder on upstream codec change

* tests

* Reneogitate new codec if client doesn't support change

* Add option to disable codec regression

---------

Co-authored-by: boks1971 <raja.gobi@tutanota.com>
This commit is contained in:
cnderrauber
2025-02-06 11:56:49 +08:00
committed by GitHub
co-authored by boks1971
parent abcaa9b8ab
commit aeec75edeb
27 changed files with 1423 additions and 201 deletions
+37 -1
View File
@@ -38,6 +38,7 @@ type DynacastManager struct {
params DynacastManagerParams
lock sync.RWMutex
regressedCodec map[string]struct{}
dynacastQuality map[string]*DynacastQuality // mime type => DynacastQuality
maxSubscribedQuality map[string]livekit.VideoQuality
committedMaxSubscribedQuality map[string]livekit.VideoQuality
@@ -58,6 +59,7 @@ func NewDynacastManager(params DynacastManagerParams) *DynacastManager {
}
d := &DynacastManager{
params: params,
regressedCodec: make(map[string]struct{}),
dynacastQuality: make(map[string]*DynacastQuality),
maxSubscribedQuality: make(map[string]livekit.VideoQuality),
committedMaxSubscribedQuality: make(map[string]livekit.VideoQuality),
@@ -85,6 +87,38 @@ func (d *DynacastManager) AddCodec(mime string) {
d.getOrCreateDynacastQuality(mime)
}
func (d *DynacastManager) HandleCodecRegression(fromMime, toMime string) {
fromDq := d.getOrCreateDynacastQuality(fromMime)
d.lock.Lock()
if d.isClosed {
d.lock.Unlock()
return
}
normalizedFromMime, normalizedToMime := strings.ToLower(fromMime), strings.ToLower(toMime)
if fromDq == nil {
// should not happen as we have added the codec on setup receiver
d.params.Logger.Warnw("regression from codec not found", nil, "mime", normalizedFromMime)
d.lock.Unlock()
return
}
d.regressedCodec[normalizedFromMime] = struct{}{}
d.maxSubscribedQuality[normalizedFromMime] = livekit.VideoQuality_OFF
// if the new codec is not added, notify the publisher to start publishing
if _, ok := d.maxSubscribedQuality[normalizedToMime]; !ok {
d.maxSubscribedQuality[normalizedToMime] = livekit.VideoQuality_HIGH
}
d.lock.Unlock()
d.update(false)
fromDq.Stop()
ToDq := d.getOrCreateDynacastQuality(normalizedToMime)
fromDq.RegressTo(ToDq)
}
func (d *DynacastManager) Restart() {
d.lock.Lock()
d.committedMaxSubscribedQuality = make(map[string]livekit.VideoQuality)
@@ -183,7 +217,9 @@ func (d *DynacastManager) getDynacastQualitiesLocked() []*DynacastQuality {
func (d *DynacastManager) updateMaxQualityForMime(mime string, maxQuality livekit.VideoQuality) {
d.lock.Lock()
d.maxSubscribedQuality[mime] = maxQuality
if _, ok := d.regressedCodec[mime]; !ok {
d.maxSubscribedQuality[mime] = maxQuality
}
d.lock.Unlock()
d.update(false)