diff --git a/modules/rtp_rtcp/source/rtp_packet_history.cc b/modules/rtp_rtcp/source/rtp_packet_history.cc index e1374feb57..211077bf36 100644 --- a/modules/rtp_rtcp/source/rtp_packet_history.cc +++ b/modules/rtp_rtcp/source/rtp_packet_history.cc @@ -97,6 +97,15 @@ void RtpPacketHistory::PutRtpPacket(std::unique_ptr packet, const uint16_t rtp_seq_no = packet->SequenceNumber(); StoredPacket& stored_packet = packet_history_[rtp_seq_no]; RTC_DCHECK(stored_packet.packet == nullptr); + if (stored_packet.packet) { + // It is an error if this happen. But it can happen if the sequence numbers + // for some reason restart without that the history has been reset. + auto size_iterator = packet_size_.find(stored_packet.packet->size()); + if (size_iterator != packet_size_.end() && + size_iterator->second == stored_packet.packet->SequenceNumber()) { + packet_size_.erase(size_iterator); + } + } stored_packet.packet = std::move(packet); if (stored_packet.packet->capture_time_ms() <= 0) { @@ -209,8 +218,19 @@ std::unique_ptr RtpPacketHistory::GetBestFittingPacket( const uint16_t seq_no = upper_bound_diff < lower_bound_diff ? size_iter_upper->second : size_iter_lower->second; - RtpPacketToSend* best_packet = - packet_history_.find(seq_no)->second.packet.get(); + auto history_it = packet_history_.find(seq_no); + if (history_it == packet_history_.end()) { + RTC_LOG(LS_ERROR) << "Can't find packet in history with seq_no" << seq_no; + RTC_DCHECK(false); + return nullptr; + } + if (!history_it->second.packet) { + RTC_LOG(LS_ERROR) << "Packet pointer is null in history for seq_no" + << seq_no; + RTC_DCHECK(false); + return nullptr; + } + RtpPacketToSend* best_packet = history_it->second.packet.get(); return absl::make_unique(*best_packet); }