diff --git a/pc/media_session.cc b/pc/media_session.cc index a8dcf2c627..00daacd4b7 100644 --- a/pc/media_session.cc +++ b/pc/media_session.cc @@ -741,10 +741,16 @@ static Codecs MatchCodecPreference( const Codecs& codecs, const Codecs& supported_codecs) { Codecs filtered_codecs; - std::set kept_codecs_ids; bool want_rtx = false; bool want_red = false; + for (const auto& codec_preference : codec_preferences) { + if (IsRtxCodec(codec_preference)) { + want_rtx = true; + } else if (IsRedCodec(codec_preference)) { + want_red = true; + } + } for (const auto& codec_preference : codec_preferences) { auto found_codec = absl::c_find_if( supported_codecs, @@ -764,33 +770,30 @@ static Codecs MatchCodecPreference( if (FindMatchingCodec(supported_codecs, codecs, *found_codec, &found_codec_with_correct_pt)) { filtered_codecs.push_back(found_codec_with_correct_pt); - kept_codecs_ids.insert(std::to_string(found_codec_with_correct_pt.id)); - } - } else if (IsRtxCodec(codec_preference)) { - want_rtx = true; - } else if (IsRedCodec(codec_preference)) { - want_red = true; - } - } - - if (want_rtx || want_red) { - for (const auto& codec : codecs) { - if (IsRtxCodec(codec)) { - const auto apt = - codec.params.find(cricket::kCodecParamAssociatedPayloadType); - if (apt != codec.params.end() && - kept_codecs_ids.count(apt->second) > 0) { - filtered_codecs.push_back(codec); - } - } else if (IsRedCodec(codec)) { - const auto fmtp = - codec.params.find(cricket::kCodecParamNotInNameValueFormat); - if (fmtp != codec.params.end()) { - std::vector redundant_payloads; - rtc::split(fmtp->second, '/', &redundant_payloads); - if (redundant_payloads.size() > 0 && - kept_codecs_ids.count(redundant_payloads[0]) > 0) { - filtered_codecs.push_back(codec); + std::string id = rtc::ToString(found_codec_with_correct_pt.id); + // Search for the matching rtx or red codec. + if (want_red || want_rtx) { + for (const auto& codec : codecs) { + if (IsRtxCodec(codec)) { + const auto apt = + codec.params.find(cricket::kCodecParamAssociatedPayloadType); + if (apt != codec.params.end() && apt->second == id) { + filtered_codecs.push_back(codec); + break; + } + } else if (IsRedCodec(codec)) { + const auto fmtp = + codec.params.find(cricket::kCodecParamNotInNameValueFormat); + if (fmtp != codec.params.end()) { + std::vector redundant_payloads; + rtc::split(fmtp->second, '/', &redundant_payloads); + if (redundant_payloads.size() > 0 && + redundant_payloads[0] == id) { + filtered_codecs.push_back(codec); + break; + } + } + } } } }