snapshot of https://github.com/livekit/livekit.git @ 08c5433449c106943231037d0b892895f6e92d3b (2026-09-15)
This commit is contained in:
72
pkg/sfu/rtpextension/playoutdelay/playoutdelay.go
Normal file
72
pkg/sfu/rtpextension/playoutdelay/playoutdelay.go
Normal file
@@ -0,0 +1,72 @@
|
||||
// Copyright 2024 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 playoutdelay
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
)
|
||||
|
||||
const (
|
||||
PlayoutDelayURI = "http://www.webrtc.org/experiments/rtp-hdrext/playout-delay"
|
||||
MaxPlayoutDelayDefault = 10000 // 10s, equal to chrome's default max playout delay
|
||||
PlayoutDelayMaxValue = 10 * (1<<12 - 1) // max value for playout delay can be represented
|
||||
|
||||
playoutDelayExtensionSize = 3
|
||||
)
|
||||
|
||||
var (
|
||||
errPlayoutDelayOverflow = errors.New("playout delay overflow")
|
||||
errTooSmall = errors.New("buffer too small")
|
||||
)
|
||||
|
||||
// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
|
||||
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
// | ID | len=2 | MIN delay | MAX delay |
|
||||
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
// The wired MIN/MAX delay is in 10ms unit
|
||||
|
||||
type PlayOutDelay struct {
|
||||
Min, Max uint16 // delay in ms
|
||||
}
|
||||
|
||||
func PlayoutDelayFromValue(min, max uint16) PlayOutDelay {
|
||||
if min > PlayoutDelayMaxValue {
|
||||
min = PlayoutDelayMaxValue
|
||||
}
|
||||
if max > PlayoutDelayMaxValue {
|
||||
max = PlayoutDelayMaxValue
|
||||
}
|
||||
return PlayOutDelay{Min: min, Max: max}
|
||||
}
|
||||
|
||||
func (p PlayOutDelay) Marshal() ([]byte, error) {
|
||||
min, max := p.Min/10, p.Max/10
|
||||
if min >= 1<<12 || max >= 1<<12 {
|
||||
return nil, errPlayoutDelayOverflow
|
||||
}
|
||||
|
||||
return []byte{byte(min >> 4), byte(min<<4) | byte(max>>8), byte(max)}, nil
|
||||
}
|
||||
|
||||
func (p *PlayOutDelay) Unmarshal(rawData []byte) error {
|
||||
if len(rawData) < playoutDelayExtensionSize {
|
||||
return errTooSmall
|
||||
}
|
||||
|
||||
p.Min = (binary.BigEndian.Uint16(rawData) >> 4) * 10
|
||||
p.Max = (binary.BigEndian.Uint16(rawData[1:]) & 0x0FFF) * 10
|
||||
return nil
|
||||
}
|
||||
57
pkg/sfu/rtpextension/playoutdelay/playoutdelay_test.go
Normal file
57
pkg/sfu/rtpextension/playoutdelay/playoutdelay_test.go
Normal file
@@ -0,0 +1,57 @@
|
||||
// Copyright 2024 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 playoutdelay
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestPlayoutDelay(t *testing.T) {
|
||||
p1 := PlayOutDelay{Min: 100, Max: 200}
|
||||
b, err := p1.Marshal()
|
||||
require.NoError(t, err)
|
||||
require.Len(t, b, playoutDelayExtensionSize)
|
||||
var p2 PlayOutDelay
|
||||
err = p2.Unmarshal(b)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, p1, p2)
|
||||
|
||||
// overflow
|
||||
p3 := PlayOutDelay{Min: 100, Max: (1 << 12) * 10}
|
||||
_, err = p3.Marshal()
|
||||
require.ErrorIs(t, err, errPlayoutDelayOverflow)
|
||||
|
||||
// too small
|
||||
p4 := PlayOutDelay{}
|
||||
err = p4.Unmarshal([]byte{0x00, 0x00})
|
||||
require.ErrorIs(t, err, errTooSmall)
|
||||
|
||||
// from value
|
||||
p5 := PlayoutDelayFromValue(1<<12*10, 1<<12*10+10)
|
||||
_, err = p5.Marshal()
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, uint16((1<<12)-1)*10, p5.Min)
|
||||
require.Equal(t, uint16((1<<12)-1)*10, p5.Max)
|
||||
|
||||
p6 := PlayOutDelay{Min: 100, Max: PlayoutDelayMaxValue}
|
||||
bytes, err := p6.Marshal()
|
||||
require.NoError(t, err)
|
||||
p6Unmarshal := PlayOutDelay{}
|
||||
err = p6Unmarshal.Unmarshal(bytes)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, p6, p6Unmarshal)
|
||||
}
|
||||
Reference in New Issue
Block a user