-
Notifications
You must be signed in to change notification settings - Fork 9
/
clienttrackred.go
109 lines (89 loc) · 2.89 KB
/
clienttrackred.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package sfu
import (
"encoding/binary"
"errors"
"github.com/pion/rtp"
"github.com/pion/webrtc/v4"
)
var (
ErrIncompleteRedHeader = errors.New("util: incomplete RED header")
ErrIncompleteRedBlock = errors.New("util: incomplete RED block")
)
type clientTrackRed struct {
*clientTrackAudio
}
func newClientTrackRed(t *clientTrackAudio) *clientTrackRed {
ct := &clientTrackRed{
clientTrackAudio: t,
}
return ct
}
func (t *clientTrackRed) push(p *rtp.Packet, _ QualityLevel) {
if t.client.peerConnection.PC().ConnectionState() != webrtc.PeerConnectionStateConnected {
return
}
if !t.client.receiveRED {
primaryPacket := t.remoteTrack.rtppool.GetPacket()
primaryPacket.Payload = t.getPrimaryEncoding(p.Payload[:len(p.Payload)])
primaryPacket.Header = p.Header
if err := t.localTrack.WriteRTP(primaryPacket); err != nil {
t.client.log.Tracef("clienttrack: error on write primary rtp %s", err.Error())
}
t.remoteTrack.rtppool.PutPacket(primaryPacket)
} else {
if err := t.localTrack.WriteRTP(p); err != nil {
t.client.log.Tracef("clienttrack: error on write rtp %s", err.Error())
}
}
}
func (t *clientTrackRed) getPrimaryEncoding(payload []byte) []byte {
primaryPayload, err := extractPrimaryEncodingForRED(payload)
if err != nil {
t.client.log.Tracef("clienttrack: error on extract primary encoding for red %s", err.Error())
return payload
}
return primaryPayload
}
// // Credit to Livekit
// // https://github.com/livekit/livekit/blob/56dd39968408f0973374e5b336a28606a1da79d2/pkg/sfu/redprimaryreceiver.go#L267
func extractPrimaryEncodingForRED(payload []byte) ([]byte, error) {
/* RED payload https://datatracker.ietf.org/doc/html/rfc2198#section-3
0 1 2 3
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
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|F| block PT | timestamp offset | block length |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
F: 1 bit First bit in header indicates whether another header block
follows. If 1 further header blocks follow, if 0 this is the
last header block.
*/
var blockLength int
for {
if len(payload) < 1 {
// illegal data, need at least one byte for primary encoding
return nil, ErrIncompleteRedHeader
}
if payload[0]&0x80 == 0 {
// last block is primary encoding data
payload = payload[1:]
break
} else {
if len(payload) < 4 {
// illegal data
return nil, ErrIncompleteRedHeader
}
blockLength += int(binary.BigEndian.Uint16(payload[2:]) & 0x03FF)
payload = payload[4:]
}
}
if len(payload) < blockLength {
return nil, ErrIncompleteRedBlock
}
return payload[blockLength:], nil
}
func (t *clientTrackRed) Quality() QualityLevel {
return QualityAudioRed
}
func (t *clientTrackRed) MaxQuality() QualityLevel {
return QualityAudioRed
}