deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [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 09:11:00 -0800 | [diff] [blame] | 11 | #include "pc/rtp_parameters_conversion.h" |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 12 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 13 | #include <cstdint> |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 14 | #include <set> |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 15 | #include <string> |
| 16 | #include <unordered_map> |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 17 | #include <utility> |
| 18 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 19 | #include "api/array_view.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 20 | #include "api/media_types.h" |
| 21 | #include "media/base/media_constants.h" |
| 22 | #include "media/base/rtp_utils.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 23 | #include "rtc_base/checks.h" |
| 24 | #include "rtc_base/logging.h" |
| 25 | #include "rtc_base/strings/string_builder.h" |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 26 | |
| 27 | namespace webrtc { |
| 28 | |
| 29 | RTCErrorOr<cricket::FeedbackParam> ToCricketFeedbackParam( |
| 30 | const RtcpFeedback& feedback) { |
| 31 | switch (feedback.type) { |
| 32 | case RtcpFeedbackType::CCM: |
| 33 | if (!feedback.message_type) { |
| 34 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 35 | "Missing message type in CCM RtcpFeedback."); |
| 36 | } else if (*feedback.message_type != RtcpFeedbackMessageType::FIR) { |
| 37 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 38 | "Invalid message type in CCM RtcpFeedback."); |
| 39 | } |
| 40 | return cricket::FeedbackParam(cricket::kRtcpFbParamCcm, |
| 41 | cricket::kRtcpFbCcmParamFir); |
| 42 | case RtcpFeedbackType::NACK: |
| 43 | if (!feedback.message_type) { |
| 44 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 45 | "Missing message type in NACK RtcpFeedback."); |
| 46 | } |
| 47 | switch (*feedback.message_type) { |
| 48 | case RtcpFeedbackMessageType::GENERIC_NACK: |
| 49 | return cricket::FeedbackParam(cricket::kRtcpFbParamNack); |
| 50 | case RtcpFeedbackMessageType::PLI: |
| 51 | return cricket::FeedbackParam(cricket::kRtcpFbParamNack, |
| 52 | cricket::kRtcpFbNackParamPli); |
| 53 | default: |
| 54 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 55 | "Invalid message type in NACK RtcpFeedback."); |
| 56 | } |
| 57 | case RtcpFeedbackType::REMB: |
| 58 | if (feedback.message_type) { |
| 59 | LOG_AND_RETURN_ERROR( |
| 60 | RTCErrorType::INVALID_PARAMETER, |
| 61 | "Didn't expect message type in REMB RtcpFeedback."); |
| 62 | } |
| 63 | return cricket::FeedbackParam(cricket::kRtcpFbParamRemb); |
| 64 | case RtcpFeedbackType::TRANSPORT_CC: |
| 65 | if (feedback.message_type) { |
| 66 | LOG_AND_RETURN_ERROR( |
| 67 | RTCErrorType::INVALID_PARAMETER, |
| 68 | "Didn't expect message type in transport-cc RtcpFeedback."); |
| 69 | } |
| 70 | return cricket::FeedbackParam(cricket::kRtcpFbParamTransportCc); |
| 71 | } |
| 72 | // Not reached; avoids compile warning. |
| 73 | FATAL(); |
| 74 | } |
| 75 | |
| 76 | template <typename C> |
| 77 | static RTCError ToCricketCodecTypeSpecific(const RtpCodecParameters& codec, |
| 78 | C* cricket_codec); |
| 79 | |
| 80 | template <> |
| 81 | RTCError ToCricketCodecTypeSpecific<cricket::AudioCodec>( |
| 82 | const RtpCodecParameters& codec, |
| 83 | cricket::AudioCodec* cricket_codec) { |
| 84 | if (codec.kind != cricket::MEDIA_TYPE_AUDIO) { |
| 85 | LOG_AND_RETURN_ERROR( |
| 86 | RTCErrorType::INVALID_PARAMETER, |
| 87 | "Can't use video codec with audio sender or receiver."); |
| 88 | } |
| 89 | if (!codec.num_channels) { |
| 90 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 91 | "Missing number of channels for audio codec."); |
| 92 | } |
| 93 | if (*codec.num_channels <= 0) { |
| 94 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_RANGE, |
| 95 | "Number of channels must be positive."); |
| 96 | } |
| 97 | cricket_codec->channels = *codec.num_channels; |
| 98 | if (!codec.clock_rate) { |
| 99 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 100 | "Missing codec clock rate."); |
| 101 | } |
| 102 | if (*codec.clock_rate <= 0) { |
| 103 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_RANGE, |
| 104 | "Clock rate must be positive."); |
| 105 | } |
| 106 | cricket_codec->clockrate = *codec.clock_rate; |
| 107 | return RTCError::OK(); |
| 108 | } |
| 109 | |
| 110 | // Video codecs don't use num_channels or clock_rate, but they should at least |
| 111 | // be validated to ensure the application isn't trying to do something it |
| 112 | // doesn't intend to. |
| 113 | template <> |
| 114 | RTCError ToCricketCodecTypeSpecific<cricket::VideoCodec>( |
| 115 | const RtpCodecParameters& codec, |
| 116 | cricket::VideoCodec*) { |
| 117 | if (codec.kind != cricket::MEDIA_TYPE_VIDEO) { |
| 118 | LOG_AND_RETURN_ERROR( |
| 119 | RTCErrorType::INVALID_PARAMETER, |
| 120 | "Can't use audio codec with video sender or receiver."); |
| 121 | } |
| 122 | if (codec.num_channels) { |
| 123 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 124 | "Video codec shouldn't have num_channels."); |
| 125 | } |
| 126 | if (!codec.clock_rate) { |
| 127 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 128 | "Missing codec clock rate."); |
| 129 | } |
| 130 | if (*codec.clock_rate != cricket::kVideoCodecClockrate) { |
| 131 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 132 | "Video clock rate must be 90000."); |
| 133 | } |
| 134 | return RTCError::OK(); |
| 135 | } |
| 136 | |
| 137 | template <typename C> |
| 138 | RTCErrorOr<C> ToCricketCodec(const RtpCodecParameters& codec) { |
| 139 | C cricket_codec; |
| 140 | // Start with audio/video specific conversion. |
| 141 | RTCError err = ToCricketCodecTypeSpecific(codec, &cricket_codec); |
| 142 | if (!err.ok()) { |
| 143 | return std::move(err); |
| 144 | } |
| 145 | cricket_codec.name = codec.name; |
| 146 | if (!cricket::IsValidRtpPayloadType(codec.payload_type)) { |
Florent Castelli | 72b751a | 2018-06-28 14:09:33 +0200 | [diff] [blame] | 147 | char buf[40]; |
| 148 | rtc::SimpleStringBuilder sb(buf); |
| 149 | sb << "Invalid payload type: " << codec.payload_type; |
| 150 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_RANGE, sb.str()); |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 151 | } |
| 152 | cricket_codec.id = codec.payload_type; |
| 153 | for (const RtcpFeedback& feedback : codec.rtcp_feedback) { |
| 154 | auto result = ToCricketFeedbackParam(feedback); |
| 155 | if (!result.ok()) { |
| 156 | return result.MoveError(); |
| 157 | } |
| 158 | cricket_codec.AddFeedbackParam(result.MoveValue()); |
| 159 | } |
| 160 | cricket_codec.params.insert(codec.parameters.begin(), codec.parameters.end()); |
| 161 | return std::move(cricket_codec); |
| 162 | } |
| 163 | |
| 164 | template RTCErrorOr<cricket::AudioCodec> ToCricketCodec( |
| 165 | const RtpCodecParameters& codec); |
| 166 | template RTCErrorOr<cricket::VideoCodec> ToCricketCodec( |
| 167 | const RtpCodecParameters& codec); |
| 168 | |
| 169 | template <typename C> |
| 170 | RTCErrorOr<std::vector<C>> ToCricketCodecs( |
| 171 | const std::vector<RtpCodecParameters>& codecs) { |
| 172 | std::vector<C> cricket_codecs; |
| 173 | std::set<int> seen_payload_types; |
| 174 | for (const RtpCodecParameters& codec : codecs) { |
| 175 | auto result = ToCricketCodec<C>(codec); |
| 176 | if (!result.ok()) { |
| 177 | return result.MoveError(); |
| 178 | } |
| 179 | if (!seen_payload_types.insert(codec.payload_type).second) { |
Florent Castelli | 72b751a | 2018-06-28 14:09:33 +0200 | [diff] [blame] | 180 | char buf[40]; |
| 181 | rtc::SimpleStringBuilder sb(buf); |
| 182 | sb << "Duplicate payload type: " << codec.payload_type; |
| 183 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, sb.str()); |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 184 | } |
| 185 | cricket_codecs.push_back(result.MoveValue()); |
| 186 | } |
| 187 | return std::move(cricket_codecs); |
| 188 | } |
| 189 | |
| 190 | template RTCErrorOr<std::vector<cricket::AudioCodec>> ToCricketCodecs< |
| 191 | cricket::AudioCodec>(const std::vector<RtpCodecParameters>& codecs); |
| 192 | |
| 193 | template RTCErrorOr<std::vector<cricket::VideoCodec>> ToCricketCodecs< |
| 194 | cricket::VideoCodec>(const std::vector<RtpCodecParameters>& codecs); |
| 195 | |
| 196 | RTCErrorOr<cricket::RtpHeaderExtensions> ToCricketRtpHeaderExtensions( |
| 197 | const std::vector<RtpHeaderExtensionParameters>& extensions) { |
| 198 | cricket::RtpHeaderExtensions cricket_extensions; |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 199 | std::set<int> seen_header_extension_ids; |
| 200 | for (const RtpHeaderExtensionParameters& extension : extensions) { |
| 201 | if (extension.id < RtpHeaderExtensionParameters::kMinId || |
| 202 | extension.id > RtpHeaderExtensionParameters::kMaxId) { |
Florent Castelli | 72b751a | 2018-06-28 14:09:33 +0200 | [diff] [blame] | 203 | char buf[50]; |
| 204 | rtc::SimpleStringBuilder sb(buf); |
| 205 | sb << "Invalid header extension id: " << extension.id; |
| 206 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_RANGE, sb.str()); |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 207 | } |
| 208 | if (!seen_header_extension_ids.insert(extension.id).second) { |
Florent Castelli | 72b751a | 2018-06-28 14:09:33 +0200 | [diff] [blame] | 209 | char buf[50]; |
| 210 | rtc::SimpleStringBuilder sb(buf); |
| 211 | sb << "Duplicate header extension id: " << extension.id; |
| 212 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, sb.str()); |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 213 | } |
| 214 | cricket_extensions.push_back(extension); |
| 215 | } |
| 216 | return std::move(cricket_extensions); |
| 217 | } |
| 218 | |
| 219 | RTCErrorOr<cricket::StreamParamsVec> ToCricketStreamParamsVec( |
| 220 | const std::vector<RtpEncodingParameters>& encodings) { |
| 221 | if (encodings.size() > 1u) { |
| 222 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_PARAMETER, |
| 223 | "ORTC API implementation doesn't currently " |
| 224 | "support simulcast or layered encodings."); |
| 225 | } else if (encodings.empty()) { |
| 226 | return cricket::StreamParamsVec(); |
| 227 | } |
| 228 | cricket::StreamParamsVec cricket_streams; |
| 229 | const RtpEncodingParameters& encoding = encodings[0]; |
| 230 | if (encoding.rtx && encoding.rtx->ssrc && !encoding.ssrc) { |
| 231 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_PARAMETER, |
| 232 | "Setting an RTX SSRC explicitly while leaving the " |
| 233 | "primary SSRC unset is not currently supported."); |
| 234 | } |
| 235 | if (encoding.ssrc) { |
| 236 | cricket::StreamParams stream_params; |
| 237 | stream_params.add_ssrc(*encoding.ssrc); |
| 238 | if (encoding.rtx && encoding.rtx->ssrc) { |
| 239 | stream_params.AddFidSsrc(*encoding.ssrc, *encoding.rtx->ssrc); |
| 240 | } |
| 241 | cricket_streams.push_back(std::move(stream_params)); |
| 242 | } |
| 243 | return std::move(cricket_streams); |
| 244 | } |
| 245 | |
Danil Chapovalov | 00c7183 | 2018-06-15 15:58:38 +0200 | [diff] [blame] | 246 | absl::optional<RtcpFeedback> ToRtcpFeedback( |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 247 | const cricket::FeedbackParam& cricket_feedback) { |
| 248 | if (cricket_feedback.id() == cricket::kRtcpFbParamCcm) { |
| 249 | if (cricket_feedback.param() == cricket::kRtcpFbCcmParamFir) { |
Oskar Sundbom | ff610bd | 2017-11-16 10:57:44 +0100 | [diff] [blame] | 250 | return RtcpFeedback(RtcpFeedbackType::CCM, RtcpFeedbackMessageType::FIR); |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 251 | } else { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 252 | RTC_LOG(LS_WARNING) << "Unsupported parameter for CCM RTCP feedback: " |
| 253 | << cricket_feedback.param(); |
Danil Chapovalov | 00c7183 | 2018-06-15 15:58:38 +0200 | [diff] [blame] | 254 | return absl::nullopt; |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 255 | } |
| 256 | } else if (cricket_feedback.id() == cricket::kRtcpFbParamNack) { |
| 257 | if (cricket_feedback.param().empty()) { |
Oskar Sundbom | ff610bd | 2017-11-16 10:57:44 +0100 | [diff] [blame] | 258 | return RtcpFeedback(RtcpFeedbackType::NACK, |
| 259 | RtcpFeedbackMessageType::GENERIC_NACK); |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 260 | } else if (cricket_feedback.param() == cricket::kRtcpFbNackParamPli) { |
Oskar Sundbom | ff610bd | 2017-11-16 10:57:44 +0100 | [diff] [blame] | 261 | return RtcpFeedback(RtcpFeedbackType::NACK, RtcpFeedbackMessageType::PLI); |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 262 | } else { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 263 | RTC_LOG(LS_WARNING) << "Unsupported parameter for NACK RTCP feedback: " |
| 264 | << cricket_feedback.param(); |
Danil Chapovalov | 00c7183 | 2018-06-15 15:58:38 +0200 | [diff] [blame] | 265 | return absl::nullopt; |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 266 | } |
| 267 | } else if (cricket_feedback.id() == cricket::kRtcpFbParamRemb) { |
| 268 | if (!cricket_feedback.param().empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 269 | RTC_LOG(LS_WARNING) << "Unsupported parameter for REMB RTCP feedback: " |
| 270 | << cricket_feedback.param(); |
Danil Chapovalov | 00c7183 | 2018-06-15 15:58:38 +0200 | [diff] [blame] | 271 | return absl::nullopt; |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 272 | } else { |
Oskar Sundbom | ff610bd | 2017-11-16 10:57:44 +0100 | [diff] [blame] | 273 | return RtcpFeedback(RtcpFeedbackType::REMB); |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 274 | } |
| 275 | } else if (cricket_feedback.id() == cricket::kRtcpFbParamTransportCc) { |
| 276 | if (!cricket_feedback.param().empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 277 | RTC_LOG(LS_WARNING) |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 278 | << "Unsupported parameter for transport-cc RTCP feedback: " |
| 279 | << cricket_feedback.param(); |
Danil Chapovalov | 00c7183 | 2018-06-15 15:58:38 +0200 | [diff] [blame] | 280 | return absl::nullopt; |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 281 | } else { |
Oskar Sundbom | ff610bd | 2017-11-16 10:57:44 +0100 | [diff] [blame] | 282 | return RtcpFeedback(RtcpFeedbackType::TRANSPORT_CC); |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 283 | } |
| 284 | } |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 285 | RTC_LOG(LS_WARNING) << "Unsupported RTCP feedback type: " |
| 286 | << cricket_feedback.id(); |
Danil Chapovalov | 00c7183 | 2018-06-15 15:58:38 +0200 | [diff] [blame] | 287 | return absl::nullopt; |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 288 | } |
| 289 | |
zhihuang | 2436639 | 2017-03-08 17:15:06 -0800 | [diff] [blame] | 290 | std::vector<RtpEncodingParameters> ToRtpEncodings( |
| 291 | const cricket::StreamParamsVec& stream_params) { |
| 292 | std::vector<RtpEncodingParameters> rtp_encodings; |
| 293 | for (const cricket::StreamParams& stream_param : stream_params) { |
| 294 | RtpEncodingParameters rtp_encoding; |
| 295 | rtp_encoding.ssrc.emplace(stream_param.first_ssrc()); |
| 296 | uint32_t rtx_ssrc = 0; |
| 297 | if (stream_param.GetFidSsrc(stream_param.first_ssrc(), &rtx_ssrc)) { |
| 298 | RtpRtxParameters rtx_param(rtx_ssrc); |
| 299 | rtp_encoding.rtx.emplace(rtx_param); |
| 300 | } |
| 301 | rtp_encodings.push_back(std::move(rtp_encoding)); |
| 302 | } |
| 303 | return rtp_encodings; |
| 304 | } |
| 305 | |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 306 | template <typename C> |
| 307 | cricket::MediaType KindOfCodec(); |
| 308 | |
| 309 | template <> |
| 310 | cricket::MediaType KindOfCodec<cricket::AudioCodec>() { |
| 311 | return cricket::MEDIA_TYPE_AUDIO; |
| 312 | } |
| 313 | |
| 314 | template <> |
| 315 | cricket::MediaType KindOfCodec<cricket::VideoCodec>() { |
| 316 | return cricket::MEDIA_TYPE_VIDEO; |
| 317 | } |
| 318 | |
| 319 | template <typename C> |
| 320 | static void ToRtpCodecCapabilityTypeSpecific(const C& cricket_codec, |
| 321 | RtpCodecCapability* codec); |
| 322 | |
| 323 | template <> |
| 324 | void ToRtpCodecCapabilityTypeSpecific<cricket::AudioCodec>( |
| 325 | const cricket::AudioCodec& cricket_codec, |
| 326 | RtpCodecCapability* codec) { |
Oskar Sundbom | ff610bd | 2017-11-16 10:57:44 +0100 | [diff] [blame] | 327 | codec->num_channels = static_cast<int>(cricket_codec.channels); |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | template <> |
| 331 | void ToRtpCodecCapabilityTypeSpecific<cricket::VideoCodec>( |
| 332 | const cricket::VideoCodec& cricket_codec, |
| 333 | RtpCodecCapability* codec) {} |
| 334 | |
| 335 | template <typename C> |
| 336 | RtpCodecCapability ToRtpCodecCapability(const C& cricket_codec) { |
| 337 | RtpCodecCapability codec; |
| 338 | codec.name = cricket_codec.name; |
| 339 | codec.kind = KindOfCodec<C>(); |
| 340 | codec.clock_rate.emplace(cricket_codec.clockrate); |
| 341 | codec.preferred_payload_type.emplace(cricket_codec.id); |
| 342 | for (const cricket::FeedbackParam& cricket_feedback : |
| 343 | cricket_codec.feedback_params.params()) { |
Danil Chapovalov | 00c7183 | 2018-06-15 15:58:38 +0200 | [diff] [blame] | 344 | absl::optional<RtcpFeedback> feedback = ToRtcpFeedback(cricket_feedback); |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 345 | if (feedback) { |
Danil Chapovalov | 57ff273 | 2018-03-28 11:25:15 +0200 | [diff] [blame] | 346 | codec.rtcp_feedback.push_back(feedback.value()); |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 347 | } |
| 348 | } |
| 349 | ToRtpCodecCapabilityTypeSpecific(cricket_codec, &codec); |
| 350 | codec.parameters.insert(cricket_codec.params.begin(), |
| 351 | cricket_codec.params.end()); |
| 352 | return codec; |
| 353 | } |
| 354 | |
| 355 | template RtpCodecCapability ToRtpCodecCapability<cricket::AudioCodec>( |
| 356 | const cricket::AudioCodec& cricket_codec); |
| 357 | template RtpCodecCapability ToRtpCodecCapability<cricket::VideoCodec>( |
| 358 | const cricket::VideoCodec& cricket_codec); |
| 359 | |
zhihuang | 2436639 | 2017-03-08 17:15:06 -0800 | [diff] [blame] | 360 | template <typename C> |
| 361 | static void ToRtpCodecParametersTypeSpecific(const C& cricket_codec, |
| 362 | RtpCodecParameters* codec); |
| 363 | template <> |
| 364 | void ToRtpCodecParametersTypeSpecific<cricket::AudioCodec>( |
| 365 | const cricket::AudioCodec& cricket_codec, |
| 366 | RtpCodecParameters* codec) { |
Oskar Sundbom | ff610bd | 2017-11-16 10:57:44 +0100 | [diff] [blame] | 367 | codec->num_channels = static_cast<int>(cricket_codec.channels); |
zhihuang | 2436639 | 2017-03-08 17:15:06 -0800 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | template <> |
| 371 | void ToRtpCodecParametersTypeSpecific<cricket::VideoCodec>( |
| 372 | const cricket::VideoCodec& cricket_codec, |
| 373 | RtpCodecParameters* codec) {} |
| 374 | |
| 375 | template <typename C> |
| 376 | RtpCodecParameters ToRtpCodecParameters(const C& cricket_codec) { |
| 377 | RtpCodecParameters codec_param; |
| 378 | codec_param.name = cricket_codec.name; |
| 379 | codec_param.kind = KindOfCodec<C>(); |
| 380 | codec_param.clock_rate.emplace(cricket_codec.clockrate); |
| 381 | codec_param.payload_type = cricket_codec.id; |
| 382 | for (const cricket::FeedbackParam& cricket_feedback : |
| 383 | cricket_codec.feedback_params.params()) { |
Danil Chapovalov | 00c7183 | 2018-06-15 15:58:38 +0200 | [diff] [blame] | 384 | absl::optional<RtcpFeedback> feedback = ToRtcpFeedback(cricket_feedback); |
zhihuang | 2436639 | 2017-03-08 17:15:06 -0800 | [diff] [blame] | 385 | if (feedback) { |
Danil Chapovalov | 57ff273 | 2018-03-28 11:25:15 +0200 | [diff] [blame] | 386 | codec_param.rtcp_feedback.push_back(feedback.value()); |
zhihuang | 2436639 | 2017-03-08 17:15:06 -0800 | [diff] [blame] | 387 | } |
| 388 | } |
| 389 | ToRtpCodecParametersTypeSpecific(cricket_codec, &codec_param); |
| 390 | codec_param.parameters.insert(cricket_codec.params.begin(), |
| 391 | cricket_codec.params.end()); |
| 392 | return codec_param; |
| 393 | } |
| 394 | |
| 395 | template RtpCodecParameters ToRtpCodecParameters<cricket::AudioCodec>( |
| 396 | const cricket::AudioCodec& cricket_codec); |
| 397 | template RtpCodecParameters ToRtpCodecParameters<cricket::VideoCodec>( |
| 398 | const cricket::VideoCodec& cricket_codec); |
| 399 | |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 400 | template <class C> |
| 401 | RtpCapabilities ToRtpCapabilities( |
| 402 | const std::vector<C>& cricket_codecs, |
| 403 | const cricket::RtpHeaderExtensions& cricket_extensions) { |
| 404 | RtpCapabilities capabilities; |
| 405 | bool have_red = false; |
| 406 | bool have_ulpfec = false; |
| 407 | bool have_flexfec = false; |
Florent Castelli | 5473a45 | 2018-11-06 17:27:21 +0100 | [diff] [blame] | 408 | bool have_rtx = false; |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 409 | for (const C& cricket_codec : cricket_codecs) { |
| 410 | if (cricket_codec.name == cricket::kRedCodecName) { |
| 411 | have_red = true; |
| 412 | } else if (cricket_codec.name == cricket::kUlpfecCodecName) { |
| 413 | have_ulpfec = true; |
| 414 | } else if (cricket_codec.name == cricket::kFlexfecCodecName) { |
| 415 | have_flexfec = true; |
Florent Castelli | 5473a45 | 2018-11-06 17:27:21 +0100 | [diff] [blame] | 416 | } else if (cricket_codec.name == cricket::kRtxCodecName) { |
| 417 | if (have_rtx) { |
| 418 | // There should only be one RTX codec entry |
| 419 | continue; |
| 420 | } |
| 421 | have_rtx = true; |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 422 | } |
Florent Castelli | 5473a45 | 2018-11-06 17:27:21 +0100 | [diff] [blame] | 423 | auto codec_capability = ToRtpCodecCapability(cricket_codec); |
| 424 | if (cricket_codec.name == cricket::kRtxCodecName) { |
| 425 | // RTX codec should not have any parameter |
| 426 | codec_capability.parameters.clear(); |
| 427 | } |
| 428 | capabilities.codecs.push_back(codec_capability); |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 429 | } |
| 430 | for (const RtpExtension& cricket_extension : cricket_extensions) { |
| 431 | capabilities.header_extensions.emplace_back(cricket_extension.uri, |
| 432 | cricket_extension.id); |
| 433 | } |
| 434 | if (have_red) { |
| 435 | capabilities.fec.push_back(FecMechanism::RED); |
| 436 | } |
| 437 | if (have_red && have_ulpfec) { |
| 438 | capabilities.fec.push_back(FecMechanism::RED_AND_ULPFEC); |
| 439 | } |
| 440 | if (have_flexfec) { |
| 441 | capabilities.fec.push_back(FecMechanism::FLEXFEC); |
| 442 | } |
| 443 | return capabilities; |
| 444 | } |
| 445 | |
| 446 | template RtpCapabilities ToRtpCapabilities<cricket::AudioCodec>( |
| 447 | const std::vector<cricket::AudioCodec>& cricket_codecs, |
| 448 | const cricket::RtpHeaderExtensions& cricket_extensions); |
| 449 | template RtpCapabilities ToRtpCapabilities<cricket::VideoCodec>( |
| 450 | const std::vector<cricket::VideoCodec>& cricket_codecs, |
| 451 | const cricket::RtpHeaderExtensions& cricket_extensions); |
| 452 | |
zhihuang | 2436639 | 2017-03-08 17:15:06 -0800 | [diff] [blame] | 453 | template <class C> |
| 454 | RtpParameters ToRtpParameters( |
| 455 | const std::vector<C>& cricket_codecs, |
| 456 | const cricket::RtpHeaderExtensions& cricket_extensions, |
| 457 | const cricket::StreamParamsVec& stream_params) { |
| 458 | RtpParameters rtp_parameters; |
| 459 | for (const C& cricket_codec : cricket_codecs) { |
| 460 | rtp_parameters.codecs.push_back(ToRtpCodecParameters(cricket_codec)); |
| 461 | } |
| 462 | for (const RtpExtension& cricket_extension : cricket_extensions) { |
| 463 | rtp_parameters.header_extensions.emplace_back(cricket_extension.uri, |
| 464 | cricket_extension.id); |
| 465 | } |
| 466 | rtp_parameters.encodings = ToRtpEncodings(stream_params); |
| 467 | return rtp_parameters; |
| 468 | } |
| 469 | |
| 470 | template RtpParameters ToRtpParameters<cricket::AudioCodec>( |
| 471 | const std::vector<cricket::AudioCodec>& cricket_codecs, |
| 472 | const cricket::RtpHeaderExtensions& cricket_extensions, |
| 473 | const cricket::StreamParamsVec& stream_params); |
| 474 | template RtpParameters ToRtpParameters<cricket::VideoCodec>( |
| 475 | const std::vector<cricket::VideoCodec>& cricket_codecs, |
| 476 | const cricket::RtpHeaderExtensions& cricket_extensions, |
| 477 | const cricket::StreamParamsVec& stream_params); |
| 478 | |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 479 | } // namespace webrtc |