Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 11 #include "webrtc/ortc/rtpparametersconversion.h" | |
| 12 | |
| 13 #include <set> | |
| 14 #include <sstream> | |
| 15 #include <utility> | |
| 16 | |
| 17 #include "webrtc/media/base/rtputils.h" | |
| 18 | |
| 19 namespace webrtc { | |
| 20 | |
| 21 RTCErrorOr<cricket::FeedbackParam> ToFeedbackParam( | |
| 22 const RtcpFeedback& feedback) { | |
| 23 switch (feedback.type) { | |
| 24 case RtcpFeedbackType::CCM: | |
| 25 if (!feedback.message_type) { | |
| 26 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, | |
| 27 "Missing message type in CCM RtcpFeedback."); | |
| 28 } else if (*feedback.message_type != RtcpFeedbackMessageType::FIR) { | |
| 29 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, | |
| 30 "Invalid message type in CCM RtcpFeedback."); | |
| 31 } | |
| 32 return cricket::FeedbackParam(cricket::kRtcpFbParamCcm, | |
| 33 cricket::kRtcpFbCcmParamFir); | |
| 34 case RtcpFeedbackType::NACK: | |
| 35 if (!feedback.message_type) { | |
| 36 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, | |
| 37 "Missing message type in NACK RtcpFeedback."); | |
| 38 } | |
| 39 switch (*feedback.message_type) { | |
| 40 case RtcpFeedbackMessageType::GENERIC_NACK: | |
| 41 return cricket::FeedbackParam(cricket::kRtcpFbParamNack); | |
| 42 case RtcpFeedbackMessageType::PLI: | |
| 43 return cricket::FeedbackParam(cricket::kRtcpFbParamNack, | |
| 44 cricket::kRtcpFbNackParamPli); | |
| 45 default: | |
| 46 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, | |
| 47 "Invalid message type in NACK RtcpFeedback."); | |
| 48 } | |
| 49 case RtcpFeedbackType::REMB: | |
| 50 if (feedback.message_type) { | |
| 51 LOG_AND_RETURN_ERROR( | |
| 52 RTCErrorType::INVALID_PARAMETER, | |
| 53 "Didn't expect message type in REMB RtcpFeedback."); | |
| 54 } | |
| 55 return cricket::FeedbackParam(cricket::kRtcpFbParamRemb); | |
| 56 case RtcpFeedbackType::TRANSPORT_CC: | |
| 57 if (feedback.message_type) { | |
| 58 LOG_AND_RETURN_ERROR( | |
| 59 RTCErrorType::INVALID_PARAMETER, | |
| 60 "Didn't expect message type in transport-cc RtcpFeedback."); | |
| 61 } | |
| 62 return cricket::FeedbackParam(cricket::kRtcpFbParamTransportCc); | |
| 63 } | |
| 64 } | |
| 65 | |
| 66 template <typename C> | |
| 67 static RTCError ToCricketCodecTypeSpecific(const RtpCodecParameters& codec, | |
| 68 C* cricket_codec); | |
| 69 | |
| 70 template <> | |
| 71 RTCError ToCricketCodecTypeSpecific<cricket::AudioCodec>( | |
| 72 const RtpCodecParameters& codec, | |
| 73 cricket::AudioCodec* cricket_codec) { | |
| 74 if (codec.kind != cricket::MEDIA_TYPE_AUDIO) { | |
| 75 LOG_AND_RETURN_ERROR( | |
| 76 RTCErrorType::INVALID_PARAMETER, | |
| 77 "Can't use video codec with audio sender or receiver."); | |
| 78 } | |
| 79 if (!codec.num_channels) { | |
| 80 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, | |
| 81 "Missing number of channels for audio codec."); | |
| 82 } | |
| 83 if (*codec.num_channels <= 0) { | |
| 84 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_RANGE, | |
| 85 "Number of channels must be positive."); | |
| 86 } | |
| 87 cricket_codec->channels = *codec.num_channels; | |
| 88 if (!codec.clock_rate) { | |
| 89 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, | |
| 90 "Missing codec clock rate."); | |
| 91 } | |
| 92 if (*codec.clock_rate <= 0) { | |
| 93 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_RANGE, | |
| 94 "Clock rate must be positive."); | |
| 95 } | |
| 96 cricket_codec->clockrate = *codec.clock_rate; | |
| 97 return RTCError::OK(); | |
| 98 } | |
| 99 | |
| 100 // Video codec doesn't use num_channels or clock_rate, but they should at least | |
| 101 // be validated. | |
| 102 template <> | |
| 103 RTCError ToCricketCodecTypeSpecific<cricket::VideoCodec>( | |
| 104 const RtpCodecParameters& codec, | |
| 105 cricket::VideoCodec*) { | |
| 106 if (codec.kind != cricket::MEDIA_TYPE_VIDEO) { | |
| 107 LOG_AND_RETURN_ERROR( | |
| 108 RTCErrorType::INVALID_PARAMETER, | |
| 109 "Can't use audio codec with video sender or receiver."); | |
| 110 } | |
| 111 if (codec.num_channels) { | |
| 112 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, | |
| 113 "Video codec shouldn't have num_channels."); | |
| 114 } | |
| 115 if (!codec.clock_rate) { | |
| 116 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, | |
| 117 "Missing codec clock rate."); | |
| 118 } | |
| 119 if (*codec.clock_rate != cricket::kVideoCodecClockrate) { | |
| 120 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, | |
| 121 "Video clock rate must be 90000."); | |
| 122 } | |
| 123 return RTCError::OK(); | |
| 124 } | |
| 125 | |
| 126 template <typename C> | |
| 127 RTCErrorOr<C> ToCricketCodec(const RtpCodecParameters& codec) { | |
| 128 C cricket_codec; | |
| 129 // Start with audio/video specific conversion. | |
| 130 RTCError err = ToCricketCodecTypeSpecific(codec, &cricket_codec); | |
| 131 if (!err.ok()) { | |
| 132 return err; | |
| 133 } | |
| 134 cricket_codec.name = codec.name; | |
| 135 if (!cricket::IsValidRtpPayloadType(codec.payload_type)) { | |
| 136 std::ostringstream oss; | |
| 137 oss << "Invalid payload type: " << codec.payload_type; | |
| 138 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_RANGE, oss.str()); | |
| 139 } | |
| 140 cricket_codec.id = codec.payload_type; | |
| 141 for (const RtcpFeedback& feedback : codec.rtcp_feedback) { | |
| 142 auto result = ToFeedbackParam(feedback); | |
| 143 if (!result.ok()) { | |
| 144 return result.MoveError(); | |
| 145 } | |
| 146 cricket_codec.AddFeedbackParam(result.MoveValue()); | |
| 147 } | |
| 148 cricket_codec.params.insert(codec.parameters.begin(), codec.parameters.end()); | |
| 149 return cricket_codec; | |
| 150 } | |
| 151 | |
| 152 template RTCErrorOr<cricket::AudioCodec> ToCricketCodec( | |
| 153 const RtpCodecParameters& codec); | |
| 154 template RTCErrorOr<cricket::VideoCodec> ToCricketCodec( | |
| 155 const RtpCodecParameters& codec); | |
| 156 | |
| 157 template <typename C> | |
| 158 RTCErrorOr<std::vector<C>> ToCricketCodecs( | |
| 159 const std::vector<RtpCodecParameters>& codecs) { | |
| 160 std::vector<C> cricket_codecs; | |
| 161 std::set<int> seen_payload_types; | |
| 162 for (const RtpCodecParameters& codec : codecs) { | |
| 163 auto result = ToCricketCodec<C>(codec); | |
| 164 if (!result.ok()) { | |
| 165 return result.MoveError(); | |
| 166 } | |
| 167 if (!seen_payload_types.insert(codec.payload_type).second) { | |
| 168 std::ostringstream oss; | |
| 169 oss << "Duplicate payload type: " << codec.payload_type; | |
| 170 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, oss.str()); | |
| 171 } | |
| 172 cricket_codecs.push_back(result.MoveValue()); | |
| 173 } | |
| 174 return cricket_codecs; | |
| 175 } | |
| 176 | |
| 177 template RTCErrorOr<std::vector<cricket::AudioCodec>> ToCricketCodecs< | |
| 178 cricket::AudioCodec>(const std::vector<RtpCodecParameters>& codecs); | |
| 179 | |
| 180 template RTCErrorOr<std::vector<cricket::VideoCodec>> ToCricketCodecs< | |
| 181 cricket::VideoCodec>(const std::vector<RtpCodecParameters>& codecs); | |
| 182 | |
| 183 RTCErrorOr<cricket::RtpHeaderExtensions> ToRtpHeaderExtensions( | |
| 184 const std::vector<RtpHeaderExtensionParameters>& extensions) { | |
| 185 cricket::RtpHeaderExtensions cricket_extensions; | |
| 186 std::ostringstream err_writer; | |
| 187 std::set<int> seen_header_extension_ids; | |
| 188 for (const RtpHeaderExtensionParameters& extension : extensions) { | |
| 189 if (extension.id < 1 || extension.id > 14) { | |
|
pthatcher1
2017/02/17 23:10:22
These could use some name constants (kMinRtpHeader
Taylor Brandstetter
2017/02/17 23:48:03
Done.
| |
| 190 err_writer << "Invalid header extension id: " << extension.id; | |
| 191 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_RANGE, err_writer.str()); | |
| 192 } | |
| 193 if (!seen_header_extension_ids.insert(extension.id).second) { | |
| 194 err_writer << "Duplicate header extension id: " << extension.id; | |
| 195 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, err_writer.str()); | |
| 196 } | |
| 197 cricket_extensions.push_back(extension); | |
| 198 } | |
| 199 return cricket_extensions; | |
| 200 } | |
| 201 | |
| 202 RTCErrorOr<cricket::StreamParamsVec> ToStreamParamsVec( | |
| 203 const std::vector<RtpEncodingParameters>& encodings) { | |
| 204 if (encodings.size() > 1u) { | |
| 205 LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_PARAMETER, | |
| 206 "ORTC API implementation doesn't currently " | |
| 207 "support simulcast or layered encodings."); | |
| 208 } else if (encodings.empty()) { | |
| 209 return cricket::StreamParamsVec(); | |
| 210 } | |
| 211 cricket::StreamParamsVec cricket_streams; | |
| 212 const RtpEncodingParameters& encoding = encodings[0]; | |
| 213 if (encoding.ssrc) { | |
| 214 cricket::StreamParams stream_params; | |
| 215 stream_params.add_ssrc(*encoding.ssrc); | |
| 216 if (encoding.rtx && encoding.rtx->ssrc) { | |
| 217 stream_params.AddFidSsrc(*encoding.ssrc, *encoding.rtx->ssrc); | |
| 218 } | |
| 219 cricket_streams.push_back(std::move(stream_params)); | |
| 220 } | |
| 221 return cricket_streams; | |
| 222 } | |
| 223 | |
| 224 rtc::Optional<RtcpFeedback> ToRtcpFeedback( | |
| 225 const cricket::FeedbackParam& cricket_feedback) { | |
| 226 if (cricket_feedback.id() == cricket::kRtcpFbParamCcm) { | |
| 227 if (cricket_feedback.param() == cricket::kRtcpFbCcmParamFir) { | |
| 228 return rtc::Optional<RtcpFeedback>( | |
| 229 {RtcpFeedbackType::CCM, RtcpFeedbackMessageType::FIR}); | |
| 230 } else { | |
| 231 LOG(LS_WARNING) << "Unsupported parameter for CCM RTCP feedback: " | |
| 232 << cricket_feedback.param(); | |
| 233 return rtc::Optional<RtcpFeedback>(); | |
| 234 } | |
| 235 } else if (cricket_feedback.id() == cricket::kRtcpFbParamNack) { | |
| 236 if (cricket_feedback.param().empty()) { | |
| 237 return rtc::Optional<RtcpFeedback>( | |
| 238 {RtcpFeedbackType::NACK, RtcpFeedbackMessageType::GENERIC_NACK}); | |
| 239 } else if (cricket_feedback.param() == cricket::kRtcpFbNackParamPli) { | |
| 240 return rtc::Optional<RtcpFeedback>( | |
| 241 {RtcpFeedbackType::NACK, RtcpFeedbackMessageType::PLI}); | |
| 242 } else { | |
| 243 LOG(LS_WARNING) << "Unsupported parameter for NACK RTCP feedback: " | |
| 244 << cricket_feedback.param(); | |
| 245 return rtc::Optional<RtcpFeedback>(); | |
| 246 } | |
| 247 } else if (cricket_feedback.id() == cricket::kRtcpFbParamRemb) { | |
| 248 if (!cricket_feedback.param().empty()) { | |
| 249 LOG(LS_WARNING) << "Unsupported parameter for REMB RTCP feedback: " | |
| 250 << cricket_feedback.param(); | |
| 251 return rtc::Optional<RtcpFeedback>(); | |
| 252 } else { | |
| 253 return rtc::Optional<RtcpFeedback>(RtcpFeedback(RtcpFeedbackType::REMB)); | |
| 254 } | |
| 255 } else if (cricket_feedback.id() == cricket::kRtcpFbParamTransportCc) { | |
| 256 if (!cricket_feedback.param().empty()) { | |
| 257 LOG(LS_WARNING) | |
| 258 << "Unsupported parameter for transport-cc RTCP feedback: " | |
| 259 << cricket_feedback.param(); | |
| 260 return rtc::Optional<RtcpFeedback>(); | |
| 261 } else { | |
| 262 return rtc::Optional<RtcpFeedback>( | |
| 263 RtcpFeedback(RtcpFeedbackType::TRANSPORT_CC)); | |
| 264 } | |
| 265 } | |
| 266 LOG(LS_WARNING) << "Unsupported RTCP feedback type: " | |
| 267 << cricket_feedback.id(); | |
| 268 return rtc::Optional<RtcpFeedback>(); | |
| 269 } | |
| 270 | |
| 271 template <typename C> | |
| 272 cricket::MediaType KindOfCodec(); | |
| 273 | |
| 274 template <> | |
| 275 cricket::MediaType KindOfCodec<cricket::AudioCodec>() { | |
| 276 return cricket::MEDIA_TYPE_AUDIO; | |
| 277 } | |
| 278 | |
| 279 template <> | |
| 280 cricket::MediaType KindOfCodec<cricket::VideoCodec>() { | |
| 281 return cricket::MEDIA_TYPE_VIDEO; | |
| 282 } | |
| 283 | |
| 284 template <typename C> | |
| 285 static void ToRtpCodecCapabilityTypeSpecific(const C& cricket_codec, | |
| 286 RtpCodecCapability* codec); | |
| 287 | |
| 288 template <> | |
| 289 void ToRtpCodecCapabilityTypeSpecific<cricket::AudioCodec>( | |
| 290 const cricket::AudioCodec& cricket_codec, | |
| 291 RtpCodecCapability* codec) { | |
| 292 codec->num_channels = rtc::Optional<int>(cricket_codec.channels); | |
| 293 } | |
| 294 | |
| 295 template <> | |
| 296 void ToRtpCodecCapabilityTypeSpecific<cricket::VideoCodec>( | |
| 297 const cricket::VideoCodec& cricket_codec, | |
| 298 RtpCodecCapability* codec) {} | |
| 299 | |
| 300 template <typename C> | |
| 301 RtpCodecCapability ToRtpCodecCapability(const C& cricket_codec) { | |
| 302 RtpCodecCapability codec; | |
| 303 codec.name = cricket_codec.name; | |
| 304 codec.kind = KindOfCodec<C>(); | |
| 305 codec.clock_rate.emplace(cricket_codec.clockrate); | |
| 306 codec.preferred_payload_type.emplace(cricket_codec.id); | |
| 307 for (const cricket::FeedbackParam& cricket_feedback : | |
| 308 cricket_codec.feedback_params.params()) { | |
| 309 rtc::Optional<RtcpFeedback> feedback = ToRtcpFeedback(cricket_feedback); | |
| 310 if (feedback) { | |
| 311 codec.rtcp_feedback.push_back(feedback.MoveValue()); | |
| 312 } | |
| 313 } | |
| 314 ToRtpCodecCapabilityTypeSpecific(cricket_codec, &codec); | |
| 315 codec.parameters.insert(cricket_codec.params.begin(), | |
| 316 cricket_codec.params.end()); | |
| 317 return codec; | |
| 318 } | |
| 319 | |
| 320 template RtpCodecCapability ToRtpCodecCapability<cricket::AudioCodec>( | |
| 321 const cricket::AudioCodec& cricket_codec); | |
| 322 template RtpCodecCapability ToRtpCodecCapability<cricket::VideoCodec>( | |
| 323 const cricket::VideoCodec& cricket_codec); | |
| 324 | |
| 325 template <class C> | |
| 326 RtpCapabilities ToRtpCapabilities( | |
| 327 const std::vector<C>& cricket_codecs, | |
| 328 const cricket::RtpHeaderExtensions& cricket_extensions) { | |
| 329 RtpCapabilities capabilities; | |
| 330 bool have_red = false; | |
| 331 bool have_ulpfec = false; | |
| 332 bool have_flexfec = false; | |
| 333 for (const C& cricket_codec : cricket_codecs) { | |
| 334 if (cricket_codec.name == cricket::kRedCodecName) { | |
| 335 have_red = true; | |
| 336 } else if (cricket_codec.name == cricket::kUlpfecCodecName) { | |
| 337 have_ulpfec = true; | |
| 338 } else if (cricket_codec.name == cricket::kFlexfecCodecName) { | |
| 339 have_flexfec = true; | |
| 340 } | |
| 341 capabilities.codecs.push_back(ToRtpCodecCapability(cricket_codec)); | |
| 342 } | |
| 343 for (const RtpExtension& cricket_extension : cricket_extensions) { | |
| 344 capabilities.header_extensions.emplace_back(cricket_extension.uri, | |
| 345 cricket_extension.id); | |
| 346 } | |
| 347 if (have_red) { | |
| 348 capabilities.fec.push_back(FecMechanism::RED); | |
| 349 } | |
| 350 if (have_red && have_ulpfec) { | |
| 351 capabilities.fec.push_back(FecMechanism::RED_AND_ULPFEC); | |
| 352 } | |
| 353 if (have_flexfec) { | |
| 354 capabilities.fec.push_back(FecMechanism::FLEXFEC); | |
| 355 } | |
| 356 return capabilities; | |
| 357 } | |
| 358 | |
| 359 template RtpCapabilities ToRtpCapabilities<cricket::AudioCodec>( | |
| 360 const std::vector<cricket::AudioCodec>& cricket_codecs, | |
| 361 const cricket::RtpHeaderExtensions& cricket_extensions); | |
| 362 template RtpCapabilities ToRtpCapabilities<cricket::VideoCodec>( | |
| 363 const std::vector<cricket::VideoCodec>& cricket_codecs, | |
| 364 const cricket::RtpHeaderExtensions& cricket_extensions); | |
| 365 | |
| 366 } // namespace webrtc | |
| OLD | NEW |