Write adds an RTP Packet, ordering is not guaranteed, newer packets may arrive later
(pkt []byte)
| 139 | |
| 140 | // Write adds an RTP Packet, ordering is not guaranteed, newer packets may arrive later |
| 141 | func (b *Buffer) Write(pkt []byte) (n int, err error) { |
| 142 | var rtpPacket rtp.Packet |
| 143 | err = rtpPacket.Unmarshal(pkt) |
| 144 | if err != nil { |
| 145 | return |
| 146 | } |
| 147 | |
| 148 | b.Lock() |
| 149 | if b.BufferBase.IsClosed() { |
| 150 | b.Unlock() |
| 151 | err = io.EOF |
| 152 | return |
| 153 | } |
| 154 | |
| 155 | now := mono.UnixNano() |
| 156 | if b.twcc != nil && b.twccExtID != 0 { |
| 157 | if ext := rtpPacket.GetExtension(b.twccExtID); ext != nil { |
| 158 | b.twcc.Push(rtpPacket.SSRC, binary.BigEndian.Uint16(ext[0:2]), now, rtpPacket.Marker) |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | // libwebrtc will use 0 ssrc for probing, don't push the packet to pending queue to avoid memory increasing since |
| 163 | // the Bind will not be called to consume the pending packets. More details in https://github.com/pion/webrtc/pull/2816 |
| 164 | if rtpPacket.SSRC == 0 { |
| 165 | b.Unlock() |
| 166 | return |
| 167 | } |
| 168 | |
| 169 | // handle RTX packet |
| 170 | if pb := b.primaryBufferForRTX; pb != nil { |
| 171 | b.Unlock() |
| 172 | |
| 173 | // skip padding only packets |
| 174 | if rtpPacket.Padding && len(rtpPacket.Payload) == 0 { |
| 175 | return |
| 176 | } |
| 177 | |
| 178 | pb.writeRTX(&rtpPacket, now) |
| 179 | return |
| 180 | } |
| 181 | |
| 182 | if !b.isBound { |
| 183 | packet := make([]byte, len(pkt)) |
| 184 | copy(packet, pkt) |
| 185 | |
| 186 | if len(b.pPackets) == 0 { |
| 187 | b.logger.Debugw("received first packet") |
| 188 | } |
| 189 | |
| 190 | startIdx := 0 |
| 191 | overflow := len(b.pPackets) - max(b.BufferBase.MaxVideoPkts(), b.BufferBase.MaxAudioPkts()) |
| 192 | if overflow > 0 { |
| 193 | startIdx = overflow |
| 194 | } |
| 195 | b.pPackets = append(b.pPackets[startIdx:], pendingPacket{ |
| 196 | packet: packet, |
| 197 | arrivalTime: now, |
| 198 | }) |