|
| 1 | +package nack |
| 2 | + |
| 3 | +import ( |
| 4 | + "math/rand" |
| 5 | + "sync" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/pion/interceptor" |
| 9 | + "github.com/pion/logging" |
| 10 | + "github.com/pion/rtcp" |
| 11 | + "github.com/pion/rtp" |
| 12 | +) |
| 13 | + |
| 14 | +// GeneratorInterceptor interceptor generates nack feedback messages. |
| 15 | +type GeneratorInterceptor struct { |
| 16 | + interceptor.NoOp |
| 17 | + size uint16 |
| 18 | + skipLastN uint16 |
| 19 | + interval time.Duration |
| 20 | + receiveLogs *sync.Map |
| 21 | + m sync.Mutex |
| 22 | + wg sync.WaitGroup |
| 23 | + close chan struct{} |
| 24 | + log logging.LeveledLogger |
| 25 | +} |
| 26 | + |
| 27 | +// NewGeneratorInterceptor returns a new GeneratorInterceptor interceptor |
| 28 | +func NewGeneratorInterceptor(opts ...GeneratorOption) (*GeneratorInterceptor, error) { |
| 29 | + r := &GeneratorInterceptor{ |
| 30 | + NoOp: interceptor.NoOp{}, |
| 31 | + size: 8192, |
| 32 | + skipLastN: 0, |
| 33 | + interval: time.Millisecond * 100, |
| 34 | + receiveLogs: &sync.Map{}, |
| 35 | + close: make(chan struct{}), |
| 36 | + log: logging.NewDefaultLoggerFactory().NewLogger("nack_generator"), |
| 37 | + } |
| 38 | + |
| 39 | + for _, opt := range opts { |
| 40 | + opt(r) |
| 41 | + } |
| 42 | + |
| 43 | + if _, err := newReceiveLog(r.size); err != nil { |
| 44 | + return nil, err |
| 45 | + } |
| 46 | + |
| 47 | + return r, nil |
| 48 | +} |
| 49 | + |
| 50 | +// BindRTCPWriter lets you modify any outgoing RTCP packets. It is called once per PeerConnection. The returned method |
| 51 | +// will be called once per packet batch. |
| 52 | +func (n *GeneratorInterceptor) BindRTCPWriter(writer interceptor.RTCPWriter) interceptor.RTCPWriter { |
| 53 | + n.m.Lock() |
| 54 | + defer n.m.Unlock() |
| 55 | + select { |
| 56 | + case <-n.close: |
| 57 | + // already closed |
| 58 | + return writer |
| 59 | + default: |
| 60 | + } |
| 61 | + |
| 62 | + n.wg.Add(1) |
| 63 | + |
| 64 | + go n.loop(writer) |
| 65 | + |
| 66 | + return writer |
| 67 | +} |
| 68 | + |
| 69 | +// BindRemoteStream lets you modify any incoming RTP packets. It is called once for per RemoteStream. The returned method |
| 70 | +// will be called once per rtp packet. |
| 71 | +func (n *GeneratorInterceptor) BindRemoteStream(info *interceptor.StreamInfo, reader interceptor.RTPReader) interceptor.RTPReader { |
| 72 | + hasNack := false |
| 73 | + for _, fb := range info.RTCPFeedback { |
| 74 | + if fb.Type == "nack" && fb.Parameter == "" { |
| 75 | + hasNack = true |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + if !hasNack { |
| 80 | + return reader |
| 81 | + } |
| 82 | + |
| 83 | + // error is already checked in NewGeneratorInterceptor |
| 84 | + receiveLog, _ := newReceiveLog(n.size) |
| 85 | + n.receiveLogs.Store(info.SSRC, receiveLog) |
| 86 | + |
| 87 | + return interceptor.RTPReaderFunc(func() (*rtp.Packet, interceptor.Attributes, error) { |
| 88 | + p, attr, err := reader.Read() |
| 89 | + if err != nil { |
| 90 | + return nil, nil, err |
| 91 | + } |
| 92 | + |
| 93 | + receiveLog.add(p.SequenceNumber) |
| 94 | + |
| 95 | + return p, attr, nil |
| 96 | + }) |
| 97 | +} |
| 98 | + |
| 99 | +// UnbindLocalStream is called when the Stream is removed. It can be used to clean up any data related to that track. |
| 100 | +func (n *GeneratorInterceptor) UnbindLocalStream(info *interceptor.StreamInfo) { |
| 101 | + n.receiveLogs.Delete(info.SSRC) |
| 102 | +} |
| 103 | + |
| 104 | +// Close closes the interceptor |
| 105 | +func (n *GeneratorInterceptor) Close() error { |
| 106 | + defer n.wg.Wait() |
| 107 | + n.m.Lock() |
| 108 | + defer n.m.Unlock() |
| 109 | + |
| 110 | + select { |
| 111 | + case <-n.close: |
| 112 | + // already closed |
| 113 | + return nil |
| 114 | + default: |
| 115 | + } |
| 116 | + |
| 117 | + close(n.close) |
| 118 | + |
| 119 | + return nil |
| 120 | +} |
| 121 | + |
| 122 | +func (n *GeneratorInterceptor) loop(rtcpWriter interceptor.RTCPWriter) { |
| 123 | + defer n.wg.Done() |
| 124 | + |
| 125 | + senderSSRC := rand.Uint32() // #nosec |
| 126 | + |
| 127 | + ticker := time.NewTicker(n.interval) |
| 128 | + for { |
| 129 | + select { |
| 130 | + case <-ticker.C: |
| 131 | + n.receiveLogs.Range(func(key, value interface{}) bool { |
| 132 | + ssrc := key.(uint32) |
| 133 | + receiveLog := value.(*receiveLog) |
| 134 | + |
| 135 | + missing := receiveLog.missingSeqNumbers(n.skipLastN) |
| 136 | + if len(missing) == 0 { |
| 137 | + return true |
| 138 | + } |
| 139 | + |
| 140 | + nack := &rtcp.TransportLayerNack{ |
| 141 | + SenderSSRC: senderSSRC, |
| 142 | + MediaSSRC: ssrc, |
| 143 | + Nacks: rtcp.NackPairsFromSequenceNumbers(missing), |
| 144 | + } |
| 145 | + |
| 146 | + if _, err := rtcpWriter.Write([]rtcp.Packet{nack}, interceptor.Attributes{}); err != nil { |
| 147 | + n.log.Warnf("failed sending nack: %+v", err) |
| 148 | + } |
| 149 | + |
| 150 | + return true |
| 151 | + }) |
| 152 | + |
| 153 | + case <-n.close: |
| 154 | + return |
| 155 | + } |
| 156 | + } |
| 157 | +} |
0 commit comments