public void Write(RtpPacket packet) { lock (_lock) { var delta = _codec != null ? (short)(packet.SequenceNumber - _lastSeqNumber) : (short)1; if (Math.Abs(delta) >= _bufferSize) { ResetBuffer(); delta = 1; } if (_codec == null) { _lastSeqNumber = packet.SequenceNumber - 1; _readSeqNumber = packet.SequenceNumber; } if (_codec == null || _lastPayloadType != packet.PayloadType) { _codec?.Dispose(); _codec = _codecFactory(packet.PayloadType); _packetDuration = _codec.CalcSamplesNumber(packet.Payload, 0, packet.Payload.Length); if (_samples == null || _samples.Length < _packetDuration) { _samples = new short[_packetDuration]; } _bufferSize = (2 * _playBuffer + _packetDuration - 1) / _packetDuration; if (_packets == null || _packets.Length < _bufferSize) { _packets = new RtpPacket[_bufferSize]; } _lastPayloadType = packet.PayloadType; } _packets[(_lastSeqNumber + delta) % _bufferSize] = packet; if (delta > 0) { _lastSeqNumber += delta; } if (_lastSeqNumber - _readSeqNumber >= _bufferSize) { _readSeqNumber = _lastSeqNumber - _bufferSize + 1; } } }