Try 2 to consolidate mime type (#3407)
* Normalize mime type and add utilities. An attempt to normalize mime type and avoid string compares remembering to do case insensitive search. Not the best solution. Open to ideas. But, define our own mime types (just in case Pion changes things and Pion also does not have red mime type defined which should be easy to add though) and tried to use it everywhere. But, as we get a bunch of callbacks and info from Pion, needed conversion in more places than I anticipated. And also makes it necessary to carry that cognitive load of what comes from Pion and needing to process it properly. * more locations * test * Paul feedback * MimeType type * more consolidation * Remove unused * test * test * mime type as int * use string method * Pass error details and timeouts. (#3402) * go mod tidy (#3408) * Rename CHANGELOG to CHANGELOG.md (#3391) Enables markdown features in this otherwise already markdown'ish formatted document * Update config.go to properly process bool env vars (#3382) Fixes issue https://github.com/livekit/livekit/issues/3381 * fix(deps): update go deps (#3341) Generated by renovateBot Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * Use a Twirp server hook to send API call details to telemetry. (#3401) * Use a Twirp server hook to send API call details to telemetry. * mage generate and clean up * Add project_id * deps * - Redact requests - Do not store responses - Extract top level fields room_name, room_id, participant_identity, participant_id, track_id as appropriate - Store status as int * deps * Update pkg/sfu/mime/mimetype.go * Fix prefer codec test * handle down track mime changes --------- Co-authored-by: Denys Smirnov <dennwc@pm.me> Co-authored-by: Philzen <Philzen@users.noreply.github.com> Co-authored-by: Pablo Fuente Pérez <pablofuenteperez@gmail.com> Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: Paul Wells <paulwe@gmail.com> Co-authored-by: cnderrauber <zengjie9004@gmail.com>
This commit is contained in:
co-authored by
Denys Smirnov
Philzen
Pablo Fuente Pérez
renovate[bot]
Paul Wells
cnderrauber
parent
045bd785a9
commit
9551c52c85
+29
-43
@@ -32,6 +32,7 @@ import (
|
||||
"go.uber.org/atomic"
|
||||
|
||||
"github.com/livekit/livekit-server/pkg/sfu/audio"
|
||||
"github.com/livekit/livekit-server/pkg/sfu/mime"
|
||||
act "github.com/livekit/livekit-server/pkg/sfu/rtpextension/abscapturetime"
|
||||
dd "github.com/livekit/livekit-server/pkg/sfu/rtpextension/dependencydescriptor"
|
||||
"github.com/livekit/livekit-server/pkg/sfu/rtpstats"
|
||||
@@ -94,7 +95,7 @@ type Buffer struct {
|
||||
rtpParameters webrtc.RTPParameters
|
||||
payloadType uint8
|
||||
rtxPayloadType uint8
|
||||
mime string
|
||||
mime mime.MimeType
|
||||
|
||||
snRangeMap *utils.RangeMap[uint64, uint64]
|
||||
|
||||
@@ -218,10 +219,10 @@ func (b *Buffer) Bind(params webrtc.RTPParameters, codec webrtc.RTPCodecCapabili
|
||||
|
||||
b.clockRate = codec.ClockRate
|
||||
b.lastReport = mono.UnixNano()
|
||||
b.mime = strings.ToLower(codec.MimeType)
|
||||
b.mime = mime.NormalizeMimeType(codec.MimeType)
|
||||
b.rtpParameters = params
|
||||
for _, codecParameter := range params.Codecs {
|
||||
if strings.EqualFold(codecParameter.MimeType, codec.MimeType) {
|
||||
if mime.IsMimeTypeStringEqual(codecParameter.MimeType, codec.MimeType) {
|
||||
b.payloadType = uint8(codecParameter.PayloadType)
|
||||
break
|
||||
}
|
||||
@@ -234,7 +235,7 @@ func (b *Buffer) Bind(params webrtc.RTPParameters, codec webrtc.RTPCodecCapabili
|
||||
|
||||
// find RTX payload type
|
||||
for _, codec := range params.Codecs {
|
||||
if strings.EqualFold(codec.MimeType, "video/rtx") && strings.Contains(codec.SDPFmtpLine, fmt.Sprintf("apt=%d", b.payloadType)) {
|
||||
if mime.IsMimeTypeStringRTX(codec.MimeType) && strings.Contains(codec.SDPFmtpLine, fmt.Sprintf("apt=%d", b.payloadType)) {
|
||||
b.rtxPayloadType = uint8(codec.PayloadType)
|
||||
break
|
||||
}
|
||||
@@ -248,7 +249,7 @@ func (b *Buffer) Bind(params webrtc.RTPParameters, codec webrtc.RTPCodecCapabili
|
||||
continue
|
||||
}
|
||||
b.ddExtID = uint8(ext.ID)
|
||||
b.createDDParserAndFrameRateCalculator(codec.MimeType)
|
||||
b.createDDParserAndFrameRateCalculator()
|
||||
|
||||
case sdp.AudioLevelURI:
|
||||
b.audioLevelExtID = uint8(ext.ID)
|
||||
@@ -260,15 +261,15 @@ func (b *Buffer) Bind(params webrtc.RTPParameters, codec webrtc.RTPCodecCapabili
|
||||
}
|
||||
|
||||
switch {
|
||||
case strings.HasPrefix(b.mime, "audio/"):
|
||||
case mime.IsMimeTypeAudio(b.mime):
|
||||
b.codecType = webrtc.RTPCodecTypeAudio
|
||||
b.bucket = bucket.NewBucket[uint64](InitPacketBufferSizeAudio)
|
||||
|
||||
case strings.HasPrefix(b.mime, "video/"):
|
||||
case mime.IsMimeTypeVideo(b.mime):
|
||||
b.codecType = webrtc.RTPCodecTypeVideo
|
||||
b.bucket = bucket.NewBucket[uint64](InitPacketBufferSizeVideo)
|
||||
if b.frameRateCalculator[0] == nil {
|
||||
b.createFrameRateCalculator(codec.MimeType)
|
||||
b.createFrameRateCalculator()
|
||||
}
|
||||
if bitrates > 0 {
|
||||
pps := bitrates / 8 / 1200
|
||||
@@ -292,7 +293,7 @@ func (b *Buffer) Bind(params webrtc.RTPParameters, codec webrtc.RTPCodecCapabili
|
||||
// pion use a single mediaengine to manage negotiated codecs of peerconnection, that means we can't have different
|
||||
// codec settings at track level for same codec type, so enable nack for all audio receivers but don't create nack queue
|
||||
// for red codec.
|
||||
if strings.EqualFold(b.mime, "audio/red") {
|
||||
if b.mime == mime.MimeTypeRED {
|
||||
break
|
||||
}
|
||||
b.logger.Debugw("Setting feedback", "type", webrtc.TypeRTCPFBNACK)
|
||||
@@ -313,8 +314,8 @@ func (b *Buffer) OnCodecChange(fn func(webrtc.RTPCodecParameters)) {
|
||||
b.Unlock()
|
||||
}
|
||||
|
||||
func (b *Buffer) createDDParserAndFrameRateCalculator(mime string) {
|
||||
if IsSvcCodec(mime) || strings.EqualFold(mime, webrtc.MimeTypeVP8) {
|
||||
func (b *Buffer) createDDParserAndFrameRateCalculator() {
|
||||
if mime.IsMimeTypeSVC(b.mime) || b.mime == mime.MimeTypeVP8 {
|
||||
frc := NewFrameRateCalculatorDD(b.clockRate, b.logger)
|
||||
for i := range b.frameRateCalculator {
|
||||
b.frameRateCalculator[i] = frc.GetFrameRateCalculatorForSpatial(int32(i))
|
||||
@@ -325,18 +326,18 @@ func (b *Buffer) createDDParserAndFrameRateCalculator(mime string) {
|
||||
}
|
||||
}
|
||||
|
||||
func (b *Buffer) createFrameRateCalculator(mime string) {
|
||||
switch {
|
||||
case strings.EqualFold(mime, webrtc.MimeTypeVP8):
|
||||
func (b *Buffer) createFrameRateCalculator() {
|
||||
switch b.mime {
|
||||
case mime.MimeTypeVP8:
|
||||
b.frameRateCalculator[0] = NewFrameRateCalculatorVP8(b.clockRate, b.logger)
|
||||
|
||||
case strings.EqualFold(mime, webrtc.MimeTypeVP9):
|
||||
case mime.MimeTypeVP9:
|
||||
frc := NewFrameRateCalculatorVP9(b.clockRate, b.logger)
|
||||
for i := range b.frameRateCalculator {
|
||||
b.frameRateCalculator[i] = frc.GetFrameRateCalculatorForSpatial(int32(i))
|
||||
}
|
||||
|
||||
case strings.EqualFold(mime, webrtc.MimeTypeH265):
|
||||
case mime.MimeTypeH265:
|
||||
b.frameRateCalculator[0] = NewFrameRateCalculatorH26x(b.clockRate, b.logger)
|
||||
}
|
||||
}
|
||||
@@ -763,7 +764,7 @@ func (b *Buffer) handleCodecChange(newPT uint8) {
|
||||
codecFound = true
|
||||
}
|
||||
|
||||
if strings.EqualFold(codec.MimeType, "video/rtx") && strings.Contains(codec.SDPFmtpLine, fmt.Sprintf("apt=%d", newPT)) {
|
||||
if mime.IsMimeTypeStringRTX(codec.MimeType) && strings.Contains(codec.SDPFmtpLine, fmt.Sprintf("apt=%d", newPT)) {
|
||||
rtxFound = true
|
||||
rtxPt = uint8(codec.PayloadType)
|
||||
}
|
||||
@@ -776,21 +777,22 @@ func (b *Buffer) handleCodecChange(newPT uint8) {
|
||||
b.logger.Errorw("could not find codec for new payload type", nil, "pt", newPT, "rtpParameters", b.rtpParameters)
|
||||
return
|
||||
}
|
||||
b.logger.Infow("codec changed",
|
||||
b.logger.Infow(
|
||||
"codec changed",
|
||||
"oldPayload", b.payloadType, "newPayload", newPT,
|
||||
"oldRtxPayload", b.rtxPayloadType, "newRtxPayload", rtxPt,
|
||||
"oldMime", b.mime, "newMime", newCodec.MimeType)
|
||||
b.payloadType = newPT
|
||||
b.rtxPayloadType = rtxPt
|
||||
b.mime = strings.ToLower(newCodec.MimeType)
|
||||
b.mime = mime.NormalizeMimeType(newCodec.MimeType)
|
||||
b.frameRateCalculated = false
|
||||
|
||||
if b.ddExtID != 0 {
|
||||
b.createDDParserAndFrameRateCalculator(b.mime)
|
||||
b.createDDParserAndFrameRateCalculator()
|
||||
}
|
||||
|
||||
if b.frameRateCalculator[0] == nil {
|
||||
b.createFrameRateCalculator(b.mime)
|
||||
b.createFrameRateCalculator()
|
||||
}
|
||||
|
||||
b.bucket.ResyncOnNextPacket()
|
||||
@@ -874,8 +876,8 @@ func (b *Buffer) getExtPacket(rtpPacket *rtp.Packet, arrivalTime int64, flowStat
|
||||
}
|
||||
}
|
||||
|
||||
switch utils.MatchMimeType(b.mime) {
|
||||
case utils.MimeTypeVP8:
|
||||
switch b.mime {
|
||||
case mime.MimeTypeVP8:
|
||||
vp8Packet := VP8{}
|
||||
if err := vp8Packet.Unmarshal(rtpPacket.Payload); err != nil {
|
||||
b.logger.Warnw("could not unmarshal VP8 packet", err)
|
||||
@@ -891,7 +893,7 @@ func (b *Buffer) getExtPacket(rtpPacket *rtp.Packet, arrivalTime int64, flowStat
|
||||
ep.Payload = vp8Packet
|
||||
ep.Spatial = InvalidLayerSpatial // vp8 don't have spatial scalability, reset to invalid
|
||||
|
||||
case utils.MimeTypeVP9:
|
||||
case mime.MimeTypeVP9:
|
||||
if ep.DependencyDescriptor == nil {
|
||||
var vp9Packet codecs.VP9Packet
|
||||
_, err := vp9Packet.Unmarshal(rtpPacket.Payload)
|
||||
@@ -907,14 +909,14 @@ func (b *Buffer) getExtPacket(rtpPacket *rtp.Packet, arrivalTime int64, flowStat
|
||||
}
|
||||
ep.KeyFrame = IsVP9KeyFrame(rtpPacket.Payload)
|
||||
|
||||
case utils.MimeTypeH264:
|
||||
case mime.MimeTypeH264:
|
||||
ep.KeyFrame = IsH264KeyFrame(rtpPacket.Payload)
|
||||
ep.Spatial = InvalidLayerSpatial // h.264 don't have spatial scalability, reset to invalid
|
||||
|
||||
case utils.MimeTypeAV1:
|
||||
case mime.MimeTypeAV1:
|
||||
ep.KeyFrame = IsAV1KeyFrame(rtpPacket.Payload)
|
||||
|
||||
case utils.MimeTypeH265:
|
||||
case mime.MimeTypeH265:
|
||||
if ep.DependencyDescriptor == nil {
|
||||
if len(rtpPacket.Payload) < 2 {
|
||||
b.logger.Warnw("invalid H265 packet", nil)
|
||||
@@ -1218,19 +1220,3 @@ func (b *Buffer) GetTemporalLayerFpsForSpatial(layer int32) []float32 {
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------
|
||||
|
||||
// SVC-TODO: Have to use more conditions to differentiate between
|
||||
// SVC-TODO: SVC and non-SVC (could be single layer or simulcast).
|
||||
// SVC-TODO: May only need to differentiate between simulcast and non-simulcast
|
||||
// SVC-TODO: i. e. may be possible to treat single layer as SVC to get proper/intended functionality.
|
||||
func IsSvcCodec(mime string) bool {
|
||||
switch utils.MatchMimeType(mime) {
|
||||
case utils.MimeTypeAV1, utils.MimeTypeVP9:
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func IsRedCodec(mime string) bool {
|
||||
return strings.HasSuffix(strings.ToLower(mime), "red")
|
||||
}
|
||||
|
||||
@@ -15,12 +15,10 @@
|
||||
package connectionquality
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/frostbyte73/core"
|
||||
"github.com/pion/webrtc/v4"
|
||||
"go.uber.org/atomic"
|
||||
"google.golang.org/protobuf/types/known/timestamppb"
|
||||
|
||||
@@ -28,6 +26,7 @@ import (
|
||||
"github.com/livekit/protocol/logger"
|
||||
|
||||
"github.com/livekit/livekit-server/pkg/sfu/buffer"
|
||||
"github.com/livekit/livekit-server/pkg/sfu/mime"
|
||||
"github.com/livekit/livekit-server/pkg/sfu/rtpstats"
|
||||
)
|
||||
|
||||
@@ -60,7 +59,7 @@ type ConnectionStatsParams struct {
|
||||
type ConnectionStats struct {
|
||||
params ConnectionStatsParams
|
||||
|
||||
codecMimeType atomic.String
|
||||
codecMimeType atomic.Value // mime.MimeType
|
||||
|
||||
isStarted atomic.Bool
|
||||
isVideo atomic.Bool
|
||||
@@ -88,19 +87,19 @@ func NewConnectionStats(params ConnectionStatsParams) *ConnectionStats {
|
||||
}
|
||||
}
|
||||
|
||||
func (cs *ConnectionStats) StartAt(codecMimeType string, isFECEnabled bool, at time.Time) {
|
||||
func (cs *ConnectionStats) StartAt(codecMimeType mime.MimeType, isFECEnabled bool, at time.Time) {
|
||||
if cs.isStarted.Swap(true) {
|
||||
return
|
||||
}
|
||||
|
||||
cs.isVideo.Store(strings.HasPrefix(strings.ToLower(codecMimeType), "video/"))
|
||||
cs.isVideo.Store(mime.IsMimeTypeVideo(codecMimeType))
|
||||
cs.codecMimeType.Store(codecMimeType)
|
||||
cs.scorer.StartAt(getPacketLossWeight(codecMimeType, isFECEnabled), at)
|
||||
|
||||
go cs.updateStatsWorker()
|
||||
}
|
||||
|
||||
func (cs *ConnectionStats) Start(codecMimeType string, isFECEnabled bool) {
|
||||
func (cs *ConnectionStats) Start(codecMimeType mime.MimeType, isFECEnabled bool) {
|
||||
cs.StartAt(codecMimeType, isFECEnabled, time.Now())
|
||||
}
|
||||
|
||||
@@ -108,6 +107,12 @@ func (cs *ConnectionStats) Close() {
|
||||
cs.done.Break()
|
||||
}
|
||||
|
||||
func (cs *ConnectionStats) UpdateCodec(codecMimeType mime.MimeType, isFECEnabled bool) {
|
||||
cs.isVideo.Store(mime.IsMimeTypeVideo(codecMimeType))
|
||||
cs.codecMimeType.Store(codecMimeType)
|
||||
cs.scorer.UpdatePacketLossWeight(getPacketLossWeight(codecMimeType, isFECEnabled))
|
||||
}
|
||||
|
||||
func (cs *ConnectionStats) OnStatsUpdate(fn func(cs *ConnectionStats, stat *livekit.AnalyticsStat)) {
|
||||
cs.onStatsUpdate = fn
|
||||
}
|
||||
@@ -340,7 +345,7 @@ func (cs *ConnectionStats) getStat() {
|
||||
cs.onStatsUpdate(cs, &livekit.AnalyticsStat{
|
||||
Score: score,
|
||||
Streams: analyticsStreams,
|
||||
Mime: cs.codecMimeType.Load(),
|
||||
Mime: cs.codecMimeType.Load().(mime.MimeType).String(),
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -381,10 +386,10 @@ func (cs *ConnectionStats) updateStatsWorker() {
|
||||
// For video:
|
||||
//
|
||||
// o No in-built codec repair available, hence same for all codecs
|
||||
func getPacketLossWeight(mimeType string, isFecEnabled bool) float64 {
|
||||
func getPacketLossWeight(mimeType mime.MimeType, isFecEnabled bool) float64 {
|
||||
var plw float64
|
||||
switch {
|
||||
case strings.EqualFold(mimeType, webrtc.MimeTypeOpus):
|
||||
case mimeType == mime.MimeTypeOpus:
|
||||
// 2.5%: fall to GOOD, 7.5%: fall to POOR
|
||||
plw = 8.0
|
||||
if isFecEnabled {
|
||||
@@ -392,7 +397,7 @@ func getPacketLossWeight(mimeType string, isFecEnabled bool) float64 {
|
||||
plw /= 1.5
|
||||
}
|
||||
|
||||
case strings.EqualFold(mimeType, "audio/red"):
|
||||
case mimeType == mime.MimeTypeRED:
|
||||
// 5%: fall to GOOD, 15.0%: fall to POOR
|
||||
plw = 4.0
|
||||
if isFecEnabled {
|
||||
@@ -400,7 +405,7 @@ func getPacketLossWeight(mimeType string, isFecEnabled bool) float64 {
|
||||
plw /= 1.5
|
||||
}
|
||||
|
||||
case strings.HasPrefix(strings.ToLower(mimeType), "video/"):
|
||||
case mime.IsMimeTypeVideo(mimeType):
|
||||
// 2%: fall to GOOD, 6%: fall to POOR
|
||||
plw = 10.0
|
||||
}
|
||||
|
||||
@@ -19,10 +19,10 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/pion/webrtc/v4"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/livekit/livekit-server/pkg/sfu/buffer"
|
||||
"github.com/livekit/livekit-server/pkg/sfu/mime"
|
||||
"github.com/livekit/livekit-server/pkg/sfu/rtpstats"
|
||||
"github.com/livekit/protocol/livekit"
|
||||
"github.com/livekit/protocol/logger"
|
||||
@@ -70,7 +70,7 @@ func TestConnectionQuality(t *testing.T) {
|
||||
|
||||
duration := 5 * time.Second
|
||||
now := time.Now()
|
||||
cs.StartAt(webrtc.MimeTypeOpus, false, now.Add(-duration))
|
||||
cs.StartAt(mime.MimeTypeOpus, false, now.Add(-duration))
|
||||
cs.UpdateMuteAt(false, now.Add(-1*time.Second))
|
||||
|
||||
// no data and not enough unmute time should return default state which is EXCELLENT quality
|
||||
@@ -484,7 +484,7 @@ func TestConnectionQuality(t *testing.T) {
|
||||
|
||||
duration := 5 * time.Second
|
||||
now := time.Now()
|
||||
cs.StartAt(webrtc.MimeTypeOpus, false, now.Add(-duration))
|
||||
cs.StartAt(mime.MimeTypeOpus, false, now.Add(-duration))
|
||||
cs.UpdateMuteAt(false, now.Add(-1*time.Second))
|
||||
|
||||
// RTT does not knock quality down because it is dependent and hence not taken into account
|
||||
@@ -517,7 +517,7 @@ func TestConnectionQuality(t *testing.T) {
|
||||
|
||||
duration := 5 * time.Second
|
||||
now := time.Now()
|
||||
cs.StartAt(webrtc.MimeTypeOpus, false, now.Add(-duration))
|
||||
cs.StartAt(mime.MimeTypeOpus, false, now.Add(-duration))
|
||||
cs.UpdateMuteAt(false, now.Add(-1*time.Second))
|
||||
|
||||
// Jitter does not knock quality down because it is dependent and hence not taken into account
|
||||
@@ -548,7 +548,7 @@ func TestConnectionQuality(t *testing.T) {
|
||||
}
|
||||
testCases := []struct {
|
||||
name string
|
||||
mimeType string
|
||||
mimeType mime.MimeType
|
||||
isFECEnabled bool
|
||||
packetsExpected uint32
|
||||
expectedQualities []expectedQuality
|
||||
@@ -557,7 +557,7 @@ func TestConnectionQuality(t *testing.T) {
|
||||
// "audio/opus" - no fec - 0 <= loss < 2.5%: EXCELLENT, 2.5% <= loss < 7.5%: GOOD, >= 7.5%: POOR
|
||||
{
|
||||
name: "audio/opus - no fec",
|
||||
mimeType: "audio/opus",
|
||||
mimeType: mime.MimeTypeOpus,
|
||||
isFECEnabled: false,
|
||||
packetsExpected: 200,
|
||||
expectedQualities: []expectedQuality{
|
||||
@@ -581,7 +581,7 @@ func TestConnectionQuality(t *testing.T) {
|
||||
// "audio/opus" - fec - 0 <= loss < 3.75%: EXCELLENT, 3.75% <= loss < 11.25%: GOOD, >= 11.25%: POOR
|
||||
{
|
||||
name: "audio/opus - fec",
|
||||
mimeType: "audio/opus",
|
||||
mimeType: mime.MimeTypeOpus,
|
||||
isFECEnabled: true,
|
||||
packetsExpected: 200,
|
||||
expectedQualities: []expectedQuality{
|
||||
@@ -605,7 +605,7 @@ func TestConnectionQuality(t *testing.T) {
|
||||
// "audio/red" - no fec - 0 <= loss < 5%: EXCELLENT, 5% <= loss < 15%: GOOD, >= 15%: POOR
|
||||
{
|
||||
name: "audio/red - no fec",
|
||||
mimeType: "audio/red",
|
||||
mimeType: mime.MimeTypeRED,
|
||||
isFECEnabled: false,
|
||||
packetsExpected: 200,
|
||||
expectedQualities: []expectedQuality{
|
||||
@@ -629,7 +629,7 @@ func TestConnectionQuality(t *testing.T) {
|
||||
// "audio/red" - fec - 0 <= loss < 7.5%: EXCELLENT, 7.5% <= loss < 22.5%: GOOD, >= 22.5%: POOR
|
||||
{
|
||||
name: "audio/red - fec",
|
||||
mimeType: "audio/red",
|
||||
mimeType: mime.MimeTypeRED,
|
||||
isFECEnabled: true,
|
||||
packetsExpected: 200,
|
||||
expectedQualities: []expectedQuality{
|
||||
@@ -653,7 +653,7 @@ func TestConnectionQuality(t *testing.T) {
|
||||
// "video/*" - 0 <= loss < 2%: EXCELLENT, 2% <= loss < 6%: GOOD, >= 6%: POOR
|
||||
{
|
||||
name: "video/*",
|
||||
mimeType: "video/vp8",
|
||||
mimeType: mime.MimeTypeVP8,
|
||||
isFECEnabled: false,
|
||||
packetsExpected: 200,
|
||||
expectedQualities: []expectedQuality{
|
||||
@@ -786,7 +786,7 @@ func TestConnectionQuality(t *testing.T) {
|
||||
|
||||
duration := 5 * time.Second
|
||||
now := time.Now()
|
||||
cs.StartAt(webrtc.MimeTypeVP8, false, now)
|
||||
cs.StartAt(mime.MimeTypeVP8, false, now)
|
||||
|
||||
for _, tr := range tc.transitions {
|
||||
cs.AddBitrateTransitionAt(tr.bitrate, now.Add(tr.offset))
|
||||
@@ -878,7 +878,7 @@ func TestConnectionQuality(t *testing.T) {
|
||||
|
||||
duration := 5 * time.Second
|
||||
now := time.Now()
|
||||
cs.StartAt(webrtc.MimeTypeVP8, false, now)
|
||||
cs.StartAt(mime.MimeTypeVP8, false, now)
|
||||
|
||||
for _, tr := range tc.transitions {
|
||||
cs.AddLayerTransitionAt(tr.distance, now.Add(tr.offset))
|
||||
|
||||
@@ -258,6 +258,13 @@ func (q *qualityScorer) Start(packetLossWeight float64) {
|
||||
q.startAtLocked(packetLossWeight, time.Now())
|
||||
}
|
||||
|
||||
func (q *qualityScorer) UpdatePacketLossWeight(packetLossWeight float64) {
|
||||
q.lock.Lock()
|
||||
defer q.lock.Unlock()
|
||||
|
||||
q.packetLossWeight = packetLossWeight
|
||||
}
|
||||
|
||||
func (q *qualityScorer) updateMuteAtLocked(isMuted bool, at time.Time) {
|
||||
if isMuted {
|
||||
q.mutedAt = at
|
||||
|
||||
+32
-18
@@ -40,6 +40,7 @@ import (
|
||||
"github.com/livekit/livekit-server/pkg/sfu/buffer"
|
||||
"github.com/livekit/livekit-server/pkg/sfu/ccutils"
|
||||
"github.com/livekit/livekit-server/pkg/sfu/connectionquality"
|
||||
"github.com/livekit/livekit-server/pkg/sfu/mime"
|
||||
"github.com/livekit/livekit-server/pkg/sfu/pacer"
|
||||
act "github.com/livekit/livekit-server/pkg/sfu/rtpextension/abscapturetime"
|
||||
dd "github.com/livekit/livekit-server/pkg/sfu/rtpextension/dependencydescriptor"
|
||||
@@ -253,6 +254,7 @@ type DownTrack struct {
|
||||
params DowntrackParams
|
||||
id livekit.TrackID
|
||||
kind webrtc.RTPCodecType
|
||||
mime mime.MimeType
|
||||
ssrc uint32
|
||||
ssrcRTX uint32
|
||||
payloadType atomic.Uint32
|
||||
@@ -345,11 +347,12 @@ type DownTrack struct {
|
||||
// NewDownTrack returns a DownTrack.
|
||||
func NewDownTrack(params DowntrackParams) (*DownTrack, error) {
|
||||
codecs := params.Codecs
|
||||
mimeType := mime.NormalizeMimeType(codecs[0].MimeType)
|
||||
var kind webrtc.RTPCodecType
|
||||
switch {
|
||||
case strings.HasPrefix(codecs[0].MimeType, "audio/"):
|
||||
case mime.IsMimeTypeAudio(mimeType):
|
||||
kind = webrtc.RTPCodecTypeAudio
|
||||
case strings.HasPrefix(codecs[0].MimeType, "video/"):
|
||||
case mime.IsMimeTypeVideo(mimeType):
|
||||
kind = webrtc.RTPCodecTypeVideo
|
||||
default:
|
||||
kind = webrtc.RTPCodecType(0)
|
||||
@@ -505,13 +508,13 @@ func (d *DownTrack) Bind(t webrtc.TrackLocalContext) (webrtc.RTPCodecParameters,
|
||||
}
|
||||
|
||||
isFECEnabled := false
|
||||
if strings.EqualFold(matchedUpstreamCodec.MimeType, MimeTypeAudioRed) {
|
||||
if mime.IsMimeTypeStringRED(matchedUpstreamCodec.MimeType) {
|
||||
d.isRED = true
|
||||
for _, c := range d.upstreamCodecs {
|
||||
isFECEnabled = strings.Contains(strings.ToLower(c.SDPFmtpLine), "useinbandfec=1")
|
||||
|
||||
// assume upstream primary codec is opus since we only support it for audio now
|
||||
if strings.EqualFold(c.MimeType, webrtc.MimeTypeOpus) {
|
||||
if mime.IsMimeTypeStringOpus(c.MimeType) {
|
||||
d.upstreamPrimaryPT = uint8(c.PayloadType)
|
||||
break
|
||||
}
|
||||
@@ -525,7 +528,7 @@ func (d *DownTrack) Bind(t webrtc.TrackLocalContext) (webrtc.RTPCodecParameters,
|
||||
d.params.Logger.Errorw("failed to parse primary and secondary payload type for RED", err, "matchedCodec", codec)
|
||||
}
|
||||
d.primaryPT = uint8(primaryPT)
|
||||
} else if strings.HasPrefix(strings.ToLower(matchedUpstreamCodec.MimeType), "audio/") {
|
||||
} else if mime.IsMimeTypeStringAudio(matchedUpstreamCodec.MimeType) {
|
||||
isFECEnabled = strings.Contains(strings.ToLower(matchedUpstreamCodec.SDPFmtpLine), "fec")
|
||||
}
|
||||
|
||||
@@ -558,6 +561,7 @@ func (d *DownTrack) Bind(t webrtc.TrackLocalContext) (webrtc.RTPCodecParameters,
|
||||
d.params.Logger.Debugw("DownTrack.Bind", logFields...)
|
||||
|
||||
d.writeStream = t.WriteStream()
|
||||
d.mime = mime.NormalizeMimeType(codec.MimeType)
|
||||
if rr := d.params.BufferFactory.GetOrNew(packetio.RTCPBufferPacket, d.ssrc).(*buffer.RTCPReader); rr != nil {
|
||||
rr.OnPacket(func(pkt []byte) {
|
||||
d.handleRTCP(pkt)
|
||||
@@ -580,10 +584,11 @@ func (d *DownTrack) Bind(t webrtc.TrackLocalContext) (webrtc.RTPCodecParameters,
|
||||
d.onBinding(nil)
|
||||
}
|
||||
d.setBindStateLocked(bindStateBound)
|
||||
mimeType := d.mime
|
||||
d.bindLock.Unlock()
|
||||
|
||||
d.forwarder.DetermineCodec(codec.RTPCodecCapability, d.Receiver().HeaderExtensions())
|
||||
d.connectionStats.Start(codec.MimeType, isFECEnabled)
|
||||
d.connectionStats.Start(mimeType, isFECEnabled)
|
||||
d.params.Logger.Debugw("downtrack bound")
|
||||
}
|
||||
|
||||
@@ -635,9 +640,9 @@ func (d *DownTrack) handleReceiverReady() {
|
||||
}
|
||||
}
|
||||
|
||||
func (d *DownTrack) handleUpstreamCodecChange(mime string) {
|
||||
func (d *DownTrack) handleUpstreamCodecChange(mimeType string) {
|
||||
d.bindLock.Lock()
|
||||
if strings.EqualFold(d.codec.MimeType, mime) {
|
||||
if mime.IsMimeTypeStringEqual(d.codec.MimeType, mimeType) {
|
||||
d.bindLock.Unlock()
|
||||
return
|
||||
}
|
||||
@@ -653,7 +658,7 @@ func (d *DownTrack) handleUpstreamCodecChange(mime string) {
|
||||
|
||||
var codec webrtc.RTPCodecParameters
|
||||
for _, c := range d.upstreamCodecs {
|
||||
if !strings.EqualFold(c.MimeType, mime) {
|
||||
if !mime.IsMimeTypeStringEqual(d.codec.MimeType, mimeType) {
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -670,7 +675,7 @@ func (d *DownTrack) handleUpstreamCodecChange(mime string) {
|
||||
"can't find matched codec for new upstream payload type", nil,
|
||||
"upstreamCodecs", d.upstreamCodecs,
|
||||
"remoteParameters", d.negotiatedCodecParameters,
|
||||
"mime", mime,
|
||||
"mime", mimeType,
|
||||
)
|
||||
d.bindLock.Unlock()
|
||||
return
|
||||
@@ -679,6 +684,9 @@ func (d *DownTrack) handleUpstreamCodecChange(mime string) {
|
||||
d.payloadType.Store(uint32(codec.PayloadType))
|
||||
d.payloadTypeRTX.Store(uint32(utils.FindRTXPayloadType(codec.PayloadType, d.negotiatedCodecParameters)))
|
||||
d.codec = codec.RTPCodecCapability
|
||||
d.mime = mime.NormalizeMimeType(codec.MimeType)
|
||||
newMimeType := d.mime
|
||||
isFECEnabled := strings.Contains(strings.ToLower(d.codec.SDPFmtpLine), "fec")
|
||||
d.bindLock.Unlock()
|
||||
|
||||
d.params.Logger.Infow(
|
||||
@@ -690,6 +698,7 @@ func (d *DownTrack) handleUpstreamCodecChange(mime string) {
|
||||
|
||||
d.forwarder.Restart()
|
||||
d.forwarder.DetermineCodec(codec.RTPCodecCapability, d.Receiver().HeaderExtensions())
|
||||
d.connectionStats.UpdateCodec(newMimeType, isFECEnabled)
|
||||
}
|
||||
|
||||
// Unbind implements the teardown logic when the track is no longer needed. This happens
|
||||
@@ -744,6 +753,12 @@ func (d *DownTrack) Codec() webrtc.RTPCodecCapability {
|
||||
return d.codec
|
||||
}
|
||||
|
||||
func (d *DownTrack) Mime() mime.MimeType {
|
||||
d.bindLock.Lock()
|
||||
defer d.bindLock.Unlock()
|
||||
return d.mime
|
||||
}
|
||||
|
||||
// StreamID is the group this track belongs too. This must be unique
|
||||
func (d *DownTrack) StreamID() string { return d.params.StreamID }
|
||||
|
||||
@@ -1679,16 +1694,15 @@ func (d *DownTrack) writeBlankFrameRTP(duration float32, generation uint32) chan
|
||||
return
|
||||
}
|
||||
|
||||
mime := strings.ToLower(d.Codec().MimeType)
|
||||
var getBlankFrame func(bool) ([]byte, error)
|
||||
switch {
|
||||
case strings.EqualFold(mime, webrtc.MimeTypeOpus):
|
||||
switch d.mime {
|
||||
case mime.MimeTypeOpus:
|
||||
getBlankFrame = d.getOpusBlankFrame
|
||||
case strings.EqualFold(mime, MimeTypeAudioRed):
|
||||
case mime.MimeTypeRED:
|
||||
getBlankFrame = d.getOpusRedBlankFrame
|
||||
case strings.EqualFold(mime, webrtc.MimeTypeVP8):
|
||||
case mime.MimeTypeVP8:
|
||||
getBlankFrame = d.getVP8BlankFrame
|
||||
case strings.EqualFold(mime, webrtc.MimeTypeH264):
|
||||
case mime.MimeTypeH264:
|
||||
getBlankFrame = d.getH264BlankFrame
|
||||
default:
|
||||
close(done)
|
||||
@@ -1696,7 +1710,7 @@ func (d *DownTrack) writeBlankFrameRTP(duration float32, generation uint32) chan
|
||||
}
|
||||
|
||||
frameRate := uint32(30)
|
||||
if mime == strings.ToLower(webrtc.MimeTypeOpus) || mime == strings.ToLower(MimeTypeAudioRed) {
|
||||
if d.mime == mime.MimeTypeOpus || d.mime == mime.MimeTypeRED {
|
||||
frameRate = 50
|
||||
}
|
||||
|
||||
@@ -2414,7 +2428,7 @@ func (d *DownTrack) sendPaddingOnMute() {
|
||||
|
||||
if d.kind == webrtc.RTPCodecTypeVideo {
|
||||
d.sendPaddingOnMuteForVideo()
|
||||
} else if strings.EqualFold(d.Codec().MimeType, webrtc.MimeTypeOpus) {
|
||||
} else if d.mime == mime.MimeTypeOpus {
|
||||
d.sendSilentFrameOnMuteForOpus()
|
||||
}
|
||||
}
|
||||
|
||||
+21
-19
@@ -19,7 +19,6 @@ import (
|
||||
"fmt"
|
||||
"math"
|
||||
"math/rand"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@@ -35,6 +34,7 @@ import (
|
||||
|
||||
"github.com/livekit/livekit-server/pkg/sfu/buffer"
|
||||
"github.com/livekit/livekit-server/pkg/sfu/codecmunger"
|
||||
"github.com/livekit/livekit-server/pkg/sfu/mime"
|
||||
dd "github.com/livekit/livekit-server/pkg/sfu/rtpextension/dependencydescriptor"
|
||||
"github.com/livekit/livekit-server/pkg/sfu/rtpstats"
|
||||
"github.com/livekit/livekit-server/pkg/sfu/videolayerselector"
|
||||
@@ -210,7 +210,8 @@ func (r refInfo) MarshalLogObject(e zapcore.ObjectEncoder) error {
|
||||
|
||||
type Forwarder struct {
|
||||
lock sync.RWMutex
|
||||
codec webrtc.RTPCodecCapability
|
||||
mime mime.MimeType
|
||||
clockRate uint32
|
||||
kind webrtc.RTPCodecType
|
||||
logger logger.Logger
|
||||
skipReferenceTS bool
|
||||
@@ -249,6 +250,7 @@ func NewForwarder(
|
||||
rtpStats *rtpstats.RTPStatsSender,
|
||||
) *Forwarder {
|
||||
f := &Forwarder{
|
||||
mime: mime.MimeTypeUnknown,
|
||||
kind: kind,
|
||||
logger: logger,
|
||||
skipReferenceTS: skipReferenceTS,
|
||||
@@ -299,11 +301,13 @@ func (f *Forwarder) DetermineCodec(codec webrtc.RTPCodecCapability, extensions [
|
||||
f.lock.Lock()
|
||||
defer f.lock.Unlock()
|
||||
|
||||
codecChanged := f.codec.MimeType != "" && f.codec.MimeType != codec.MimeType
|
||||
toMimeType := mime.NormalizeMimeType(codec.MimeType)
|
||||
codecChanged := f.mime != mime.MimeTypeUnknown && f.mime != toMimeType
|
||||
if codecChanged {
|
||||
f.logger.Debugw("forwarder codec changed", "from", f.codec.MimeType, "to", codec.MimeType)
|
||||
f.logger.Debugw("forwarder codec changed", "from", f.mime, "to", toMimeType)
|
||||
}
|
||||
f.codec = codec
|
||||
f.mime = toMimeType
|
||||
f.clockRate = codec.ClockRate
|
||||
|
||||
ddAvailable := func(exts []webrtc.RTPHeaderExtensionParameter) bool {
|
||||
for _, ext := range exts {
|
||||
@@ -314,8 +318,8 @@ func (f *Forwarder) DetermineCodec(codec webrtc.RTPCodecCapability, extensions [
|
||||
return false
|
||||
}
|
||||
|
||||
switch strings.ToLower(codec.MimeType) {
|
||||
case "video/vp8":
|
||||
switch f.mime {
|
||||
case mime.MimeTypeVP8:
|
||||
f.codecMunger = codecmunger.NewVP8FromNull(f.codecMunger, f.logger)
|
||||
if f.vls != nil {
|
||||
if vls := videolayerselector.NewSimulcastFromOther(f.vls); vls != nil {
|
||||
@@ -328,16 +332,14 @@ func (f *Forwarder) DetermineCodec(codec webrtc.RTPCodecCapability, extensions [
|
||||
}
|
||||
f.vls.SetTemporalLayerSelector(temporallayerselector.NewVP8(f.logger))
|
||||
|
||||
case "video/h264":
|
||||
fallthrough
|
||||
case "video/h265":
|
||||
case mime.MimeTypeH264, mime.MimeTypeH265:
|
||||
if f.vls != nil {
|
||||
f.vls = videolayerselector.NewSimulcastFromOther(f.vls)
|
||||
} else {
|
||||
f.vls = videolayerselector.NewSimulcast(f.logger)
|
||||
}
|
||||
|
||||
case "video/vp9":
|
||||
case mime.MimeTypeVP9:
|
||||
// DD-TODO : we only enable dd layer selector for av1/vp9 now, in the future we can enable it for vp8 too
|
||||
isDDAvailable := ddAvailable(extensions)
|
||||
if isDDAvailable {
|
||||
@@ -355,7 +357,7 @@ func (f *Forwarder) DetermineCodec(codec webrtc.RTPCodecCapability, extensions [
|
||||
}
|
||||
// SVC-TODO: Support for VP9 simulcast. When DD is not available, have to pick selector based on VP9 SVC or Simulcast
|
||||
|
||||
case "video/av1":
|
||||
case mime.MimeTypeAV1:
|
||||
// DD-TODO : we only enable dd layer selector for av1/vp9 now, in the future we can enable it for vp8 too
|
||||
isDDAvailable := ddAvailable(extensions)
|
||||
if isDDAvailable {
|
||||
@@ -1513,7 +1515,7 @@ func (f *Forwarder) Pause(availableLayers []int32, brs Bitrates) VideoAllocation
|
||||
|
||||
func (f *Forwarder) updateAllocation(alloc VideoAllocation, reason string) VideoAllocation {
|
||||
// restrict target temporal to 0 if codec does not support temporal layers
|
||||
if alloc.TargetLayer.IsValid() && strings.ToLower(f.codec.MimeType) == "video/h264" {
|
||||
if alloc.TargetLayer.IsValid() && f.mime == mime.MimeTypeH264 {
|
||||
alloc.TargetLayer.Temporal = 0
|
||||
}
|
||||
|
||||
@@ -1663,7 +1665,7 @@ func (f *Forwarder) getRefLayerRTPTimestamp(ts uint32, refLayer, targetLayer int
|
||||
}
|
||||
|
||||
ntpDiff := mediatransportutil.NtpTime(srRef.NtpTimestamp).Time().Sub(mediatransportutil.NtpTime(srTarget.NtpTimestamp).Time())
|
||||
rtpDiff := ntpDiff.Nanoseconds() * int64(f.codec.ClockRate) / 1e9
|
||||
rtpDiff := ntpDiff.Nanoseconds() * int64(f.clockRate) / 1e9
|
||||
|
||||
// calculate other layer's time stamp at the same time as ref layer's NTP time
|
||||
normalizedOtherTS := srTarget.RtpTimestamp + uint32(rtpDiff)
|
||||
@@ -1795,7 +1797,7 @@ func (f *Forwarder) processSourceSwitch(extPkt *buffer.ExtPacket, layer int32) e
|
||||
} else {
|
||||
if !f.preStartTime.IsZero() {
|
||||
timeSinceFirst := time.Since(f.preStartTime)
|
||||
rtpDiff := uint64(timeSinceFirst.Nanoseconds() * int64(f.codec.ClockRate) / 1e9)
|
||||
rtpDiff := uint64(timeSinceFirst.Nanoseconds() * int64(f.clockRate) / 1e9)
|
||||
extExpectedTS = f.extFirstTS + rtpDiff
|
||||
if f.dummyStartTSOffset == 0 {
|
||||
f.dummyStartTSOffset = extExpectedTS - uint64(refTS)
|
||||
@@ -1839,7 +1841,7 @@ func (f *Forwarder) processSourceSwitch(extPkt *buffer.ExtPacket, layer int32) e
|
||||
// Ideally, extRefTS should not be ahead of extExpectedTS, but extExpectedTS uses the first packet's
|
||||
// wall clock time. So, if the first packet experienced abmormal latency, it is possible
|
||||
// for extRefTS > extExpectedTS
|
||||
diffSeconds := float64(int64(extExpectedTS-extRefTS)) / float64(f.codec.ClockRate)
|
||||
diffSeconds := float64(int64(extExpectedTS-extRefTS)) / float64(f.clockRate)
|
||||
if diffSeconds >= 0.0 {
|
||||
if f.resumeBehindThreshold > 0 && diffSeconds > f.resumeBehindThreshold {
|
||||
logTransitionInfo("resume, reference too far behind", extExpectedTS, extRefTS, extLastTS, diffSeconds)
|
||||
@@ -1862,7 +1864,7 @@ func (f *Forwarder) processSourceSwitch(extPkt *buffer.ExtPacket, layer int32) e
|
||||
f.resumeBehindThreshold = 0.0
|
||||
} else {
|
||||
// switching between layers, check if extRefTS is too far behind the last sent
|
||||
diffSeconds := float64(int64(extRefTS-extLastTS)) / float64(f.codec.ClockRate)
|
||||
diffSeconds := float64(int64(extRefTS-extLastTS)) / float64(f.clockRate)
|
||||
if diffSeconds < 0.0 {
|
||||
if math.Abs(diffSeconds) > LayerSwitchBehindThresholdSeconds {
|
||||
// this could be due to pacer trickling out this layer. Error out and wait for a more opportune time.
|
||||
@@ -1877,7 +1879,7 @@ func (f *Forwarder) processSourceSwitch(extPkt *buffer.ExtPacket, layer int32) e
|
||||
logTransition("layer switch, reference is slightly behind", extExpectedTS, extRefTS, extLastTS, diffSeconds)
|
||||
extNextTS = extLastTS + 1
|
||||
} else {
|
||||
diffSeconds = float64(int64(extRefTS-extExpectedTS)) / float64(f.codec.ClockRate)
|
||||
diffSeconds = float64(int64(extRefTS-extExpectedTS)) / float64(f.clockRate)
|
||||
if diffSeconds > SwitchAheadThresholdSeconds {
|
||||
logTransition("layer switch, reference too far ahead", extExpectedTS, extRefTS, extLastTS, diffSeconds)
|
||||
}
|
||||
@@ -2154,7 +2156,7 @@ func (f *Forwarder) GetSnTsForBlankFrames(frameRate uint32, numPackets int) ([]S
|
||||
if int64(extExpectedTS-extLastTS) <= 0 {
|
||||
extExpectedTS = extLastTS + 1
|
||||
}
|
||||
snts, err := f.rtpMunger.UpdateAndGetPaddingSnTs(numPackets, f.codec.ClockRate, frameRate, frameEndNeeded, extExpectedTS)
|
||||
snts, err := f.rtpMunger.UpdateAndGetPaddingSnTs(numPackets, f.clockRate, frameRate, frameEndNeeded, extExpectedTS)
|
||||
return snts, frameEndNeeded, err
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,271 @@
|
||||
// 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 mime
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/pion/webrtc/v4"
|
||||
)
|
||||
|
||||
const (
|
||||
MimeTypePrefixAudio = "audio/"
|
||||
MimeTypePrefixVideo = "video/"
|
||||
)
|
||||
|
||||
type MimeTypeCodec int
|
||||
|
||||
const (
|
||||
MimeTypeCodecUnknown MimeTypeCodec = iota
|
||||
MimeTypeCodecH264
|
||||
MimeTypeCodecH265
|
||||
MimeTypeCodecOpus
|
||||
MimeTypeCodecRED
|
||||
MimeTypeCodecVP8
|
||||
MimeTypeCodecVP9
|
||||
MimeTypeCodecAV1
|
||||
MimeTypeCodecG722
|
||||
MimeTypeCodecPCMU
|
||||
MimeTypeCodecPCMA
|
||||
MimeTypeCodecRTX
|
||||
MimeTypeCodecFlexFEC
|
||||
)
|
||||
|
||||
func (m MimeTypeCodec) String() string {
|
||||
switch m {
|
||||
case MimeTypeCodecUnknown:
|
||||
return "MimeTypeCodecUnknown"
|
||||
case MimeTypeCodecH264:
|
||||
return "H264"
|
||||
case MimeTypeCodecH265:
|
||||
return "H265"
|
||||
case MimeTypeCodecOpus:
|
||||
return "opus"
|
||||
case MimeTypeCodecRED:
|
||||
return "red"
|
||||
case MimeTypeCodecVP8:
|
||||
return "VP8"
|
||||
case MimeTypeCodecVP9:
|
||||
return "VP9"
|
||||
case MimeTypeCodecAV1:
|
||||
return "AV1"
|
||||
case MimeTypeCodecG722:
|
||||
return "G722"
|
||||
case MimeTypeCodecPCMU:
|
||||
return "PCMU"
|
||||
case MimeTypeCodecPCMA:
|
||||
return "PCMA"
|
||||
case MimeTypeCodecRTX:
|
||||
return "rtx"
|
||||
case MimeTypeCodecFlexFEC:
|
||||
return "flexfec"
|
||||
}
|
||||
|
||||
return "MimeTypeCodecUnknown"
|
||||
}
|
||||
|
||||
func NormalizeMimeTypeCodec(codec string) MimeTypeCodec {
|
||||
switch {
|
||||
case strings.EqualFold(codec, "h264"):
|
||||
return MimeTypeCodecH264
|
||||
case strings.EqualFold(codec, "h265"):
|
||||
return MimeTypeCodecH265
|
||||
case strings.EqualFold(codec, "opus"):
|
||||
return MimeTypeCodecOpus
|
||||
case strings.EqualFold(codec, "red"):
|
||||
return MimeTypeCodecRED
|
||||
case strings.EqualFold(codec, "vp8"):
|
||||
return MimeTypeCodecVP8
|
||||
case strings.EqualFold(codec, "vp9"):
|
||||
return MimeTypeCodecVP9
|
||||
case strings.EqualFold(codec, "av1"):
|
||||
return MimeTypeCodecAV1
|
||||
case strings.EqualFold(codec, "g722"):
|
||||
return MimeTypeCodecG722
|
||||
case strings.EqualFold(codec, "pcmu"):
|
||||
return MimeTypeCodecPCMU
|
||||
case strings.EqualFold(codec, "pcma"):
|
||||
return MimeTypeCodecPCMA
|
||||
case strings.EqualFold(codec, "rtx"):
|
||||
return MimeTypeCodecRTX
|
||||
case strings.EqualFold(codec, "flexfec"):
|
||||
return MimeTypeCodecFlexFEC
|
||||
}
|
||||
|
||||
return MimeTypeCodecUnknown
|
||||
}
|
||||
|
||||
func GetMimeTypeCodec(mime string) MimeTypeCodec {
|
||||
i := strings.IndexByte(mime, '/')
|
||||
if i == -1 {
|
||||
return MimeTypeCodecUnknown
|
||||
}
|
||||
|
||||
return NormalizeMimeTypeCodec(mime[i+1:])
|
||||
}
|
||||
|
||||
func IsMimeTypeCodecStringOpus(codec string) bool {
|
||||
return NormalizeMimeTypeCodec(codec) == MimeTypeCodecOpus
|
||||
}
|
||||
|
||||
func IsMimeTypeCodecStringRED(codec string) bool {
|
||||
return NormalizeMimeTypeCodec(codec) == MimeTypeCodecRED
|
||||
}
|
||||
|
||||
func IsMimeTypeCodecStringH264(codec string) bool {
|
||||
return NormalizeMimeTypeCodec(codec) == MimeTypeCodecH264
|
||||
}
|
||||
|
||||
type MimeType int
|
||||
|
||||
const (
|
||||
MimeTypeUnknown MimeType = iota
|
||||
MimeTypeH264
|
||||
MimeTypeH265
|
||||
MimeTypeOpus
|
||||
MimeTypeRED
|
||||
MimeTypeVP8
|
||||
MimeTypeVP9
|
||||
MimeTypeAV1
|
||||
MimeTypeG722
|
||||
MimeTypePCMU
|
||||
MimeTypePCMA
|
||||
MimeTypeRTX
|
||||
MimeTypeFlexFEC
|
||||
)
|
||||
|
||||
func (m MimeType) String() string {
|
||||
switch m {
|
||||
case MimeTypeUnknown:
|
||||
return "MimeTypeUnknown"
|
||||
case MimeTypeH264:
|
||||
return webrtc.MimeTypeH264
|
||||
case MimeTypeH265:
|
||||
return webrtc.MimeTypeH265
|
||||
case MimeTypeOpus:
|
||||
return webrtc.MimeTypeOpus
|
||||
case MimeTypeRED:
|
||||
return "audio/red"
|
||||
case MimeTypeVP8:
|
||||
return webrtc.MimeTypeVP8
|
||||
case MimeTypeVP9:
|
||||
return webrtc.MimeTypeVP9
|
||||
case MimeTypeAV1:
|
||||
return webrtc.MimeTypeAV1
|
||||
case MimeTypeG722:
|
||||
return webrtc.MimeTypeG722
|
||||
case MimeTypePCMU:
|
||||
return webrtc.MimeTypePCMU
|
||||
case MimeTypePCMA:
|
||||
return webrtc.MimeTypePCMA
|
||||
case MimeTypeRTX:
|
||||
return webrtc.MimeTypeRTX
|
||||
case MimeTypeFlexFEC:
|
||||
return webrtc.MimeTypeFlexFEC
|
||||
}
|
||||
|
||||
return "MimeTypeUnknown"
|
||||
}
|
||||
|
||||
func NormalizeMimeType(mime string) MimeType {
|
||||
switch {
|
||||
case strings.EqualFold(mime, webrtc.MimeTypeH264):
|
||||
return MimeTypeH264
|
||||
case strings.EqualFold(mime, webrtc.MimeTypeH265):
|
||||
return MimeTypeH265
|
||||
case strings.EqualFold(mime, webrtc.MimeTypeOpus):
|
||||
return MimeTypeOpus
|
||||
case strings.EqualFold(mime, "audio/red"):
|
||||
return MimeTypeRED
|
||||
case strings.EqualFold(mime, webrtc.MimeTypeVP8):
|
||||
return MimeTypeVP8
|
||||
case strings.EqualFold(mime, webrtc.MimeTypeVP9):
|
||||
return MimeTypeVP9
|
||||
case strings.EqualFold(mime, webrtc.MimeTypeAV1):
|
||||
return MimeTypeAV1
|
||||
case strings.EqualFold(mime, webrtc.MimeTypeG722):
|
||||
return MimeTypeG722
|
||||
case strings.EqualFold(mime, webrtc.MimeTypePCMU):
|
||||
return MimeTypePCMU
|
||||
case strings.EqualFold(mime, webrtc.MimeTypePCMA):
|
||||
return MimeTypePCMA
|
||||
case strings.EqualFold(mime, webrtc.MimeTypeRTX):
|
||||
return MimeTypeRTX
|
||||
case strings.EqualFold(mime, webrtc.MimeTypeFlexFEC):
|
||||
return MimeTypeFlexFEC
|
||||
}
|
||||
|
||||
return MimeTypeUnknown
|
||||
}
|
||||
|
||||
func IsMimeTypeStringEqual(mime1 string, mime2 string) bool {
|
||||
return NormalizeMimeType(mime1) == NormalizeMimeType(mime2)
|
||||
}
|
||||
|
||||
func IsMimeTypeStringAudio(mime string) bool {
|
||||
return strings.HasPrefix(mime, MimeTypePrefixAudio)
|
||||
}
|
||||
|
||||
func IsMimeTypeAudio(mimeType MimeType) bool {
|
||||
return strings.HasPrefix(mimeType.String(), MimeTypePrefixAudio)
|
||||
}
|
||||
|
||||
func IsMimeTypeStringVideo(mime string) bool {
|
||||
return strings.HasPrefix(mime, MimeTypePrefixVideo)
|
||||
}
|
||||
|
||||
func IsMimeTypeVideo(mimeType MimeType) bool {
|
||||
return strings.HasPrefix(mimeType.String(), MimeTypePrefixVideo)
|
||||
}
|
||||
|
||||
// SVC-TODO: Have to use more conditions to differentiate between
|
||||
// SVC-TODO: SVC and non-SVC (could be single layer or simulcast).
|
||||
// SVC-TODO: May only need to differentiate between simulcast and non-simulcast
|
||||
// SVC-TODO: i. e. may be possible to treat single layer as SVC to get proper/intended functionality.
|
||||
func IsMimeTypeSVC(mimeType MimeType) bool {
|
||||
switch mimeType {
|
||||
case MimeTypeAV1, MimeTypeVP9:
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func IsMimeTypeStringSVC(mime string) bool {
|
||||
return IsMimeTypeSVC(NormalizeMimeType(mime))
|
||||
}
|
||||
|
||||
func IsMimeTypeStringRED(mime string) bool {
|
||||
return NormalizeMimeType(mime) == MimeTypeRED
|
||||
}
|
||||
|
||||
func IsMimeTypeStringOpus(mime string) bool {
|
||||
return NormalizeMimeType(mime) == MimeTypeOpus
|
||||
}
|
||||
|
||||
func IsMimeTypeStringRTX(mime string) bool {
|
||||
return NormalizeMimeType(mime) == MimeTypeRTX
|
||||
}
|
||||
|
||||
func IsMimeTypeStringVP8(mime string) bool {
|
||||
return NormalizeMimeType(mime) == MimeTypeVP8
|
||||
}
|
||||
|
||||
func IsMimeTypeStringVP9(mime string) bool {
|
||||
return NormalizeMimeType(mime) == MimeTypeVP9
|
||||
}
|
||||
|
||||
func IsMimeTypeStringH264(mime string) bool {
|
||||
return NormalizeMimeType(mime) == MimeTypeH264
|
||||
}
|
||||
+10
-4
@@ -33,6 +33,7 @@ import (
|
||||
"github.com/livekit/livekit-server/pkg/sfu/audio"
|
||||
"github.com/livekit/livekit-server/pkg/sfu/buffer"
|
||||
"github.com/livekit/livekit-server/pkg/sfu/connectionquality"
|
||||
"github.com/livekit/livekit-server/pkg/sfu/mime"
|
||||
dd "github.com/livekit/livekit-server/pkg/sfu/rtpextension/dependencydescriptor"
|
||||
"github.com/livekit/livekit-server/pkg/sfu/rtpstats"
|
||||
"github.com/livekit/livekit-server/pkg/sfu/streamtracker"
|
||||
@@ -100,6 +101,7 @@ type TrackReceiver interface {
|
||||
// returns the initial codec of the receiver, it is determined by the track's codec
|
||||
// and will not change if the codec changes during the session (publisher changes codec)
|
||||
Codec() webrtc.RTPCodecParameters
|
||||
Mime() mime.MimeType
|
||||
HeaderExtensions() []webrtc.RTPHeaderExtensionParameter
|
||||
IsClosed() bool
|
||||
|
||||
@@ -254,8 +256,8 @@ func NewWebRTCReceiver(
|
||||
codecState: ReceiverCodecStateNormal,
|
||||
kind: track.Kind(),
|
||||
onRTCP: onRTCP,
|
||||
isSVC: buffer.IsSvcCodec(track.Codec().MimeType),
|
||||
isRED: buffer.IsRedCodec(track.Codec().MimeType),
|
||||
isSVC: mime.IsMimeTypeStringSVC(track.Codec().MimeType),
|
||||
isRED: mime.IsMimeTypeStringRED(track.Codec().MimeType),
|
||||
}
|
||||
|
||||
for _, opt := range opts {
|
||||
@@ -278,9 +280,9 @@ func NewWebRTCReceiver(
|
||||
}
|
||||
})
|
||||
w.connectionStats.Start(
|
||||
w.codec.MimeType,
|
||||
mime.NormalizeMimeType(w.codec.MimeType),
|
||||
// TODO: technically not correct to declare FEC on when RED. Need the primary codec's fmtp line to check.
|
||||
strings.EqualFold(w.codec.MimeType, MimeTypeAudioRed) || strings.Contains(strings.ToLower(w.codec.SDPFmtpLine), "useinbandfec=1"),
|
||||
mime.IsMimeTypeStringRED(w.codec.MimeType) || strings.Contains(strings.ToLower(w.codec.SDPFmtpLine), "useinbandfec=1"),
|
||||
)
|
||||
|
||||
w.streamTrackerManager = NewStreamTrackerManager(logger, trackInfo, w.isSVC, w.codec.ClockRate, streamTrackerManagerConfig)
|
||||
@@ -371,6 +373,10 @@ func (w *WebRTCReceiver) Codec() webrtc.RTPCodecParameters {
|
||||
return w.codec
|
||||
}
|
||||
|
||||
func (w *WebRTCReceiver) Mime() mime.MimeType {
|
||||
return mime.NormalizeMimeType(w.codec.MimeType)
|
||||
}
|
||||
|
||||
func (w *WebRTCReceiver) HeaderExtensions() []webrtc.RTPHeaderExtensionParameter {
|
||||
return w.receiver.GetParameters().HeaderExtensions
|
||||
}
|
||||
|
||||
@@ -27,10 +27,6 @@ import (
|
||||
"github.com/livekit/protocol/logger"
|
||||
)
|
||||
|
||||
const (
|
||||
MimeTypeAudioRed = "audio/red"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrIncompleteRedHeader = errors.New("incomplete red block header")
|
||||
ErrIncompleteRedBlock = errors.New("incomplete red block payload")
|
||||
|
||||
@@ -17,8 +17,8 @@ package utils
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/livekit/livekit-server/pkg/sfu/mime"
|
||||
"github.com/pion/interceptor"
|
||||
"github.com/pion/rtp"
|
||||
"github.com/pion/webrtc/v4"
|
||||
@@ -29,7 +29,7 @@ import (
|
||||
func CodecParametersFuzzySearch(needle webrtc.RTPCodecParameters, haystack []webrtc.RTPCodecParameters) (webrtc.RTPCodecParameters, error) {
|
||||
// First attempt to match on MimeType + SDPFmtpLine
|
||||
for _, c := range haystack {
|
||||
if strings.EqualFold(c.RTPCodecCapability.MimeType, needle.RTPCodecCapability.MimeType) &&
|
||||
if mime.IsMimeTypeStringEqual(c.RTPCodecCapability.MimeType, needle.RTPCodecCapability.MimeType) &&
|
||||
c.RTPCodecCapability.SDPFmtpLine == needle.RTPCodecCapability.SDPFmtpLine {
|
||||
return c, nil
|
||||
}
|
||||
@@ -37,7 +37,7 @@ func CodecParametersFuzzySearch(needle webrtc.RTPCodecParameters, haystack []web
|
||||
|
||||
// Fallback to just MimeType
|
||||
for _, c := range haystack {
|
||||
if strings.EqualFold(c.RTPCodecCapability.MimeType, needle.RTPCodecCapability.MimeType) {
|
||||
if mime.IsMimeTypeStringEqual(c.RTPCodecCapability.MimeType, needle.RTPCodecCapability.MimeType) {
|
||||
return c, nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,105 +0,0 @@
|
||||
// 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 utils
|
||||
|
||||
type MimeType int
|
||||
|
||||
const (
|
||||
MimeTypeUnknown MimeType = iota
|
||||
MimeTypeVP8
|
||||
MimeTypeVP9
|
||||
MimeTypeH264
|
||||
MimeTypeAV1
|
||||
MimeTypeH265
|
||||
)
|
||||
|
||||
func MatchMimeType(mimeType string) MimeType {
|
||||
switch len(mimeType) {
|
||||
case 9:
|
||||
switch mimeType[0] {
|
||||
case 'v', 'V':
|
||||
switch mimeType[1] {
|
||||
case 'i', 'I':
|
||||
switch mimeType[2] {
|
||||
case 'd', 'D':
|
||||
switch mimeType[3] {
|
||||
case 'e', 'E':
|
||||
switch mimeType[4] {
|
||||
case 'o', 'O':
|
||||
switch mimeType[5] {
|
||||
case '/':
|
||||
switch mimeType[6] {
|
||||
case 'v', 'V':
|
||||
switch mimeType[7] {
|
||||
case 'p', 'P':
|
||||
switch mimeType[8] {
|
||||
case '8':
|
||||
return MimeTypeVP8
|
||||
case '9':
|
||||
return MimeTypeVP9
|
||||
}
|
||||
}
|
||||
case 'a', 'A':
|
||||
switch mimeType[7] {
|
||||
case 'v', 'V':
|
||||
switch mimeType[8] {
|
||||
case '1':
|
||||
return MimeTypeAV1
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
case 10:
|
||||
switch mimeType[0] {
|
||||
case 'v', 'V':
|
||||
switch mimeType[1] {
|
||||
case 'i', 'I':
|
||||
switch mimeType[2] {
|
||||
case 'd', 'D':
|
||||
switch mimeType[3] {
|
||||
case 'e', 'E':
|
||||
switch mimeType[4] {
|
||||
case 'o', 'O':
|
||||
switch mimeType[5] {
|
||||
case '/':
|
||||
switch mimeType[6] {
|
||||
case 'h', 'H':
|
||||
switch mimeType[7] {
|
||||
case '2':
|
||||
switch mimeType[8] {
|
||||
case '6':
|
||||
switch mimeType[9] {
|
||||
case '4':
|
||||
return MimeTypeH264
|
||||
case '5':
|
||||
return MimeTypeH265
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return MimeTypeUnknown
|
||||
}
|
||||
@@ -1,65 +0,0 @@
|
||||
// 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 utils
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func toLowerSwitch(mimeType string) MimeType {
|
||||
switch strings.ToLower(mimeType) {
|
||||
case "video/vp8":
|
||||
return MimeTypeVP8
|
||||
case "video/vp9":
|
||||
return MimeTypeVP9
|
||||
case "video/h264":
|
||||
return MimeTypeH264
|
||||
case "video/av1":
|
||||
return MimeTypeAV1
|
||||
default:
|
||||
return MimeTypeUnknown
|
||||
}
|
||||
}
|
||||
|
||||
func TestMimeTypeMatch(t *testing.T) {
|
||||
require.Equal(t, MimeTypeVP8, MatchMimeType("VIDEO/VP8"), "VIDEO/VP8")
|
||||
require.Equal(t, MimeTypeVP9, MatchMimeType("VIDEO/VP9"), "VIDEO/VP9")
|
||||
require.Equal(t, MimeTypeH264, MatchMimeType("VIDEO/H264"), "VIDEO/H264")
|
||||
require.Equal(t, MimeTypeAV1, MatchMimeType("VIDEO/AV1"), "VIDEO/AV1")
|
||||
}
|
||||
|
||||
func BenchmarkMimeTypeMatch(b *testing.B) {
|
||||
mimeTypes := []string{
|
||||
"video/VP8",
|
||||
"video/VP9",
|
||||
"video/H264",
|
||||
"video/AV1",
|
||||
}
|
||||
|
||||
b.Run("ToLower/switch", func(b *testing.B) {
|
||||
for i := range b.N {
|
||||
_ = toLowerSwitch(mimeTypes[i%len(mimeTypes)])
|
||||
}
|
||||
})
|
||||
|
||||
b.Run("MatchMimeType", func(b *testing.B) {
|
||||
for i := range b.N {
|
||||
_ = MatchMimeType(mimeTypes[i%len(mimeTypes)])
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user