deadbeef | e814a0d | 2017-02-26 02:15:09 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 11 | #include "pc/rtp_parameters_conversion.h" |
deadbeef | e814a0d | 2017-02-26 02:15:09 | [diff] [blame] | 12 | |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 13 | #include <cstdint> |
deadbeef | e814a0d | 2017-02-26 02:15:09 | [diff] [blame] | 14 | #include <set> |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 15 | #include <string> |
Harald Alvestrand | c24a218 | 2022-02-23 13:44:59 | [diff] [blame] | 16 | #include <type_traits> |
deadbeef | e814a0d | 2017-02-26 02:15:09 | [diff] [blame] | 17 | #include <utility> |
| 18 | |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 19 | #include "api/array_view.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 20 | #include "api/media_types.h" |
Florent Castelli | d0b8e8e | 2023-06-04 23:22:36 | [diff] [blame] | 21 | #include "api/rtc_error.h" |
Florent Castelli | 8c4b9ea | 2023-06-02 16:06:57 | [diff] [blame] | 22 | #include "media/base/codec.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 23 | #include "media/base/media_constants.h" |
| 24 | #include "media/base/rtp_utils.h" |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 25 | #include "rtc_base/checks.h" |
| 26 | #include "rtc_base/logging.h" |
| 27 | #include "rtc_base/strings/string_builder.h" |
deadbeef | e814a0d | 2017-02-26 02:15:09 | [diff] [blame] | 28 | |
| 29 | namespace webrtc { |
| 30 | |
| 31 | RTCErrorOr<cricket::FeedbackParam> ToCricketFeedbackParam( |
| 32 | const RtcpFeedback& feedback) { |
| 33 | switch (feedback.type) { |
| 34 | case RtcpFeedbackType::CCM: |
| 35 | if (!feedback.message_type) { |
| 36 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 37 | "Missing message type in CCM RtcpFeedback."); |
| 38 | } else if (*feedback.message_type != RtcpFeedbackMessageType::FIR) { |
| 39 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 40 | "Invalid message type in CCM RtcpFeedback."); |
| 41 | } |
| 42 | return cricket::FeedbackParam(cricket::kRtcpFbParamCcm, |
| 43 | cricket::kRtcpFbCcmParamFir); |
Elad Alon | fadb181 | 2019-05-24 11:40:02 | [diff] [blame] | 44 | case RtcpFeedbackType::LNTF: |
| 45 | if (feedback.message_type) { |
| 46 | LOG_AND_RETURN_ERROR( |
| 47 | RTCErrorType::INVALID_PARAMETER, |
| 48 | "Didn't expect message type in LNTF RtcpFeedback."); |
| 49 | } |
| 50 | return cricket::FeedbackParam(cricket::kRtcpFbParamLntf); |
deadbeef | e814a0d | 2017-02-26 02:15:09 | [diff] [blame] | 51 | case RtcpFeedbackType::NACK: |
| 52 | if (!feedback.message_type) { |
| 53 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 54 | "Missing message type in NACK RtcpFeedback."); |
| 55 | } |
| 56 | switch (*feedback.message_type) { |
| 57 | case RtcpFeedbackMessageType::GENERIC_NACK: |
| 58 | return cricket::FeedbackParam(cricket::kRtcpFbParamNack); |
| 59 | case RtcpFeedbackMessageType::PLI: |
| 60 | return cricket::FeedbackParam(cricket::kRtcpFbParamNack, |
| 61 | cricket::kRtcpFbNackParamPli); |
| 62 | default: |
| 63 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 64 | "Invalid message type in NACK RtcpFeedback."); |
| 65 | } |
| 66 | case RtcpFeedbackType::REMB: |
| 67 | if (feedback.message_type) { |
| 68 | LOG_AND_RETURN_ERROR( |
| 69 | RTCErrorType::INVALID_PARAMETER, |
| 70 | "Didn't expect message type in REMB RtcpFeedback."); |
| 71 | } |
| 72 | return cricket::FeedbackParam(cricket::kRtcpFbParamRemb); |
| 73 | case RtcpFeedbackType::TRANSPORT_CC: |
| 74 | if (feedback.message_type) { |
| 75 | LOG_AND_RETURN_ERROR( |
| 76 | RTCErrorType::INVALID_PARAMETER, |
| 77 | "Didn't expect message type in transport-cc RtcpFeedback."); |
| 78 | } |
| 79 | return cricket::FeedbackParam(cricket::kRtcpFbParamTransportCc); |
| 80 | } |
Karl Wiberg | c95b939 | 2020-11-07 23:49:37 | [diff] [blame] | 81 | RTC_CHECK_NOTREACHED(); |
deadbeef | e814a0d | 2017-02-26 02:15:09 | [diff] [blame] | 82 | } |
| 83 | |
Florent Castelli | d0b8e8e | 2023-06-04 23:22:36 | [diff] [blame] | 84 | RTCErrorOr<cricket::Codec> ToCricketCodec(const RtpCodecParameters& codec) { |
| 85 | switch (codec.kind) { |
| 86 | case cricket::MEDIA_TYPE_AUDIO: |
| 87 | if (codec.kind != cricket::MEDIA_TYPE_AUDIO) { |
| 88 | LOG_AND_RETURN_ERROR( |
| 89 | RTCErrorType::INVALID_PARAMETER, |
| 90 | "Can't use video codec with audio sender or receiver."); |
| 91 | } |
| 92 | if (!codec.num_channels) { |
| 93 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 94 | "Missing number of channels for audio codec."); |
| 95 | } |
| 96 | if (*codec.num_channels <= 0) { |
| 97 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_RANGE, |
| 98 | "Number of channels must be positive."); |
| 99 | } |
| 100 | if (!codec.clock_rate) { |
| 101 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 102 | "Missing codec clock rate."); |
| 103 | } |
| 104 | if (*codec.clock_rate <= 0) { |
| 105 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_RANGE, |
| 106 | "Clock rate must be positive."); |
| 107 | } |
| 108 | break; |
| 109 | case cricket::MEDIA_TYPE_VIDEO: |
| 110 | if (codec.kind != cricket::MEDIA_TYPE_VIDEO) { |
| 111 | LOG_AND_RETURN_ERROR( |
| 112 | RTCErrorType::INVALID_PARAMETER, |
| 113 | "Can't use audio codec with video sender or receiver."); |
| 114 | } |
| 115 | if (codec.num_channels) { |
| 116 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 117 | "Video codec shouldn't have num_channels."); |
| 118 | } |
| 119 | if (!codec.clock_rate) { |
| 120 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 121 | "Missing codec clock rate."); |
| 122 | } |
| 123 | if (*codec.clock_rate != cricket::kVideoCodecClockrate) { |
| 124 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 125 | "Video clock rate must be 90000."); |
| 126 | } |
| 127 | break; |
| 128 | default: |
| 129 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 130 | "Unknown codec type"); |
| 131 | } |
deadbeef | e814a0d | 2017-02-26 02:15:09 | [diff] [blame] | 132 | |
deadbeef | e814a0d | 2017-02-26 02:15:09 | [diff] [blame] | 133 | if (!cricket::IsValidRtpPayloadType(codec.payload_type)) { |
Florent Castelli | 72b751a | 2018-06-28 12:09:33 | [diff] [blame] | 134 | char buf[40]; |
| 135 | rtc::SimpleStringBuilder sb(buf); |
| 136 | sb << "Invalid payload type: " << codec.payload_type; |
| 137 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_RANGE, sb.str()); |
deadbeef | e814a0d | 2017-02-26 02:15:09 | [diff] [blame] | 138 | } |
Florent Castelli | d0b8e8e | 2023-06-04 23:22:36 | [diff] [blame] | 139 | |
| 140 | cricket::Codec cricket_codec = [&]() { |
| 141 | if (codec.kind == cricket::MEDIA_TYPE_AUDIO) { |
| 142 | return cricket::CreateAudioCodec(codec.payload_type, codec.name, |
| 143 | *codec.clock_rate, *codec.num_channels); |
| 144 | } |
| 145 | RTC_DCHECK(codec.kind == cricket::MEDIA_TYPE_VIDEO); |
| 146 | return cricket::CreateVideoCodec(codec.payload_type, codec.name); |
| 147 | }(); |
| 148 | |
deadbeef | e814a0d | 2017-02-26 02:15:09 | [diff] [blame] | 149 | for (const RtcpFeedback& feedback : codec.rtcp_feedback) { |
| 150 | auto result = ToCricketFeedbackParam(feedback); |
| 151 | if (!result.ok()) { |
| 152 | return result.MoveError(); |
| 153 | } |
| 154 | cricket_codec.AddFeedbackParam(result.MoveValue()); |
| 155 | } |
Johannes Kron | 72d6915 | 2020-02-10 13:05:55 | [diff] [blame] | 156 | cricket_codec.params = codec.parameters; |
deadbeef | e814a0d | 2017-02-26 02:15:09 | [diff] [blame] | 157 | return std::move(cricket_codec); |
| 158 | } |
| 159 | |
Florent Castelli | d0b8e8e | 2023-06-04 23:22:36 | [diff] [blame] | 160 | RTCErrorOr<std::vector<cricket::Codec>> ToCricketCodecs( |
deadbeef | e814a0d | 2017-02-26 02:15:09 | [diff] [blame] | 161 | const std::vector<RtpCodecParameters>& codecs) { |
Florent Castelli | d0b8e8e | 2023-06-04 23:22:36 | [diff] [blame] | 162 | std::vector<cricket::Codec> cricket_codecs; |
deadbeef | e814a0d | 2017-02-26 02:15:09 | [diff] [blame] | 163 | std::set<int> seen_payload_types; |
| 164 | for (const RtpCodecParameters& codec : codecs) { |
Florent Castelli | d0b8e8e | 2023-06-04 23:22:36 | [diff] [blame] | 165 | auto result = ToCricketCodec(codec); |
deadbeef | e814a0d | 2017-02-26 02:15:09 | [diff] [blame] | 166 | if (!result.ok()) { |
| 167 | return result.MoveError(); |
| 168 | } |
| 169 | if (!seen_payload_types.insert(codec.payload_type).second) { |
Florent Castelli | 72b751a | 2018-06-28 12:09:33 | [diff] [blame] | 170 | char buf[40]; |
| 171 | rtc::SimpleStringBuilder sb(buf); |
| 172 | sb << "Duplicate payload type: " << codec.payload_type; |
| 173 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, sb.str()); |
deadbeef | e814a0d | 2017-02-26 02:15:09 | [diff] [blame] | 174 | } |
| 175 | cricket_codecs.push_back(result.MoveValue()); |
| 176 | } |
| 177 | return std::move(cricket_codecs); |
| 178 | } |
| 179 | |
deadbeef | e814a0d | 2017-02-26 02:15:09 | [diff] [blame] | 180 | RTCErrorOr<cricket::StreamParamsVec> ToCricketStreamParamsVec( |
| 181 | const std::vector<RtpEncodingParameters>& encodings) { |
| 182 | if (encodings.size() > 1u) { |
| 183 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_PARAMETER, |
| 184 | "ORTC API implementation doesn't currently " |
| 185 | "support simulcast or layered encodings."); |
| 186 | } else if (encodings.empty()) { |
| 187 | return cricket::StreamParamsVec(); |
| 188 | } |
| 189 | cricket::StreamParamsVec cricket_streams; |
| 190 | const RtpEncodingParameters& encoding = encodings[0]; |
deadbeef | e814a0d | 2017-02-26 02:15:09 | [diff] [blame] | 191 | if (encoding.ssrc) { |
| 192 | cricket::StreamParams stream_params; |
| 193 | stream_params.add_ssrc(*encoding.ssrc); |
deadbeef | e814a0d | 2017-02-26 02:15:09 | [diff] [blame] | 194 | cricket_streams.push_back(std::move(stream_params)); |
| 195 | } |
| 196 | return std::move(cricket_streams); |
| 197 | } |
| 198 | |
Danil Chapovalov | 00c71836 | 2018-06-15 13:58:38 | [diff] [blame] | 199 | absl::optional<RtcpFeedback> ToRtcpFeedback( |
deadbeef | e814a0d | 2017-02-26 02:15:09 | [diff] [blame] | 200 | const cricket::FeedbackParam& cricket_feedback) { |
| 201 | if (cricket_feedback.id() == cricket::kRtcpFbParamCcm) { |
| 202 | if (cricket_feedback.param() == cricket::kRtcpFbCcmParamFir) { |
Oskar Sundbom | ff610bd | 2017-11-16 09:57:44 | [diff] [blame] | 203 | return RtcpFeedback(RtcpFeedbackType::CCM, RtcpFeedbackMessageType::FIR); |
deadbeef | e814a0d | 2017-02-26 02:15:09 | [diff] [blame] | 204 | } else { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 205 | RTC_LOG(LS_WARNING) << "Unsupported parameter for CCM RTCP feedback: " |
| 206 | << cricket_feedback.param(); |
Danil Chapovalov | 00c71836 | 2018-06-15 13:58:38 | [diff] [blame] | 207 | return absl::nullopt; |
deadbeef | e814a0d | 2017-02-26 02:15:09 | [diff] [blame] | 208 | } |
Elad Alon | fadb181 | 2019-05-24 11:40:02 | [diff] [blame] | 209 | } else if (cricket_feedback.id() == cricket::kRtcpFbParamLntf) { |
| 210 | if (cricket_feedback.param().empty()) { |
| 211 | return RtcpFeedback(RtcpFeedbackType::LNTF); |
| 212 | } else { |
| 213 | RTC_LOG(LS_WARNING) << "Unsupported parameter for LNTF RTCP feedback: " |
| 214 | << cricket_feedback.param(); |
| 215 | return absl::nullopt; |
| 216 | } |
deadbeef | e814a0d | 2017-02-26 02:15:09 | [diff] [blame] | 217 | } else if (cricket_feedback.id() == cricket::kRtcpFbParamNack) { |
| 218 | if (cricket_feedback.param().empty()) { |
Oskar Sundbom | ff610bd | 2017-11-16 09:57:44 | [diff] [blame] | 219 | return RtcpFeedback(RtcpFeedbackType::NACK, |
| 220 | RtcpFeedbackMessageType::GENERIC_NACK); |
deadbeef | e814a0d | 2017-02-26 02:15:09 | [diff] [blame] | 221 | } else if (cricket_feedback.param() == cricket::kRtcpFbNackParamPli) { |
Oskar Sundbom | ff610bd | 2017-11-16 09:57:44 | [diff] [blame] | 222 | return RtcpFeedback(RtcpFeedbackType::NACK, RtcpFeedbackMessageType::PLI); |
deadbeef | e814a0d | 2017-02-26 02:15:09 | [diff] [blame] | 223 | } else { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 224 | RTC_LOG(LS_WARNING) << "Unsupported parameter for NACK RTCP feedback: " |
| 225 | << cricket_feedback.param(); |
Danil Chapovalov | 00c71836 | 2018-06-15 13:58:38 | [diff] [blame] | 226 | return absl::nullopt; |
deadbeef | e814a0d | 2017-02-26 02:15:09 | [diff] [blame] | 227 | } |
| 228 | } else if (cricket_feedback.id() == cricket::kRtcpFbParamRemb) { |
| 229 | if (!cricket_feedback.param().empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 230 | RTC_LOG(LS_WARNING) << "Unsupported parameter for REMB RTCP feedback: " |
| 231 | << cricket_feedback.param(); |
Danil Chapovalov | 00c71836 | 2018-06-15 13:58:38 | [diff] [blame] | 232 | return absl::nullopt; |
deadbeef | e814a0d | 2017-02-26 02:15:09 | [diff] [blame] | 233 | } else { |
Oskar Sundbom | ff610bd | 2017-11-16 09:57:44 | [diff] [blame] | 234 | return RtcpFeedback(RtcpFeedbackType::REMB); |
deadbeef | e814a0d | 2017-02-26 02:15:09 | [diff] [blame] | 235 | } |
| 236 | } else if (cricket_feedback.id() == cricket::kRtcpFbParamTransportCc) { |
| 237 | if (!cricket_feedback.param().empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 238 | RTC_LOG(LS_WARNING) |
deadbeef | e814a0d | 2017-02-26 02:15:09 | [diff] [blame] | 239 | << "Unsupported parameter for transport-cc RTCP feedback: " |
| 240 | << cricket_feedback.param(); |
Danil Chapovalov | 00c71836 | 2018-06-15 13:58:38 | [diff] [blame] | 241 | return absl::nullopt; |
deadbeef | e814a0d | 2017-02-26 02:15:09 | [diff] [blame] | 242 | } else { |
Oskar Sundbom | ff610bd | 2017-11-16 09:57:44 | [diff] [blame] | 243 | return RtcpFeedback(RtcpFeedbackType::TRANSPORT_CC); |
deadbeef | e814a0d | 2017-02-26 02:15:09 | [diff] [blame] | 244 | } |
| 245 | } |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 246 | RTC_LOG(LS_WARNING) << "Unsupported RTCP feedback type: " |
| 247 | << cricket_feedback.id(); |
Danil Chapovalov | 00c71836 | 2018-06-15 13:58:38 | [diff] [blame] | 248 | return absl::nullopt; |
deadbeef | e814a0d | 2017-02-26 02:15:09 | [diff] [blame] | 249 | } |
| 250 | |
zhihuang | 2436639 | 2017-03-09 01:15:06 | [diff] [blame] | 251 | std::vector<RtpEncodingParameters> ToRtpEncodings( |
| 252 | const cricket::StreamParamsVec& stream_params) { |
| 253 | std::vector<RtpEncodingParameters> rtp_encodings; |
| 254 | for (const cricket::StreamParams& stream_param : stream_params) { |
| 255 | RtpEncodingParameters rtp_encoding; |
| 256 | rtp_encoding.ssrc.emplace(stream_param.first_ssrc()); |
zhihuang | 2436639 | 2017-03-09 01:15:06 | [diff] [blame] | 257 | rtp_encodings.push_back(std::move(rtp_encoding)); |
| 258 | } |
| 259 | return rtp_encodings; |
| 260 | } |
| 261 | |
Florent Castelli | d0b8e8e | 2023-06-04 23:22:36 | [diff] [blame] | 262 | RtpCodecCapability ToRtpCodecCapability(const cricket::Codec& cricket_codec) { |
deadbeef | e814a0d | 2017-02-26 02:15:09 | [diff] [blame] | 263 | RtpCodecCapability codec; |
| 264 | codec.name = cricket_codec.name; |
Florent Castelli | d0b8e8e | 2023-06-04 23:22:36 | [diff] [blame] | 265 | codec.kind = cricket_codec.type == cricket::Codec::Type::kAudio |
| 266 | ? cricket::MEDIA_TYPE_AUDIO |
| 267 | : cricket::MEDIA_TYPE_VIDEO; |
deadbeef | e814a0d | 2017-02-26 02:15:09 | [diff] [blame] | 268 | codec.clock_rate.emplace(cricket_codec.clockrate); |
| 269 | codec.preferred_payload_type.emplace(cricket_codec.id); |
| 270 | for (const cricket::FeedbackParam& cricket_feedback : |
| 271 | cricket_codec.feedback_params.params()) { |
Danil Chapovalov | 00c71836 | 2018-06-15 13:58:38 | [diff] [blame] | 272 | absl::optional<RtcpFeedback> feedback = ToRtcpFeedback(cricket_feedback); |
deadbeef | e814a0d | 2017-02-26 02:15:09 | [diff] [blame] | 273 | if (feedback) { |
Danil Chapovalov | 57ff273 | 2018-03-28 09:25:15 | [diff] [blame] | 274 | codec.rtcp_feedback.push_back(feedback.value()); |
deadbeef | e814a0d | 2017-02-26 02:15:09 | [diff] [blame] | 275 | } |
| 276 | } |
Florent Castelli | d0b8e8e | 2023-06-04 23:22:36 | [diff] [blame] | 277 | switch (cricket_codec.type) { |
| 278 | case cricket::Codec::Type::kAudio: |
| 279 | codec.num_channels = static_cast<int>(cricket_codec.channels); |
| 280 | break; |
| 281 | case cricket::Codec::Type::kVideo: |
| 282 | codec.scalability_modes = cricket_codec.scalability_modes; |
| 283 | break; |
| 284 | } |
deadbeef | e814a0d | 2017-02-26 02:15:09 | [diff] [blame] | 285 | codec.parameters.insert(cricket_codec.params.begin(), |
| 286 | cricket_codec.params.end()); |
| 287 | return codec; |
| 288 | } |
| 289 | |
Florent Castelli | d0b8e8e | 2023-06-04 23:22:36 | [diff] [blame] | 290 | RtpCodecParameters ToRtpCodecParameters(const cricket::Codec& cricket_codec) { |
zhihuang | 2436639 | 2017-03-09 01:15:06 | [diff] [blame] | 291 | RtpCodecParameters codec_param; |
| 292 | codec_param.name = cricket_codec.name; |
Florent Castelli | d0b8e8e | 2023-06-04 23:22:36 | [diff] [blame] | 293 | codec_param.kind = cricket_codec.type == cricket::Codec::Type::kAudio |
| 294 | ? cricket::MEDIA_TYPE_AUDIO |
| 295 | : cricket::MEDIA_TYPE_VIDEO; |
zhihuang | 2436639 | 2017-03-09 01:15:06 | [diff] [blame] | 296 | codec_param.clock_rate.emplace(cricket_codec.clockrate); |
| 297 | codec_param.payload_type = cricket_codec.id; |
| 298 | for (const cricket::FeedbackParam& cricket_feedback : |
| 299 | cricket_codec.feedback_params.params()) { |
Danil Chapovalov | 00c71836 | 2018-06-15 13:58:38 | [diff] [blame] | 300 | absl::optional<RtcpFeedback> feedback = ToRtcpFeedback(cricket_feedback); |
zhihuang | 2436639 | 2017-03-09 01:15:06 | [diff] [blame] | 301 | if (feedback) { |
Danil Chapovalov | 57ff273 | 2018-03-28 09:25:15 | [diff] [blame] | 302 | codec_param.rtcp_feedback.push_back(feedback.value()); |
zhihuang | 2436639 | 2017-03-09 01:15:06 | [diff] [blame] | 303 | } |
| 304 | } |
Florent Castelli | d0b8e8e | 2023-06-04 23:22:36 | [diff] [blame] | 305 | switch (cricket_codec.type) { |
| 306 | case cricket::Codec::Type::kAudio: |
| 307 | codec_param.num_channels = static_cast<int>(cricket_codec.channels); |
| 308 | break; |
| 309 | case cricket::Codec::Type::kVideo: |
| 310 | // Nothing to do. |
| 311 | break; |
| 312 | } |
Johannes Kron | 72d6915 | 2020-02-10 13:05:55 | [diff] [blame] | 313 | codec_param.parameters = cricket_codec.params; |
zhihuang | 2436639 | 2017-03-09 01:15:06 | [diff] [blame] | 314 | return codec_param; |
| 315 | } |
| 316 | |
deadbeef | e814a0d | 2017-02-26 02:15:09 | [diff] [blame] | 317 | RtpCapabilities ToRtpCapabilities( |
Florent Castelli | d0b8e8e | 2023-06-04 23:22:36 | [diff] [blame] | 318 | const std::vector<cricket::Codec>& cricket_codecs, |
deadbeef | e814a0d | 2017-02-26 02:15:09 | [diff] [blame] | 319 | const cricket::RtpHeaderExtensions& cricket_extensions) { |
| 320 | RtpCapabilities capabilities; |
| 321 | bool have_red = false; |
| 322 | bool have_ulpfec = false; |
| 323 | bool have_flexfec = false; |
Florent Castelli | 5473a45 | 2018-11-06 16:27:21 | [diff] [blame] | 324 | bool have_rtx = false; |
Florent Castelli | d0b8e8e | 2023-06-04 23:22:36 | [diff] [blame] | 325 | for (const cricket::Codec& cricket_codec : cricket_codecs) { |
deadbeef | e814a0d | 2017-02-26 02:15:09 | [diff] [blame] | 326 | if (cricket_codec.name == cricket::kRedCodecName) { |
| 327 | have_red = true; |
| 328 | } else if (cricket_codec.name == cricket::kUlpfecCodecName) { |
| 329 | have_ulpfec = true; |
| 330 | } else if (cricket_codec.name == cricket::kFlexfecCodecName) { |
| 331 | have_flexfec = true; |
Florent Castelli | 5473a45 | 2018-11-06 16:27:21 | [diff] [blame] | 332 | } else if (cricket_codec.name == cricket::kRtxCodecName) { |
| 333 | if (have_rtx) { |
| 334 | // There should only be one RTX codec entry |
| 335 | continue; |
| 336 | } |
| 337 | have_rtx = true; |
deadbeef | e814a0d | 2017-02-26 02:15:09 | [diff] [blame] | 338 | } |
Florent Castelli | 5473a45 | 2018-11-06 16:27:21 | [diff] [blame] | 339 | auto codec_capability = ToRtpCodecCapability(cricket_codec); |
| 340 | if (cricket_codec.name == cricket::kRtxCodecName) { |
| 341 | // RTX codec should not have any parameter |
| 342 | codec_capability.parameters.clear(); |
| 343 | } |
| 344 | capabilities.codecs.push_back(codec_capability); |
deadbeef | e814a0d | 2017-02-26 02:15:09 | [diff] [blame] | 345 | } |
| 346 | for (const RtpExtension& cricket_extension : cricket_extensions) { |
| 347 | capabilities.header_extensions.emplace_back(cricket_extension.uri, |
| 348 | cricket_extension.id); |
| 349 | } |
| 350 | if (have_red) { |
| 351 | capabilities.fec.push_back(FecMechanism::RED); |
| 352 | } |
| 353 | if (have_red && have_ulpfec) { |
| 354 | capabilities.fec.push_back(FecMechanism::RED_AND_ULPFEC); |
| 355 | } |
| 356 | if (have_flexfec) { |
| 357 | capabilities.fec.push_back(FecMechanism::FLEXFEC); |
| 358 | } |
| 359 | return capabilities; |
| 360 | } |
| 361 | |
zhihuang | 2436639 | 2017-03-09 01:15:06 | [diff] [blame] | 362 | RtpParameters ToRtpParameters( |
Florent Castelli | d0b8e8e | 2023-06-04 23:22:36 | [diff] [blame] | 363 | const std::vector<cricket::Codec>& cricket_codecs, |
zhihuang | 2436639 | 2017-03-09 01:15:06 | [diff] [blame] | 364 | const cricket::RtpHeaderExtensions& cricket_extensions, |
| 365 | const cricket::StreamParamsVec& stream_params) { |
| 366 | RtpParameters rtp_parameters; |
Florent Castelli | d0b8e8e | 2023-06-04 23:22:36 | [diff] [blame] | 367 | for (const cricket::Codec& cricket_codec : cricket_codecs) { |
zhihuang | 2436639 | 2017-03-09 01:15:06 | [diff] [blame] | 368 | rtp_parameters.codecs.push_back(ToRtpCodecParameters(cricket_codec)); |
| 369 | } |
| 370 | for (const RtpExtension& cricket_extension : cricket_extensions) { |
| 371 | rtp_parameters.header_extensions.emplace_back(cricket_extension.uri, |
| 372 | cricket_extension.id); |
| 373 | } |
| 374 | rtp_parameters.encodings = ToRtpEncodings(stream_params); |
| 375 | return rtp_parameters; |
| 376 | } |
| 377 | |
deadbeef | e814a0d | 2017-02-26 02:15:09 | [diff] [blame] | 378 | } // namespace webrtc |