* Fix header size calculation in stats.
With pacer inserting some extensions, the header size used in stats
(and more impoetantly when probing for bandwidth estimation and
metering the bytes to control the probes) was incorrect. The size
was effectively was that of incoming extensions. It would have been
close enough though.
Anyhow, a bit of history
- initially was planning on packaging all the necessary fields into
pacer packet and pacer would callback after sending, but that was not
great for a couple of reasons
- had to send in a bunch of useless data (as far as pacer is
concerned) into pacer.
- callback every packet (this is not bad, just a function call which
happens in the foward path too, but had to lug around the above
data).
- in the forward path, there is a very edge case issue when calling stats update
after pacer.Enqueue() - details in https://github.com/livekit/livekit/pull/2085,
but that is a rare case.
Because of those reasons, the update was placed in the forward path
before enqueue, but did not notice the header size issue till now.
As a compromise, `pacer.Enqueue` returns the headerSize and payloadSize.
It uses a dummy header to calculate size. Real extension will be added
just before sending packet on the wire. pion/rtp replaces extension if
one is already present. So, the dummy would be replaced by the real one
before sending on the wire.
https://github.com/pion/rtp/blob/a21194ecfb5362261a0dc4af1f68e4a8944df345/packet.go#L398
This does introduce back the second rare edge case, but that is very
rare and even if it happens, not catastrophic.
* cleanup
* add extensions and dummy as well in downtrack to make pacer cleaner
86 lines
1.7 KiB
Go
86 lines
1.7 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 pacer
|
|
|
|
import (
|
|
"sync"
|
|
|
|
"github.com/frostbyte73/core"
|
|
"github.com/gammazero/deque"
|
|
"github.com/livekit/livekit-server/pkg/sfu/sendsidebwe"
|
|
"github.com/livekit/protocol/logger"
|
|
)
|
|
|
|
type NoQueue struct {
|
|
*Base
|
|
|
|
logger logger.Logger
|
|
|
|
lock sync.RWMutex
|
|
packets deque.Deque[*Packet]
|
|
wake chan struct{}
|
|
stop core.Fuse
|
|
}
|
|
|
|
func NewNoQueue(logger logger.Logger, sendSideBWE *sendsidebwe.SendSideBWE) *NoQueue {
|
|
n := &NoQueue{
|
|
Base: NewBase(logger, sendSideBWE),
|
|
logger: logger,
|
|
wake: make(chan struct{}, 1),
|
|
}
|
|
n.packets.SetMinCapacity(9)
|
|
|
|
go n.sendWorker()
|
|
return n
|
|
}
|
|
|
|
func (n *NoQueue) Stop() {
|
|
n.stop.Break()
|
|
}
|
|
|
|
func (n *NoQueue) Enqueue(p *Packet) {
|
|
n.lock.Lock()
|
|
n.packets.PushBack(p)
|
|
n.lock.Unlock()
|
|
|
|
select {
|
|
case n.wake <- struct{}{}:
|
|
default:
|
|
}
|
|
}
|
|
|
|
func (n *NoQueue) sendWorker() {
|
|
for {
|
|
<-n.wake
|
|
for {
|
|
if n.stop.IsBroken() {
|
|
return
|
|
}
|
|
|
|
n.lock.Lock()
|
|
if n.packets.Len() == 0 {
|
|
n.lock.Unlock()
|
|
break
|
|
}
|
|
p := n.packets.PopFront()
|
|
n.lock.Unlock()
|
|
|
|
n.Base.SendPacket(p)
|
|
}
|
|
}
|
|
}
|
|
|
|
// ------------------------------------------------
|