Reject migration if codec mismatch with published tracks (#2225)
* Reject migrated/published track mismatch codec with track info * Check potential codecs * Issue full connect if mismatch * fix codec finding
This commit is contained in:
+11
-2
@@ -239,13 +239,22 @@ func (t *MediaTrack) AddReceiver(receiver *webrtc.RTPReceiver, track *webrtc.Tra
|
|||||||
t.params.Logger.Debugw("AddReceiver", "mime", track.Codec().MimeType)
|
t.params.Logger.Debugw("AddReceiver", "mime", track.Codec().MimeType)
|
||||||
wr := t.MediaTrackReceiver.Receiver(mime)
|
wr := t.MediaTrackReceiver.Receiver(mime)
|
||||||
if wr == nil {
|
if wr == nil {
|
||||||
var priority int
|
priority := -1
|
||||||
for idx, c := range t.params.TrackInfo.Codecs {
|
for idx, c := range t.params.TrackInfo.Codecs {
|
||||||
if strings.HasSuffix(mime, c.MimeType) {
|
if strings.EqualFold(mime, c.MimeType) {
|
||||||
priority = idx
|
priority = idx
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if len(t.params.TrackInfo.Codecs) == 0 {
|
||||||
|
priority = 0
|
||||||
|
}
|
||||||
|
if priority < 0 {
|
||||||
|
t.params.Logger.Warnw("could not find codec for webrtc receiver", nil, "webrtcCodec", mime, "track", logger.Proto(t.params.TrackInfo))
|
||||||
|
t.lock.Unlock()
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
newWR := sfu.NewWebRTCReceiver(
|
newWR := sfu.NewWebRTCReceiver(
|
||||||
receiver,
|
receiver,
|
||||||
track,
|
track,
|
||||||
|
|||||||
+29
-7
@@ -1624,8 +1624,10 @@ func (p *ParticipantImpl) addPendingTrackLocked(req *livekit.AddTrackRequest) *l
|
|||||||
seenCodecs := make(map[string]struct{})
|
seenCodecs := make(map[string]struct{})
|
||||||
for _, codec := range req.SimulcastCodecs {
|
for _, codec := range req.SimulcastCodecs {
|
||||||
mime := codec.Codec
|
mime := codec.Codec
|
||||||
if req.Type == livekit.TrackType_VIDEO && !strings.HasPrefix(mime, "video/") {
|
if req.Type == livekit.TrackType_VIDEO {
|
||||||
mime = "video/" + mime
|
if !strings.HasPrefix(mime, "video/") {
|
||||||
|
mime = "video/" + mime
|
||||||
|
}
|
||||||
if !IsCodecEnabled(p.enabledPublishCodecs, webrtc.RTPCodecCapability{MimeType: mime}) {
|
if !IsCodecEnabled(p.enabledPublishCodecs, webrtc.RTPCodecCapability{MimeType: mime}) {
|
||||||
altCodec := selectAlternativeVideoCodec(p.enabledPublishCodecs)
|
altCodec := selectAlternativeVideoCodec(p.enabledPublishCodecs)
|
||||||
p.pubLogger.Infow("falling back to alternative codec",
|
p.pubLogger.Infow("falling back to alternative codec",
|
||||||
@@ -1762,12 +1764,32 @@ func (p *ParticipantImpl) mediaTrackReceived(track *webrtc.TrackRemote, rtpRecei
|
|||||||
// use existing media track to handle simulcast
|
// use existing media track to handle simulcast
|
||||||
mt, ok := p.getPublishedTrackBySdpCid(track.ID()).(*MediaTrack)
|
mt, ok := p.getPublishedTrackBySdpCid(track.ID()).(*MediaTrack)
|
||||||
if !ok {
|
if !ok {
|
||||||
signalCid, ti := p.getPendingTrack(track.ID(), ToProtoTrackKind(track.Kind()))
|
signalCid, ti, migrated := p.getPendingTrack(track.ID(), ToProtoTrackKind(track.Kind()))
|
||||||
if ti == nil {
|
if ti == nil {
|
||||||
p.pendingTracksLock.Unlock()
|
p.pendingTracksLock.Unlock()
|
||||||
return nil, false
|
return nil, false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// check if the migrated track has correct codec
|
||||||
|
if migrated && len(ti.Codecs) > 0 {
|
||||||
|
parameters := rtpReceiver.GetParameters()
|
||||||
|
var codecFound int
|
||||||
|
for _, c := range ti.Codecs {
|
||||||
|
for _, nc := range parameters.Codecs {
|
||||||
|
if strings.EqualFold(nc.MimeType, c.MimeType) {
|
||||||
|
codecFound++
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if codecFound != len(ti.Codecs) {
|
||||||
|
p.params.Logger.Warnw("migrated track codec mismatched", nil, "track", logger.Proto(ti), "webrtcCodec", parameters)
|
||||||
|
p.pendingTracksLock.Unlock()
|
||||||
|
p.IssueFullReconnect(types.ParticipantCloseReasonMigrateCodecMismatch)
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
ti.MimeType = track.Codec().MimeType
|
ti.MimeType = track.Codec().MimeType
|
||||||
mt = p.addMediaTrack(signalCid, track.ID(), ti)
|
mt = p.addMediaTrack(signalCid, track.ID(), ti)
|
||||||
newTrack = true
|
newTrack = true
|
||||||
@@ -1976,7 +1998,7 @@ func (p *ParticipantImpl) onUpTrackManagerClose() {
|
|||||||
p.postRtcp(nil)
|
p.postRtcp(nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *ParticipantImpl) getPendingTrack(clientId string, kind livekit.TrackType) (string, *livekit.TrackInfo) {
|
func (p *ParticipantImpl) getPendingTrack(clientId string, kind livekit.TrackType) (string, *livekit.TrackInfo, bool) {
|
||||||
signalCid := clientId
|
signalCid := clientId
|
||||||
pendingInfo := p.pendingTracks[clientId]
|
pendingInfo := p.pendingTracks[clientId]
|
||||||
if pendingInfo == nil {
|
if pendingInfo == nil {
|
||||||
@@ -2012,10 +2034,10 @@ func (p *ParticipantImpl) getPendingTrack(clientId string, kind livekit.TrackTyp
|
|||||||
// if still not found, we are done
|
// if still not found, we are done
|
||||||
if pendingInfo == nil {
|
if pendingInfo == nil {
|
||||||
p.pubLogger.Errorw("track info not published prior to track", nil, "clientId", clientId)
|
p.pubLogger.Errorw("track info not published prior to track", nil, "clientId", clientId)
|
||||||
return signalCid, nil
|
return signalCid, nil, false
|
||||||
}
|
}
|
||||||
|
|
||||||
return signalCid, pendingInfo.trackInfos[0]
|
return signalCid, pendingInfo.trackInfos[0], pendingInfo.migrated
|
||||||
}
|
}
|
||||||
|
|
||||||
// setStableTrackID either generates a new TrackID or reuses a previously used one
|
// setStableTrackID either generates a new TrackID or reuses a previously used one
|
||||||
@@ -2206,7 +2228,7 @@ func (p *ParticipantImpl) IssueFullReconnect(reason types.ParticipantCloseReason
|
|||||||
|
|
||||||
scr := types.SignallingCloseReasonUnknown
|
scr := types.SignallingCloseReasonUnknown
|
||||||
switch reason {
|
switch reason {
|
||||||
case types.ParticipantCloseReasonPublicationError:
|
case types.ParticipantCloseReasonPublicationError, types.ParticipantCloseReasonMigrateCodecMismatch:
|
||||||
scr = types.SignallingCloseReasonFullReconnectPublicationError
|
scr = types.SignallingCloseReasonFullReconnectPublicationError
|
||||||
case types.ParticipantCloseReasonSubscriptionError:
|
case types.ParticipantCloseReasonSubscriptionError:
|
||||||
scr = types.SignallingCloseReasonFullReconnectSubscriptionError
|
scr = types.SignallingCloseReasonFullReconnectSubscriptionError
|
||||||
|
|||||||
@@ -284,7 +284,7 @@ func TestMuteSetting(t *testing.T) {
|
|||||||
Muted: true,
|
Muted: true,
|
||||||
})
|
})
|
||||||
|
|
||||||
_, ti := p.getPendingTrack("cid", livekit.TrackType_AUDIO)
|
_, ti, _ := p.getPendingTrack("cid", livekit.TrackType_AUDIO)
|
||||||
require.NotNil(t, ti)
|
require.NotNil(t, ti)
|
||||||
require.True(t, ti.Muted)
|
require.True(t, ti.Muted)
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -46,7 +46,7 @@ func (p *ParticipantImpl) setCodecPreferencesOpusRedForPublisher(offer webrtc.Se
|
|||||||
}
|
}
|
||||||
|
|
||||||
p.pendingTracksLock.RLock()
|
p.pendingTracksLock.RLock()
|
||||||
_, info := p.getPendingTrack(streamID, livekit.TrackType_AUDIO)
|
_, info, _ := p.getPendingTrack(streamID, livekit.TrackType_AUDIO)
|
||||||
// if RED is disabled for this track, don't prefer RED codec in offer
|
// if RED is disabled for this track, don't prefer RED codec in offer
|
||||||
disableRed := info != nil && info.DisableRed
|
disableRed := info != nil && info.DisableRed
|
||||||
p.pendingTracksLock.RUnlock()
|
p.pendingTracksLock.RUnlock()
|
||||||
@@ -132,7 +132,7 @@ func (p *ParticipantImpl) setCodecPreferencesVideoForPublisher(offer webrtc.Sess
|
|||||||
if mt != nil {
|
if mt != nil {
|
||||||
info = mt.ToProto()
|
info = mt.ToProto()
|
||||||
} else {
|
} else {
|
||||||
_, info = p.getPendingTrack(streamID, livekit.TrackType_VIDEO)
|
_, info, _ = p.getPendingTrack(streamID, livekit.TrackType_VIDEO)
|
||||||
}
|
}
|
||||||
|
|
||||||
if info == nil {
|
if info == nil {
|
||||||
@@ -239,7 +239,7 @@ func (p *ParticipantImpl) configurePublisherAnswer(answer webrtc.SessionDescript
|
|||||||
track, _ := p.getPublishedTrackBySdpCid(streamID).(*MediaTrack)
|
track, _ := p.getPublishedTrackBySdpCid(streamID).(*MediaTrack)
|
||||||
if track == nil {
|
if track == nil {
|
||||||
p.pendingTracksLock.RLock()
|
p.pendingTracksLock.RLock()
|
||||||
_, ti = p.getPendingTrack(streamID, livekit.TrackType_AUDIO)
|
_, ti, _ = p.getPendingTrack(streamID, livekit.TrackType_AUDIO)
|
||||||
p.pendingTracksLock.RUnlock()
|
p.pendingTracksLock.RUnlock()
|
||||||
} else {
|
} else {
|
||||||
ti = track.TrackInfo(false)
|
ti = track.TrackInfo(false)
|
||||||
|
|||||||
@@ -104,6 +104,7 @@ const (
|
|||||||
ParticipantCloseReasonPublicationError
|
ParticipantCloseReasonPublicationError
|
||||||
ParticipantCloseReasonSubscriptionError
|
ParticipantCloseReasonSubscriptionError
|
||||||
ParticipantCloseReasonDataChannelError
|
ParticipantCloseReasonDataChannelError
|
||||||
|
ParticipantCloseReasonMigrateCodecMismatch
|
||||||
)
|
)
|
||||||
|
|
||||||
func (p ParticipantCloseReason) String() string {
|
func (p ParticipantCloseReason) String() string {
|
||||||
@@ -154,6 +155,8 @@ func (p ParticipantCloseReason) String() string {
|
|||||||
return "SUBSCRIPTION_ERROR"
|
return "SUBSCRIPTION_ERROR"
|
||||||
case ParticipantCloseReasonDataChannelError:
|
case ParticipantCloseReasonDataChannelError:
|
||||||
return "DATA_CHANNEL_ERROR"
|
return "DATA_CHANNEL_ERROR"
|
||||||
|
case ParticipantCloseReasonMigrateCodecMismatch:
|
||||||
|
return "MIGRATE_CODEC_MISMATCH"
|
||||||
default:
|
default:
|
||||||
return fmt.Sprintf("%d", int(p))
|
return fmt.Sprintf("%d", int(p))
|
||||||
}
|
}
|
||||||
@@ -184,7 +187,7 @@ func (p ParticipantCloseReason) ToDisconnectReason() livekit.DisconnectReason {
|
|||||||
return livekit.DisconnectReason_SERVER_SHUTDOWN
|
return livekit.DisconnectReason_SERVER_SHUTDOWN
|
||||||
case ParticipantCloseReasonOvercommitted:
|
case ParticipantCloseReasonOvercommitted:
|
||||||
return livekit.DisconnectReason_SERVER_SHUTDOWN
|
return livekit.DisconnectReason_SERVER_SHUTDOWN
|
||||||
case ParticipantCloseReasonNegotiateFailed, ParticipantCloseReasonPublicationError, ParticipantCloseReasonSubscriptionError, ParticipantCloseReasonDataChannelError:
|
case ParticipantCloseReasonNegotiateFailed, ParticipantCloseReasonPublicationError, ParticipantCloseReasonSubscriptionError, ParticipantCloseReasonDataChannelError, ParticipantCloseReasonMigrateCodecMismatch:
|
||||||
return livekit.DisconnectReason_STATE_MISMATCH
|
return livekit.DisconnectReason_STATE_MISMATCH
|
||||||
default:
|
default:
|
||||||
// the other types will map to unknown reason
|
// the other types will map to unknown reason
|
||||||
|
|||||||
Reference in New Issue
Block a user