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