E2EE trailer for server injected packets. (#1908)

* Ability to use trailer with server injected frames

A 32-byte trailer generated per room.
Trailer appended when track encryption is enabled.

* E2EE trailer for server injected packets.

- Generate a 32-byte per room trailer. Too reasons for longer length
  o Laziness: utils generates a 32 byte string.
  o Longer length random string reduces chances of colliding with real data.
- Trailer sent in JoinResponse
- Trailer added to server injected frames (not to padding only packets)

* generate

* add a length check

* pass trailer in as an argument
This commit is contained in:
Raja Subramanian
2023-07-27 16:50:18 +05:30
committed by GitHub
parent 38c4eba5a3
commit fc7d4bd01e
10 changed files with 257 additions and 9 deletions
+26 -9
View File
@@ -240,6 +240,8 @@ type DownTrack struct {
maxLayerNotifierCh chan struct{}
trailer []byte
cbMu sync.RWMutex
onStatsUpdate func(dt *DownTrack, stat *livekit.AnalyticsStat)
onMaxSubscribedLayerChanged func(dt *DownTrack, layer int32)
@@ -255,6 +257,7 @@ func NewDownTrack(
subID livekit.ParticipantID,
mt int,
pacer pacer.Pacer,
trailer []byte,
logger logger.Logger,
) (*DownTrack, error) {
var kind webrtc.RTPCodecType
@@ -279,6 +282,7 @@ func NewDownTrack(
kind: kind,
codec: codecs[0].RTPCodecCapability,
pacer: pacer,
trailer: trailer,
maxLayerNotifierCh: make(chan struct{}, 20),
}
d.forwarder = NewForwarder(
@@ -1273,19 +1277,30 @@ func (d *DownTrack) writeBlankFrameRTP(duration float32, generation uint32) chan
return done
}
func (d *DownTrack) maybeAddTrailer(buf []byte) int {
if len(buf) < len(d.trailer) {
d.logger.Warnw("trailer too big", nil, "bufLen", len(buf), "trailerLen", len(d.trailer))
return 0
}
copy(buf, d.trailer)
return len(d.trailer)
}
func (d *DownTrack) getOpusBlankFrame(_frameEndNeeded bool) ([]byte, error) {
// silence frame
// Used shortly after muting to ensure residual noise does not keep
// generating noise at the decoder after the stream is stopped
// i. e. comfort noise generation actually not producing something comfortable.
payload := make([]byte, len(OpusSilenceFrame))
payload := make([]byte, 1000)
copy(payload[0:], OpusSilenceFrame)
return payload, nil
trailerLen := d.maybeAddTrailer(payload[len(OpusSilenceFrame):])
return payload[:len(OpusSilenceFrame)+trailerLen], nil
}
func (d *DownTrack) getOpusRedBlankFrame(_frameEndNeeded bool) ([]byte, error) {
// primary only silence frame for opus/red, there is no need to contain redundant silent frames
payload := make([]byte, len(OpusSilenceFrame)+1)
payload := make([]byte, 1000)
// primary header
// 0 1 2 3 4 5 6 7
@@ -1294,7 +1309,8 @@ func (d *DownTrack) getOpusRedBlankFrame(_frameEndNeeded bool) ([]byte, error) {
// +-+-+-+-+-+-+-+-+
payload[0] = opusPT
copy(payload[1:], OpusSilenceFrame)
return payload, nil
trailerLen := d.maybeAddTrailer(payload[1+len(OpusSilenceFrame):])
return payload[:1+len(OpusSilenceFrame)+trailerLen], nil
}
func (d *DownTrack) getVP8BlankFrame(frameEndNeeded bool) ([]byte, error) {
@@ -1307,17 +1323,18 @@ func (d *DownTrack) getVP8BlankFrame(frameEndNeeded bool) ([]byte, error) {
// Used even when closing out a previous frame. Looks like receivers
// do not care about content (it will probably end up being an undecodable
// frame, but that should be okay as there are key frames following)
payload := make([]byte, len(blankVP8)+len(VP8KeyFrame8x8))
payload := make([]byte, 1000)
copy(payload[:len(blankVP8)], blankVP8)
copy(payload[len(blankVP8):], VP8KeyFrame8x8)
return payload, nil
trailerLen := d.maybeAddTrailer(payload[len(blankVP8)+len(VP8KeyFrame8x8):])
return payload[:len(blankVP8)+len(VP8KeyFrame8x8)+trailerLen], nil
}
func (d *DownTrack) getH264BlankFrame(_frameEndNeeded bool) ([]byte, error) {
// TODO - Jie Zeng
// now use STAP-A to compose sps, pps, idr together, most decoder support packetization-mode 1.
// if client only support packetization-mode 0, use single nalu unit packet
buf := make([]byte, 1462)
buf := make([]byte, 1000)
offset := 0
buf[0] = 0x18 // STAP-A
offset++
@@ -1327,8 +1344,8 @@ func (d *DownTrack) getH264BlankFrame(_frameEndNeeded bool) ([]byte, error) {
copy(buf[offset:offset+len(payload)], payload)
offset += len(payload)
}
payload := buf[:offset]
return payload, nil
offset += d.maybeAddTrailer(buf[offset:])
return buf[:offset], nil
}
func (d *DownTrack) handleRTCP(bytes []byte) {