diff --git a/media/engine/webrtc_video_engine.cc b/media/engine/webrtc_video_engine.cc index 358f27d9a5..36a86ec63b 100644 --- a/media/engine/webrtc_video_engine.cc +++ b/media/engine/webrtc_video_engine.cc @@ -200,50 +200,34 @@ std::vector GetDefaultSupportedFormats( // This function will assign dynamic payload types (in the range [96, 127] // and then [35, 63]) to the input codecs, and also add ULPFEC, RED, FlexFEC, // It will also add default feedback params to the codecs. -webrtc::RTCErrorOr> AssignPayloadTypes( - const std::vector& supported_formats, +webrtc::RTCErrorOr AssignPayloadType( + const webrtc::SdpVideoFormat& format, webrtc::PayloadTypePicker& pt_mapper, const webrtc::FieldTrialsView& trials) { - std::vector output_codecs; - for (const webrtc::SdpVideoFormat& format : supported_formats) { - Codec codec = cricket::CreateVideoCodec(format); - codec.id = pt_mapper.SuggestMapping(codec, /* excluder= */ nullptr).value(); - // TODO: https://issues.webrtc.org/360058654 - Handle running out of IDs. - AddDefaultFeedbackParams(&codec, trials); - output_codecs.push_back(codec); + Codec codec = cricket::CreateVideoCodec(format); + webrtc::RTCErrorOr result = + pt_mapper.SuggestMapping(codec, /* excluder= */ nullptr); + if (!result.ok()) { + return result.MoveError(); } - return output_codecs; + codec.id = result.value(); + AddDefaultFeedbackParams(&codec, trials); + return codec; } -// This function will add associated RTX codecs for recognized codecs (VP8, VP9, -// H264, and RED). -webrtc::RTCErrorOr> AddRtx( - const std::vector& input_codecs, - webrtc::PayloadTypePicker& pt_mapper) { - std::vector output_codecs; - for (const Codec& codec : input_codecs) { - // We want to interleave the input codecs with the output codecs. - output_codecs.push_back(codec); - bool isFecCodec = absl::EqualsIgnoreCase(codec.name, kUlpfecCodecName) || - absl::EqualsIgnoreCase(codec.name, kFlexfecCodecName); - // Add associated RTX codec for non-FEC codecs. - if (!isFecCodec) { - Codec rtx_codec = - cricket::CreateVideoRtxCodec(Codec::kIdNotSet, codec.id); - webrtc::RTCErrorOr result = - pt_mapper.SuggestMapping(rtx_codec, /* excluder= */ nullptr); - if (!result.ok()) { - if (result.error().type() == webrtc::RTCErrorType::RESOURCE_EXHAUSTED) { - // Out of payload types. Stop adding RTX codecs. - break; - } - return result.MoveError(); - } - rtx_codec.id = result.value(); - output_codecs.push_back(rtx_codec); - } +// This function will add a associated RTX codec for a recognized primary codecs +// (VP8, VP9, AV1, H264, and RED). +webrtc::RTCErrorOr AddRtx(const Codec& primary_codec, + webrtc::PayloadTypePicker& pt_mapper) { + Codec rtx_codec = + cricket::CreateVideoRtxCodec(Codec::kIdNotSet, primary_codec.id); + webrtc::RTCErrorOr result = + pt_mapper.SuggestMapping(rtx_codec, /* excluder= */ nullptr); + if (!result.ok()) { + return result.MoveError(); } - return output_codecs; + rtx_codec.id = result.value(); + return rtx_codec; } // TODO(kron): Perhaps it is better to move the implicit knowledge to the place @@ -258,30 +242,34 @@ std::vector GetPayloadTypesAndDefaultCodecs( GetDefaultSupportedFormats(factory, is_decoder_factory, trials); // Temporary: Use PayloadTypePicker for assignments. - // TODO: https://issues.webrtc.org/360058654 - stop assigning PTs here. webrtc::PayloadTypePicker pt_mapper; std::vector output_codecs; for (const auto& supported_format : supported_formats) { - webrtc::RTCErrorOr> result = - AssignPayloadTypes({supported_format}, pt_mapper, trials); - RTC_DCHECK(result.ok()); - if (result.ok()) { - for (const auto& codec : result.value()) { - if (include_rtx) { - // This will return both primary and rtx if there is rtx. - result = AddRtx({codec}, pt_mapper); - if (result.ok()) { - for (const auto& codec : result.value()) { - output_codecs.push_back(codec); - } - } else { - output_codecs.push_back(codec); - } - - } else { - output_codecs.push_back(codec); - } + webrtc::RTCErrorOr result = + AssignPayloadType(supported_format, pt_mapper, trials); + if (!result.ok()) { + // TODO: https://issues.webrtc.org/360058654 - stop assigning PTs here. + // TODO: https://issues.webrtc.org/360058654 - Handle running out of IDs. + continue; + } + output_codecs.push_back(result.value()); + if (include_rtx) { + Codec::ResiliencyType resiliency_type = + result.value().GetResiliencyType(); + // FEC codecs do not use retransmission. + if (resiliency_type == Codec::ResiliencyType::kFlexfec || + resiliency_type == Codec::ResiliencyType::kUlpfec) { + continue; } + + webrtc::RTCErrorOr rtx_result = AddRtx(result.value(), pt_mapper); + if (!rtx_result.ok()) { + // TODO: https://issues.webrtc.org/360058654 - stop assigning PTs here. + // TODO: https://issues.webrtc.org/360058654 - Handle running out of + // IDs. + continue; + } + output_codecs.push_back(rtx_result.MoveValue()); } } return output_codecs;