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 codecs don't use num_channels or clock_rate, but they should at least |
| 101 // be validated to ensure the application isn't trying to do something it |
| 102 // doesn't intend to. |
| 103 template <> |
| 104 RTCError ToCricketCodecTypeSpecific<cricket::VideoCodec>( |
| 105 const RtpCodecParameters& codec, |
| 106 cricket::VideoCodec*) { |
| 107 if (codec.kind != cricket::MEDIA_TYPE_VIDEO) { |
| 108 LOG_AND_RETURN_ERROR( |
| 109 RTCErrorType::INVALID_PARAMETER, |
| 110 "Can't use audio codec with video sender or receiver."); |
| 111 } |
| 112 if (codec.num_channels) { |
| 113 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 114 "Video codec shouldn't have num_channels."); |
| 115 } |
| 116 if (!codec.clock_rate) { |
| 117 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 118 "Missing codec clock rate."); |
| 119 } |
| 120 if (*codec.clock_rate != cricket::kVideoCodecClockrate) { |
| 121 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 122 "Video clock rate must be 90000."); |
| 123 } |
| 124 return RTCError::OK(); |
| 125 } |
| 126 |
| 127 template <typename C> |
| 128 RTCErrorOr<C> ToCricketCodec(const RtpCodecParameters& codec) { |
| 129 C cricket_codec; |
| 130 // Start with audio/video specific conversion. |
| 131 RTCError err = ToCricketCodecTypeSpecific(codec, &cricket_codec); |
| 132 if (!err.ok()) { |
| 133 return err; |
| 134 } |
| 135 cricket_codec.name = codec.name; |
| 136 if (!cricket::IsValidRtpPayloadType(codec.payload_type)) { |
| 137 std::ostringstream oss; |
| 138 oss << "Invalid payload type: " << codec.payload_type; |
| 139 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_RANGE, oss.str()); |
| 140 } |
| 141 cricket_codec.id = codec.payload_type; |
| 142 for (const RtcpFeedback& feedback : codec.rtcp_feedback) { |
| 143 auto result = ToFeedbackParam(feedback); |
| 144 if (!result.ok()) { |
| 145 return result.MoveError(); |
| 146 } |
| 147 cricket_codec.AddFeedbackParam(result.MoveValue()); |
| 148 } |
| 149 cricket_codec.params.insert(codec.parameters.begin(), codec.parameters.end()); |
| 150 return cricket_codec; |
| 151 } |
| 152 |
| 153 template RTCErrorOr<cricket::AudioCodec> ToCricketCodec( |
| 154 const RtpCodecParameters& codec); |
| 155 template RTCErrorOr<cricket::VideoCodec> ToCricketCodec( |
| 156 const RtpCodecParameters& codec); |
| 157 |
| 158 template <typename C> |
| 159 RTCErrorOr<std::vector<C>> ToCricketCodecs( |
| 160 const std::vector<RtpCodecParameters>& codecs) { |
| 161 std::vector<C> cricket_codecs; |
| 162 std::set<int> seen_payload_types; |
| 163 for (const RtpCodecParameters& codec : codecs) { |
| 164 auto result = ToCricketCodec<C>(codec); |
| 165 if (!result.ok()) { |
| 166 return result.MoveError(); |
| 167 } |
| 168 if (!seen_payload_types.insert(codec.payload_type).second) { |
| 169 std::ostringstream oss; |
| 170 oss << "Duplicate payload type: " << codec.payload_type; |
| 171 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, oss.str()); |
| 172 } |
| 173 cricket_codecs.push_back(result.MoveValue()); |
| 174 } |
| 175 return cricket_codecs; |
| 176 } |
| 177 |
| 178 template RTCErrorOr<std::vector<cricket::AudioCodec>> ToCricketCodecs< |
| 179 cricket::AudioCodec>(const std::vector<RtpCodecParameters>& codecs); |
| 180 |
| 181 template RTCErrorOr<std::vector<cricket::VideoCodec>> ToCricketCodecs< |
| 182 cricket::VideoCodec>(const std::vector<RtpCodecParameters>& codecs); |
| 183 |
| 184 RTCErrorOr<cricket::RtpHeaderExtensions> ToRtpHeaderExtensions( |
| 185 const std::vector<RtpHeaderExtensionParameters>& extensions) { |
| 186 cricket::RtpHeaderExtensions cricket_extensions; |
| 187 std::ostringstream err_writer; |
| 188 std::set<int> seen_header_extension_ids; |
| 189 for (const RtpHeaderExtensionParameters& extension : extensions) { |
| 190 if (extension.id < RtpHeaderExtensionParameters::kMinId || |
| 191 extension.id > RtpHeaderExtensionParameters::kMaxId) { |
| 192 err_writer << "Invalid header extension id: " << extension.id; |
| 193 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_RANGE, err_writer.str()); |
| 194 } |
| 195 if (!seen_header_extension_ids.insert(extension.id).second) { |
| 196 err_writer << "Duplicate header extension id: " << extension.id; |
| 197 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, err_writer.str()); |
| 198 } |
| 199 cricket_extensions.push_back(extension); |
| 200 } |
| 201 return cricket_extensions; |
| 202 } |
| 203 |
| 204 RTCErrorOr<cricket::StreamParamsVec> ToStreamParamsVec( |
| 205 const std::vector<RtpEncodingParameters>& encodings) { |
| 206 if (encodings.size() > 1u) { |
| 207 LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_PARAMETER, |
| 208 "ORTC API implementation doesn't currently " |
| 209 "support simulcast or layered encodings."); |
| 210 } else if (encodings.empty()) { |
| 211 return cricket::StreamParamsVec(); |
| 212 } |
| 213 cricket::StreamParamsVec cricket_streams; |
| 214 const RtpEncodingParameters& encoding = encodings[0]; |
| 215 if (encoding.rtx && encoding.rtx->ssrc && !encoding.ssrc) { |
| 216 LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_PARAMETER, |
| 217 "Setting an RTX SSRC explicitly while leaving the " |
| 218 "primary SSRC unset is not currently supported."); |
| 219 } |
| 220 if (encoding.ssrc) { |
| 221 cricket::StreamParams stream_params; |
| 222 stream_params.add_ssrc(*encoding.ssrc); |
| 223 if (encoding.rtx && encoding.rtx->ssrc) { |
| 224 stream_params.AddFidSsrc(*encoding.ssrc, *encoding.rtx->ssrc); |
| 225 } |
| 226 cricket_streams.push_back(std::move(stream_params)); |
| 227 } |
| 228 return cricket_streams; |
| 229 } |
| 230 |
| 231 rtc::Optional<RtcpFeedback> ToRtcpFeedback( |
| 232 const cricket::FeedbackParam& cricket_feedback) { |
| 233 if (cricket_feedback.id() == cricket::kRtcpFbParamCcm) { |
| 234 if (cricket_feedback.param() == cricket::kRtcpFbCcmParamFir) { |
| 235 return rtc::Optional<RtcpFeedback>( |
| 236 {RtcpFeedbackType::CCM, RtcpFeedbackMessageType::FIR}); |
| 237 } else { |
| 238 LOG(LS_WARNING) << "Unsupported parameter for CCM RTCP feedback: " |
| 239 << cricket_feedback.param(); |
| 240 return rtc::Optional<RtcpFeedback>(); |
| 241 } |
| 242 } else if (cricket_feedback.id() == cricket::kRtcpFbParamNack) { |
| 243 if (cricket_feedback.param().empty()) { |
| 244 return rtc::Optional<RtcpFeedback>( |
| 245 {RtcpFeedbackType::NACK, RtcpFeedbackMessageType::GENERIC_NACK}); |
| 246 } else if (cricket_feedback.param() == cricket::kRtcpFbNackParamPli) { |
| 247 return rtc::Optional<RtcpFeedback>( |
| 248 {RtcpFeedbackType::NACK, RtcpFeedbackMessageType::PLI}); |
| 249 } else { |
| 250 LOG(LS_WARNING) << "Unsupported parameter for NACK RTCP feedback: " |
| 251 << cricket_feedback.param(); |
| 252 return rtc::Optional<RtcpFeedback>(); |
| 253 } |
| 254 } else if (cricket_feedback.id() == cricket::kRtcpFbParamRemb) { |
| 255 if (!cricket_feedback.param().empty()) { |
| 256 LOG(LS_WARNING) << "Unsupported parameter for REMB RTCP feedback: " |
| 257 << cricket_feedback.param(); |
| 258 return rtc::Optional<RtcpFeedback>(); |
| 259 } else { |
| 260 return rtc::Optional<RtcpFeedback>(RtcpFeedback(RtcpFeedbackType::REMB)); |
| 261 } |
| 262 } else if (cricket_feedback.id() == cricket::kRtcpFbParamTransportCc) { |
| 263 if (!cricket_feedback.param().empty()) { |
| 264 LOG(LS_WARNING) |
| 265 << "Unsupported parameter for transport-cc RTCP feedback: " |
| 266 << cricket_feedback.param(); |
| 267 return rtc::Optional<RtcpFeedback>(); |
| 268 } else { |
| 269 return rtc::Optional<RtcpFeedback>( |
| 270 RtcpFeedback(RtcpFeedbackType::TRANSPORT_CC)); |
| 271 } |
| 272 } |
| 273 LOG(LS_WARNING) << "Unsupported RTCP feedback type: " |
| 274 << cricket_feedback.id(); |
| 275 return rtc::Optional<RtcpFeedback>(); |
| 276 } |
| 277 |
| 278 template <typename C> |
| 279 cricket::MediaType KindOfCodec(); |
| 280 |
| 281 template <> |
| 282 cricket::MediaType KindOfCodec<cricket::AudioCodec>() { |
| 283 return cricket::MEDIA_TYPE_AUDIO; |
| 284 } |
| 285 |
| 286 template <> |
| 287 cricket::MediaType KindOfCodec<cricket::VideoCodec>() { |
| 288 return cricket::MEDIA_TYPE_VIDEO; |
| 289 } |
| 290 |
| 291 template <typename C> |
| 292 static void ToRtpCodecCapabilityTypeSpecific(const C& cricket_codec, |
| 293 RtpCodecCapability* codec); |
| 294 |
| 295 template <> |
| 296 void ToRtpCodecCapabilityTypeSpecific<cricket::AudioCodec>( |
| 297 const cricket::AudioCodec& cricket_codec, |
| 298 RtpCodecCapability* codec) { |
| 299 codec->num_channels = rtc::Optional<int>(cricket_codec.channels); |
| 300 } |
| 301 |
| 302 template <> |
| 303 void ToRtpCodecCapabilityTypeSpecific<cricket::VideoCodec>( |
| 304 const cricket::VideoCodec& cricket_codec, |
| 305 RtpCodecCapability* codec) {} |
| 306 |
| 307 template <typename C> |
| 308 RtpCodecCapability ToRtpCodecCapability(const C& cricket_codec) { |
| 309 RtpCodecCapability codec; |
| 310 codec.name = cricket_codec.name; |
| 311 codec.kind = KindOfCodec<C>(); |
| 312 codec.clock_rate.emplace(cricket_codec.clockrate); |
| 313 codec.preferred_payload_type.emplace(cricket_codec.id); |
| 314 for (const cricket::FeedbackParam& cricket_feedback : |
| 315 cricket_codec.feedback_params.params()) { |
| 316 rtc::Optional<RtcpFeedback> feedback = ToRtcpFeedback(cricket_feedback); |
| 317 if (feedback) { |
| 318 codec.rtcp_feedback.push_back(feedback.MoveValue()); |
| 319 } |
| 320 } |
| 321 ToRtpCodecCapabilityTypeSpecific(cricket_codec, &codec); |
| 322 codec.parameters.insert(cricket_codec.params.begin(), |
| 323 cricket_codec.params.end()); |
| 324 return codec; |
| 325 } |
| 326 |
| 327 template RtpCodecCapability ToRtpCodecCapability<cricket::AudioCodec>( |
| 328 const cricket::AudioCodec& cricket_codec); |
| 329 template RtpCodecCapability ToRtpCodecCapability<cricket::VideoCodec>( |
| 330 const cricket::VideoCodec& cricket_codec); |
| 331 |
| 332 template <class C> |
| 333 RtpCapabilities ToRtpCapabilities( |
| 334 const std::vector<C>& cricket_codecs, |
| 335 const cricket::RtpHeaderExtensions& cricket_extensions) { |
| 336 RtpCapabilities capabilities; |
| 337 bool have_red = false; |
| 338 bool have_ulpfec = false; |
| 339 bool have_flexfec = false; |
| 340 for (const C& cricket_codec : cricket_codecs) { |
| 341 if (cricket_codec.name == cricket::kRedCodecName) { |
| 342 have_red = true; |
| 343 } else if (cricket_codec.name == cricket::kUlpfecCodecName) { |
| 344 have_ulpfec = true; |
| 345 } else if (cricket_codec.name == cricket::kFlexfecCodecName) { |
| 346 have_flexfec = true; |
| 347 } |
| 348 capabilities.codecs.push_back(ToRtpCodecCapability(cricket_codec)); |
| 349 } |
| 350 for (const RtpExtension& cricket_extension : cricket_extensions) { |
| 351 capabilities.header_extensions.emplace_back(cricket_extension.uri, |
| 352 cricket_extension.id); |
| 353 } |
| 354 if (have_red) { |
| 355 capabilities.fec.push_back(FecMechanism::RED); |
| 356 } |
| 357 if (have_red && have_ulpfec) { |
| 358 capabilities.fec.push_back(FecMechanism::RED_AND_ULPFEC); |
| 359 } |
| 360 if (have_flexfec) { |
| 361 capabilities.fec.push_back(FecMechanism::FLEXFEC); |
| 362 } |
| 363 return capabilities; |
| 364 } |
| 365 |
| 366 template RtpCapabilities ToRtpCapabilities<cricket::AudioCodec>( |
| 367 const std::vector<cricket::AudioCodec>& cricket_codecs, |
| 368 const cricket::RtpHeaderExtensions& cricket_extensions); |
| 369 template RtpCapabilities ToRtpCapabilities<cricket::VideoCodec>( |
| 370 const std::vector<cricket::VideoCodec>& cricket_codecs, |
| 371 const cricket::RtpHeaderExtensions& cricket_extensions); |
| 372 |
| 373 } // namespace webrtc |
OLD | NEW |