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/rtptransportcontrollershim.h" |
| 12 |
| 13 #include <algorithm> // For "remove", "find". |
| 14 #include <set> |
| 15 #include <utility> // For std::move. |
| 16 |
| 17 #include "webrtc/api/proxy.h" |
| 18 #include "webrtc/base/checks.h" |
| 19 #include "webrtc/ortc/rtptransportshim.h" |
| 20 |
| 21 namespace { |
| 22 |
| 23 static const int kVideoClockrate = 90000; |
| 24 |
| 25 // Returns false on invalid input. Certain message types are only valid for |
| 26 // certain feedback types. |
| 27 webrtc::RTCError ValidateAndConvertRtcpFeedback( |
| 28 const webrtc::RtcpFeedback& feedback, |
| 29 cricket::Codec* codec) { |
| 30 switch (feedback.type) { |
| 31 case webrtc::RtcpFeedbackType::ACK: |
| 32 if (feedback.message_type) { |
| 33 return CreateAndLogError( |
| 34 webrtc::RTCErrorType::INVALID_PARAMETER, |
| 35 "Didn't expect message type in ACK RtcpFeedback."); |
| 36 } |
| 37 codec->AddFeedbackParam(cricket::FeedbackParam("ack")); |
| 38 return webrtc::RTCError(); |
| 39 case webrtc::RtcpFeedbackType::CCM: |
| 40 if (!feedback.message_type) { |
| 41 return CreateAndLogError(webrtc::RTCErrorType::INVALID_PARAMETER, |
| 42 "Missing message type in CCM RtcpFeedback."); |
| 43 } else if (*feedback.message_type != |
| 44 webrtc::RtcpFeedbackMessageType::FIR) { |
| 45 return CreateAndLogError(webrtc::RTCErrorType::INVALID_PARAMETER, |
| 46 "Invalid message type in CCM RtcpFeedback."); |
| 47 } |
| 48 codec->AddFeedbackParam(cricket::FeedbackParam("ccm", "fir")); |
| 49 return webrtc::RTCError(); |
| 50 case webrtc::RtcpFeedbackType::NACK: |
| 51 if (!feedback.message_type) { |
| 52 return CreateAndLogError(webrtc::RTCErrorType::INVALID_PARAMETER, |
| 53 "Missing message type in NACK RtcpFeedback."); |
| 54 } |
| 55 switch (*feedback.message_type) { |
| 56 case webrtc::RtcpFeedbackMessageType::GENERIC_NACK: |
| 57 codec->AddFeedbackParam(cricket::FeedbackParam("nack")); |
| 58 return webrtc::RTCError(); |
| 59 case webrtc::RtcpFeedbackMessageType::PLI: |
| 60 codec->AddFeedbackParam(cricket::FeedbackParam("nack", "pli")); |
| 61 return webrtc::RTCError(); |
| 62 default: |
| 63 return CreateAndLogError( |
| 64 webrtc::RTCErrorType::INVALID_PARAMETER, |
| 65 "Invalid message type in NACK RtcpFeedback."); |
| 66 } |
| 67 case webrtc::RtcpFeedbackType::REMB: |
| 68 if (feedback.message_type) { |
| 69 return CreateAndLogError( |
| 70 webrtc::RTCErrorType::INVALID_PARAMETER, |
| 71 "Didn't expect message type in REMB RtcpFeedback."); |
| 72 } |
| 73 codec->AddFeedbackParam(cricket::FeedbackParam("goog-remb")); |
| 74 return webrtc::RTCError(); |
| 75 case webrtc::RtcpFeedbackType::TRANSPORT_CC: |
| 76 if (feedback.message_type) { |
| 77 return CreateAndLogError( |
| 78 webrtc::RTCErrorType::INVALID_PARAMETER, |
| 79 "Didn't expect message type in transport-cc RtcpFeedback."); |
| 80 } |
| 81 codec->AddFeedbackParam(cricket::FeedbackParam("transport-cc")); |
| 82 return webrtc::RTCError(); |
| 83 } |
| 84 } |
| 85 |
| 86 template <typename C> |
| 87 webrtc::RTCError CodecSpecificConversion( |
| 88 const webrtc::RtpCodecParameters& codec, |
| 89 C* cricket_codec) {} |
| 90 |
| 91 template <> |
| 92 webrtc::RTCError CodecSpecificConversion<cricket::AudioCodec>( |
| 93 const webrtc::RtpCodecParameters& codec, |
| 94 cricket::AudioCodec* cricket_codec) { |
| 95 if (codec.kind != cricket::MEDIA_TYPE_AUDIO) { |
| 96 return CreateAndLogError( |
| 97 webrtc::RTCErrorType::INVALID_PARAMETER, |
| 98 "Can't use video codec with audio sender or receiver."); |
| 99 } |
| 100 if (!codec.num_channels) { |
| 101 // A default value for number of channels should have been filled already. |
| 102 RTC_NOTREACHED(); |
| 103 return CreateAndLogError(webrtc::RTCErrorType::INVALID_PARAMETER, |
| 104 "Missing number of channels for audio codec."); |
| 105 } |
| 106 if (*codec.num_channels <= 0) { |
| 107 return CreateAndLogError(webrtc::RTCErrorType::INVALID_RANGE, |
| 108 "Number of channels must be positive."); |
| 109 } |
| 110 cricket_codec->channels = *codec.num_channels; |
| 111 if (!codec.clock_rate) { |
| 112 return CreateAndLogError(webrtc::RTCErrorType::INVALID_PARAMETER, |
| 113 "Missing codec clock rate."); |
| 114 } |
| 115 if (*codec.clock_rate <= 0) { |
| 116 return CreateAndLogError(webrtc::RTCErrorType::INVALID_RANGE, |
| 117 "Clock rate must be positive."); |
| 118 } |
| 119 cricket_codec->clockrate = *codec.clock_rate; |
| 120 return webrtc::RTCError(); |
| 121 } |
| 122 |
| 123 // Video codec doesn't use num_channels or clock_rate, but they should at least |
| 124 // be validated. |
| 125 template <> |
| 126 webrtc::RTCError CodecSpecificConversion<cricket::VideoCodec>( |
| 127 const webrtc::RtpCodecParameters& codec, |
| 128 cricket::VideoCodec*) { |
| 129 if (codec.kind != cricket::MEDIA_TYPE_VIDEO) { |
| 130 return CreateAndLogError( |
| 131 webrtc::RTCErrorType::INVALID_PARAMETER, |
| 132 "Can't use video codec with video sender or receiver."); |
| 133 } |
| 134 if (codec.num_channels) { |
| 135 return CreateAndLogError(webrtc::RTCErrorType::INVALID_PARAMETER, |
| 136 "Video codec shouldn't have num_channels."); |
| 137 } |
| 138 if (!codec.clock_rate) { |
| 139 // A default value should have been filled already. |
| 140 RTC_NOTREACHED(); |
| 141 return CreateAndLogError(webrtc::RTCErrorType::INVALID_PARAMETER, |
| 142 "Missing codec clock rate."); |
| 143 } |
| 144 if (*codec.clock_rate != kVideoClockrate) { |
| 145 return CreateAndLogError(webrtc::RTCErrorType::INVALID_PARAMETER, |
| 146 "Video clock rate must be 90000."); |
| 147 } |
| 148 return webrtc::RTCError(); |
| 149 } |
| 150 |
| 151 template <typename C> |
| 152 webrtc::RTCError ValidateAndConvertCodecs( |
| 153 const std::vector<webrtc::RtpCodecParameters>& codecs, |
| 154 std::vector<C>* cricket_codecs) { |
| 155 std::ostringstream err_writer; |
| 156 std::set<int> seen_payload_types; |
| 157 for (const webrtc::RtpCodecParameters& codec : codecs) { |
| 158 C cricket_codec; |
| 159 // Start with audio/video specific conversion. |
| 160 webrtc::RTCError err = CodecSpecificConversion(codec, &cricket_codec); |
| 161 if (!err.ok()) { |
| 162 return err; |
| 163 } |
| 164 cricket_codec.name = codec.name; |
| 165 if (codec.payload_type < 0 || codec.payload_type > 127) { |
| 166 err_writer << "Invalid payload type: " << codec.payload_type; |
| 167 return CreateAndLogError(webrtc::RTCErrorType::INVALID_RANGE, |
| 168 err_writer.str()); |
| 169 } |
| 170 if (!seen_payload_types.insert(codec.payload_type).second) { |
| 171 err_writer << "Duplicate payload type: " << codec.payload_type; |
| 172 return CreateAndLogError(webrtc::RTCErrorType::INVALID_PARAMETER, |
| 173 err_writer.str()); |
| 174 } |
| 175 cricket_codec.id = codec.payload_type; |
| 176 for (const webrtc::RtcpFeedback& feedback : codec.rtcp_feedback) { |
| 177 webrtc::RTCError err = |
| 178 ValidateAndConvertRtcpFeedback(feedback, &cricket_codec); |
| 179 if (!err.ok()) { |
| 180 return err; |
| 181 } |
| 182 } |
| 183 cricket_codec.params.insert(codec.parameters.begin(), |
| 184 codec.parameters.end()); |
| 185 cricket_codecs->push_back(std::move(cricket_codec)); |
| 186 } |
| 187 return webrtc::RTCError(); |
| 188 } |
| 189 |
| 190 webrtc::RTCError ValidateAndConvertHeaderExtensions( |
| 191 const std::vector<webrtc::RtpHeaderExtensionParameters>& extensions, |
| 192 cricket::RtpHeaderExtensions* cricket_extensions) { |
| 193 std::ostringstream err_writer; |
| 194 std::set<int> seen_header_extension_ids; |
| 195 for (const webrtc::RtpHeaderExtensionParameters& extension : extensions) { |
| 196 if (extension.id < 1 || extension.id > 14) { |
| 197 err_writer << "Invalid header extension id: " << extension.id; |
| 198 return CreateAndLogError(webrtc::RTCErrorType::INVALID_RANGE, |
| 199 err_writer.str()); |
| 200 } |
| 201 if (!seen_header_extension_ids.insert(extension.id).second) { |
| 202 err_writer << "Duplicate header extension id: " << extension.id; |
| 203 return CreateAndLogError(webrtc::RTCErrorType::INVALID_PARAMETER, |
| 204 err_writer.str()); |
| 205 } |
| 206 cricket_extensions->emplace_back(extension.uri, extension.id); |
| 207 } |
| 208 return webrtc::RTCError(); |
| 209 } |
| 210 |
| 211 // Missing SSRC is treated differently for receiver encodings; this means |
| 212 // SSRCs are unsignaled. |
| 213 webrtc::RTCError ValidateAndConvertReceiverEncodings( |
| 214 const std::vector<webrtc::RtpEncodingParameters> encodings, |
| 215 cricket::StreamParamsVec* cricket_streams, |
| 216 bool* receiving) { |
| 217 if (encodings.size() > 1u) { |
| 218 return CreateAndLogError(webrtc::RTCErrorType::UNSUPPORTED_PARAMETER, |
| 219 "ORTC API implementation doesn't currently " |
| 220 "support simulcast or layered encodings."); |
| 221 } |
| 222 if (encodings.size() == 1u) { |
| 223 const webrtc::RtpEncodingParameters& encoding = encodings[0]; |
| 224 *receiving = encoding.active; |
| 225 if (encoding.ssrc) { |
| 226 cricket::StreamParams stream_params; |
| 227 stream_params.add_ssrc(*encoding.ssrc); |
| 228 if (encoding.rtx && encoding.rtx->ssrc) { |
| 229 stream_params.AddFidSsrc(*encoding.ssrc, *encoding.rtx->ssrc); |
| 230 } |
| 231 cricket_streams->push_back(std::move(stream_params)); |
| 232 } |
| 233 } else { |
| 234 *receiving = false; |
| 235 } |
| 236 return webrtc::RTCError(); |
| 237 } |
| 238 |
| 239 } // namespace |
| 240 |
| 241 namespace webrtc { |
| 242 |
| 243 BEGIN_OWNED_PROXY_MAP(RtpTransportController) |
| 244 PROXY_SIGNALING_THREAD_DESTRUCTOR() |
| 245 PROXY_CONSTMETHOD0(std::vector<RtpTransportInterface*>, GetTransports) |
| 246 protected: |
| 247 RtpTransportControllerShim* GetInternal() override { |
| 248 return internal(); |
| 249 } |
| 250 END_PROXY_MAP() |
| 251 |
| 252 // static |
| 253 std::unique_ptr<RtpTransportControllerInterface> |
| 254 RtpTransportControllerShim::CreateProxied( |
| 255 const cricket::MediaConfig& config, |
| 256 cricket::ChannelManager* channel_manager, |
| 257 webrtc::RtcEventLog* event_log, |
| 258 rtc::Thread* signaling_thread, |
| 259 rtc::Thread* worker_thread) { |
| 260 return RtpTransportControllerProxyWithInternal< |
| 261 RtpTransportControllerShim>::Create(signaling_thread, worker_thread, |
| 262 new RtpTransportControllerShim( |
| 263 config, channel_manager, |
| 264 event_log, signaling_thread, |
| 265 worker_thread)); |
| 266 } |
| 267 |
| 268 RtpTransportControllerShim::~RtpTransportControllerShim() { |
| 269 RTC_DCHECK_RUN_ON(signaling_thread_); |
| 270 if (!transport_proxies_.empty()) { |
| 271 LOG(LS_ERROR) |
| 272 << "Destroying RtpTransportControllerShim while RtpTransports " |
| 273 "are still using it; this is unsafe."; |
| 274 } |
| 275 if (voice_channel_) { |
| 276 // This would mean audio RTP senders/receivers are still using us. |
| 277 DestroyVoiceChannel(); |
| 278 } |
| 279 if (voice_channel_) { |
| 280 // This would mean video RTP senders/receivers are still using us. |
| 281 DestroyVideoChannel(); |
| 282 } |
| 283 } |
| 284 |
| 285 std::vector<RtpTransportInterface*> RtpTransportControllerShim::GetTransports() |
| 286 const { |
| 287 RTC_DCHECK_RUN_ON(signaling_thread_); |
| 288 return transport_proxies_; |
| 289 } |
| 290 |
| 291 void RtpTransportControllerShim::AddTransport( |
| 292 RtpTransportInterface* transport_proxy) { |
| 293 RTC_DCHECK_RUN_ON(signaling_thread_); |
| 294 transport_proxies_.push_back(transport_proxy); |
| 295 } |
| 296 |
| 297 void RtpTransportControllerShim::RemoveTransport( |
| 298 RtpTransportInterface* inner_transport) { |
| 299 RTC_DCHECK_RUN_ON(signaling_thread_); |
| 300 auto it = std::find_if(transport_proxies_.begin(), transport_proxies_.end(), |
| 301 [inner_transport](RtpTransportInterface* proxy) { |
| 302 return proxy->GetInternal() == inner_transport; |
| 303 }); |
| 304 if (it == transport_proxies_.end()) { |
| 305 RTC_NOTREACHED(); |
| 306 return; |
| 307 } |
| 308 transport_proxies_.erase(it); |
| 309 } |
| 310 |
| 311 RTCError RtpTransportControllerShim::SetRtcpParameters( |
| 312 const RtcpParameters& parameters, |
| 313 RtpTransportInterface* inner_transport) { |
| 314 if (inner_transport == inner_audio_transport_) { |
| 315 CopyRtcpParametersToDescriptions(parameters, &local_audio_description_, |
| 316 &remote_audio_description_); |
| 317 if (!voice_channel_->SetLocalContent(&local_audio_description_, |
| 318 cricket::CA_OFFER, nullptr)) { |
| 319 return CreateAndLogError(RTCErrorType::INTERNAL_ERROR, |
| 320 "Failed to apply new RTCP parameters."); |
| 321 } |
| 322 if (!voice_channel_->SetRemoteContent(&remote_audio_description_, |
| 323 cricket::CA_ANSWER, nullptr)) { |
| 324 return CreateAndLogError(RTCErrorType::INTERNAL_ERROR, |
| 325 "Failed to apply new RTCP parameters."); |
| 326 } |
| 327 } else if (inner_transport == inner_video_transport_) { |
| 328 CopyRtcpParametersToDescriptions(parameters, &local_video_description_, |
| 329 &remote_video_description_); |
| 330 if (!video_channel_->SetLocalContent(&local_video_description_, |
| 331 cricket::CA_OFFER, nullptr)) { |
| 332 return CreateAndLogError(RTCErrorType::INTERNAL_ERROR, |
| 333 "Failed to apply new RTCP parameters."); |
| 334 } |
| 335 if (!video_channel_->SetRemoteContent(&remote_video_description_, |
| 336 cricket::CA_ANSWER, nullptr)) { |
| 337 return CreateAndLogError(RTCErrorType::INTERNAL_ERROR, |
| 338 "Failed to apply new RTCP parameters."); |
| 339 } |
| 340 } |
| 341 return RTCError(); |
| 342 } |
| 343 |
| 344 RTCError RtpTransportControllerShim::AttachAudioSender( |
| 345 RtpTransportInterface* inner_transport) { |
| 346 if (have_audio_sender_) { |
| 347 return CreateAndLogError(RTCErrorType::UNSUPPORTED_OPERATION, |
| 348 "Using two RtpSenders with the same " |
| 349 "RtpTransportControllerShim is not currently " |
| 350 "supported."); |
| 351 } |
| 352 if (inner_audio_transport_ && inner_audio_transport_ != inner_transport) { |
| 353 return CreateAndLogError(RTCErrorType::UNSUPPORTED_PARAMETER, |
| 354 "Using different transports for the audio " |
| 355 "RtpSender and RtpReceiver is not currently " |
| 356 "supported."); |
| 357 } |
| 358 // If setting new transport, extract its RTCP parameters and create voice |
| 359 // channel. |
| 360 if (!inner_audio_transport_) { |
| 361 CopyRtcpParametersToDescriptions(inner_transport->GetRtcpParameters(), |
| 362 &local_audio_description_, |
| 363 &remote_audio_description_); |
| 364 inner_audio_transport_ = inner_transport; |
| 365 CreateVoiceChannel(); |
| 366 } |
| 367 have_audio_sender_ = true; |
| 368 return RTCError(); |
| 369 } |
| 370 |
| 371 RTCError RtpTransportControllerShim::AttachVideoSender( |
| 372 RtpTransportInterface* inner_transport) { |
| 373 if (have_video_sender_) { |
| 374 return CreateAndLogError(RTCErrorType::UNSUPPORTED_OPERATION, |
| 375 "Using two RtpSenders with the same " |
| 376 "RtpTransportControllerShim is not currently " |
| 377 "supported."); |
| 378 } |
| 379 if (inner_video_transport_ && inner_video_transport_ != inner_transport) { |
| 380 return CreateAndLogError(RTCErrorType::UNSUPPORTED_PARAMETER, |
| 381 "Using different transports for the video " |
| 382 "RtpSender and RtpReceiver is not currently " |
| 383 "supported."); |
| 384 } |
| 385 // If setting new transport, extract its RTCP parameters and create video |
| 386 // channel. |
| 387 if (!inner_video_transport_) { |
| 388 CopyRtcpParametersToDescriptions(inner_transport->GetRtcpParameters(), |
| 389 &local_video_description_, |
| 390 &remote_video_description_); |
| 391 inner_video_transport_ = inner_transport; |
| 392 CreateVideoChannel(); |
| 393 } |
| 394 have_video_sender_ = true; |
| 395 return RTCError(); |
| 396 } |
| 397 |
| 398 RTCError RtpTransportControllerShim::AttachAudioReceiver( |
| 399 RtpTransportInterface* inner_transport) { |
| 400 if (have_audio_receiver_) { |
| 401 return CreateAndLogError(RTCErrorType::UNSUPPORTED_OPERATION, |
| 402 "Using two RtpReceivers with the same " |
| 403 "RtpTransportControllerShim is not currently " |
| 404 "supported."); |
| 405 } |
| 406 if (inner_audio_transport_ && inner_audio_transport_ != inner_transport) { |
| 407 return CreateAndLogError(RTCErrorType::UNSUPPORTED_PARAMETER, |
| 408 "Using different transports for the audio " |
| 409 "RtpReceiver and RtpReceiver is not currently " |
| 410 "supported."); |
| 411 } |
| 412 // If setting new transport, extract its RTCP parameters and create voice |
| 413 // channel. |
| 414 if (!inner_audio_transport_) { |
| 415 CopyRtcpParametersToDescriptions(inner_transport->GetRtcpParameters(), |
| 416 &local_audio_description_, |
| 417 &remote_audio_description_); |
| 418 inner_audio_transport_ = inner_transport; |
| 419 CreateVoiceChannel(); |
| 420 } |
| 421 have_audio_receiver_ = true; |
| 422 return RTCError(); |
| 423 } |
| 424 |
| 425 RTCError RtpTransportControllerShim::AttachVideoReceiver( |
| 426 RtpTransportInterface* inner_transport) { |
| 427 if (have_video_receiver_) { |
| 428 return CreateAndLogError(RTCErrorType::UNSUPPORTED_OPERATION, |
| 429 "Using two RtpReceivers with the same " |
| 430 "RtpTransportControllerShim is not currently " |
| 431 "supported."); |
| 432 } |
| 433 if (inner_video_transport_ && inner_video_transport_ != inner_transport) { |
| 434 return CreateAndLogError(RTCErrorType::UNSUPPORTED_PARAMETER, |
| 435 "Using different transports for the video " |
| 436 "RtpReceiver and RtpReceiver is not currently " |
| 437 "supported."); |
| 438 } |
| 439 // If setting new transport, extract its RTCP parameters and create video |
| 440 // channel. |
| 441 if (!inner_video_transport_) { |
| 442 CopyRtcpParametersToDescriptions(inner_transport->GetRtcpParameters(), |
| 443 &local_video_description_, |
| 444 &remote_video_description_); |
| 445 inner_video_transport_ = inner_transport; |
| 446 CreateVideoChannel(); |
| 447 } |
| 448 have_video_receiver_ = true; |
| 449 return RTCError(); |
| 450 } |
| 451 |
| 452 void RtpTransportControllerShim::DetachAudioSender() { |
| 453 if (!have_audio_sender_) { |
| 454 // Should be impossible unless RtpSenderShim is doing something wrong. |
| 455 RTC_NOTREACHED(); |
| 456 return; |
| 457 } |
| 458 // Empty parameters should result in sending being stopped. |
| 459 RTCError err = |
| 460 ValidateAndApplyAudioSenderParameters(RtpParameters(), nullptr); |
| 461 RTC_DCHECK(err.ok()); |
| 462 have_audio_sender_ = false; |
| 463 if (!have_audio_receiver_) { |
| 464 DestroyVoiceChannel(); |
| 465 } |
| 466 } |
| 467 |
| 468 void RtpTransportControllerShim::DetachVideoSender() { |
| 469 if (!have_video_sender_) { |
| 470 // Should be impossible unless RtpSenderShim is doing something wrong. |
| 471 RTC_NOTREACHED(); |
| 472 return; |
| 473 } |
| 474 // Empty parameters should result in sending being stopped. |
| 475 RTCError err = |
| 476 ValidateAndApplyVideoSenderParameters(RtpParameters(), nullptr); |
| 477 RTC_DCHECK(err.ok()); |
| 478 have_video_sender_ = false; |
| 479 if (!have_video_receiver_) { |
| 480 DestroyVoiceChannel(); |
| 481 } |
| 482 } |
| 483 |
| 484 void RtpTransportControllerShim::DetachAudioReceiver() { |
| 485 if (!have_audio_receiver_) { |
| 486 // Should be impossible unless RtpReceiverShim is doing something wrong. |
| 487 RTC_NOTREACHED(); |
| 488 return; |
| 489 } |
| 490 // Empty parameters should result in receiving being stopped. |
| 491 RTCError err = ValidateAndApplyAudioReceiverParameters(RtpParameters()); |
| 492 RTC_DCHECK(err.ok()); |
| 493 have_audio_receiver_ = false; |
| 494 if (!have_audio_sender_) { |
| 495 DestroyVoiceChannel(); |
| 496 } |
| 497 } |
| 498 |
| 499 void RtpTransportControllerShim::DetachVideoReceiver() { |
| 500 if (!have_video_receiver_) { |
| 501 // Should be impossible unless RtpReceiverShim is doing something wrong. |
| 502 RTC_NOTREACHED(); |
| 503 return; |
| 504 } |
| 505 // Empty parameters should result in receiving being stopped. |
| 506 RTCError err = ValidateAndApplyVideoReceiverParameters(RtpParameters()); |
| 507 RTC_DCHECK(err.ok()); |
| 508 have_video_receiver_ = false; |
| 509 if (!have_video_sender_) { |
| 510 DestroyVideoChannel(); |
| 511 } |
| 512 } |
| 513 |
| 514 RTCError RtpTransportControllerShim::ValidateAndApplyAudioSenderParameters( |
| 515 const RtpParameters& parameters, |
| 516 uint32_t* primary_ssrc) { |
| 517 RTC_DCHECK(voice_channel_); |
| 518 RTC_DCHECK(have_audio_sender_); |
| 519 |
| 520 std::vector<cricket::AudioCodec> cricket_codecs; |
| 521 RTCError err = ValidateAndConvertCodecs(parameters.codecs, &cricket_codecs); |
| 522 if (!err.ok()) { |
| 523 return err; |
| 524 } |
| 525 |
| 526 cricket::RtpHeaderExtensions cricket_extensions; |
| 527 err = ValidateAndConvertHeaderExtensions(parameters.header_extensions, |
| 528 &cricket_extensions); |
| 529 if (!err.ok()) { |
| 530 return err; |
| 531 } |
| 532 |
| 533 cricket::StreamParamsVec cricket_streams; |
| 534 cricket::RtpTransceiverDirection local_direction = |
| 535 cricket::RtpTransceiverDirection::FromMediaContentDirection( |
| 536 local_audio_description_.direction()); |
| 537 int bandwidth = cricket::kAutoBandwidth; |
| 538 err = ValidateAndConvertSenderEncodings( |
| 539 parameters.encodings, inner_audio_transport_->GetRtcpParameters().cname, |
| 540 local_audio_description_, &cricket_streams, &local_direction.send, |
| 541 &bandwidth); |
| 542 if (!err.ok()) { |
| 543 return err; |
| 544 } |
| 545 if (primary_ssrc && !cricket_streams.empty()) { |
| 546 *primary_ssrc = cricket_streams[0].first_ssrc(); |
| 547 } |
| 548 |
| 549 // Validation is done, so we can attempt applying the descriptions. Sent |
| 550 // codecs and header extensions go in remote description, streams go in |
| 551 // local. |
| 552 remote_audio_description_.set_codecs(cricket_codecs); |
| 553 remote_audio_description_.set_rtp_header_extensions(cricket_extensions); |
| 554 remote_audio_description_.set_bandwidth(bandwidth); |
| 555 local_audio_description_.mutable_streams() = cricket_streams; |
| 556 // Direction set based on encoding "active" flag. |
| 557 local_audio_description_.set_direction( |
| 558 local_direction.ToMediaContentDirection()); |
| 559 remote_audio_description_.set_direction( |
| 560 local_direction.MakeReversed().ToMediaContentDirection()); |
| 561 |
| 562 if (!voice_channel_->SetLocalContent(&local_audio_description_, |
| 563 cricket::CA_OFFER, nullptr)) { |
| 564 return CreateAndLogError( |
| 565 RTCErrorType::INTERNAL_ERROR, |
| 566 "Failed to apply local parameters to media channel."); |
| 567 } |
| 568 if (!voice_channel_->SetRemoteContent(&remote_audio_description_, |
| 569 cricket::CA_ANSWER, nullptr)) { |
| 570 return CreateAndLogError( |
| 571 RTCErrorType::INTERNAL_ERROR, |
| 572 "Failed to apply remote parameters to media channel."); |
| 573 } |
| 574 return RTCError(); |
| 575 } |
| 576 |
| 577 RTCError RtpTransportControllerShim::ValidateAndApplyVideoSenderParameters( |
| 578 const RtpParameters& parameters, |
| 579 uint32_t* primary_ssrc) { |
| 580 RTC_DCHECK(video_channel_); |
| 581 RTC_DCHECK(have_video_sender_); |
| 582 |
| 583 std::vector<cricket::VideoCodec> cricket_codecs; |
| 584 RTCError err = ValidateAndConvertCodecs(parameters.codecs, &cricket_codecs); |
| 585 if (!err.ok()) { |
| 586 return err; |
| 587 } |
| 588 |
| 589 cricket::RtpHeaderExtensions cricket_extensions; |
| 590 err = ValidateAndConvertHeaderExtensions(parameters.header_extensions, |
| 591 &cricket_extensions); |
| 592 if (!err.ok()) { |
| 593 return err; |
| 594 } |
| 595 |
| 596 cricket::StreamParamsVec cricket_streams; |
| 597 cricket::RtpTransceiverDirection local_direction = |
| 598 cricket::RtpTransceiverDirection::FromMediaContentDirection( |
| 599 local_video_description_.direction()); |
| 600 int bandwidth = cricket::kAutoBandwidth; |
| 601 err = ValidateAndConvertSenderEncodings( |
| 602 parameters.encodings, inner_video_transport_->GetRtcpParameters().cname, |
| 603 local_video_description_, &cricket_streams, &local_direction.send, |
| 604 &bandwidth); |
| 605 if (!err.ok()) { |
| 606 return err; |
| 607 } |
| 608 if (primary_ssrc && !cricket_streams.empty()) { |
| 609 *primary_ssrc = cricket_streams[0].first_ssrc(); |
| 610 } |
| 611 |
| 612 // Validation is done, so we can attempt applying the descriptions. Sent |
| 613 // codecs and header extensions go in remote description, streams go in |
| 614 // local. |
| 615 remote_video_description_.set_codecs(cricket_codecs); |
| 616 remote_video_description_.set_rtp_header_extensions(cricket_extensions); |
| 617 remote_video_description_.set_bandwidth(bandwidth); |
| 618 local_video_description_.mutable_streams() = cricket_streams; |
| 619 // Direction set based on encoding "active" flag. |
| 620 local_video_description_.set_direction( |
| 621 local_direction.ToMediaContentDirection()); |
| 622 remote_video_description_.set_direction( |
| 623 local_direction.MakeReversed().ToMediaContentDirection()); |
| 624 |
| 625 if (!video_channel_->SetLocalContent(&local_video_description_, |
| 626 cricket::CA_OFFER, nullptr)) { |
| 627 return CreateAndLogError( |
| 628 RTCErrorType::INTERNAL_ERROR, |
| 629 "Failed to apply local parameters to media channel."); |
| 630 } |
| 631 if (!video_channel_->SetRemoteContent(&remote_video_description_, |
| 632 cricket::CA_ANSWER, nullptr)) { |
| 633 return CreateAndLogError( |
| 634 RTCErrorType::INTERNAL_ERROR, |
| 635 "Failed to apply remote parameters to media channel."); |
| 636 } |
| 637 return RTCError(); |
| 638 } |
| 639 |
| 640 RTCError RtpTransportControllerShim::ValidateAndApplyAudioReceiverParameters( |
| 641 const RtpParameters& parameters) { |
| 642 RTC_DCHECK(voice_channel_); |
| 643 RTC_DCHECK(have_audio_receiver_); |
| 644 |
| 645 std::vector<cricket::AudioCodec> cricket_codecs; |
| 646 RTCError err = ValidateAndConvertCodecs(parameters.codecs, &cricket_codecs); |
| 647 if (!err.ok()) { |
| 648 return err; |
| 649 } |
| 650 |
| 651 cricket::RtpHeaderExtensions cricket_extensions; |
| 652 err = ValidateAndConvertHeaderExtensions(parameters.header_extensions, |
| 653 &cricket_extensions); |
| 654 if (!err.ok()) { |
| 655 return err; |
| 656 } |
| 657 |
| 658 cricket::StreamParamsVec cricket_streams; |
| 659 cricket::RtpTransceiverDirection local_direction = |
| 660 cricket::RtpTransceiverDirection::FromMediaContentDirection( |
| 661 local_audio_description_.direction()); |
| 662 int bandwidth = cricket::kAutoBandwidth; |
| 663 err = ValidateAndConvertReceiverEncodings( |
| 664 parameters.encodings, &cricket_streams, &local_direction.recv); |
| 665 if (!err.ok()) { |
| 666 return err; |
| 667 } |
| 668 |
| 669 // Validation is done, so we can attempt applying the descriptions. Received |
| 670 // codecs and header extensions go in local description, streams go in |
| 671 // remote. |
| 672 local_audio_description_.set_codecs(cricket_codecs); |
| 673 local_audio_description_.set_rtp_header_extensions(cricket_extensions); |
| 674 local_audio_description_.set_bandwidth(bandwidth); |
| 675 remote_audio_description_.mutable_streams() = cricket_streams; |
| 676 // Direction set based on encoding "active" flag. |
| 677 local_audio_description_.set_direction( |
| 678 local_direction.ToMediaContentDirection()); |
| 679 remote_audio_description_.set_direction( |
| 680 local_direction.MakeReversed().ToMediaContentDirection()); |
| 681 |
| 682 if (!voice_channel_->SetLocalContent(&local_audio_description_, |
| 683 cricket::CA_OFFER, nullptr)) { |
| 684 return CreateAndLogError( |
| 685 RTCErrorType::INTERNAL_ERROR, |
| 686 "Failed to apply local parameters to media channel."); |
| 687 } |
| 688 if (!voice_channel_->SetRemoteContent(&remote_audio_description_, |
| 689 cricket::CA_ANSWER, nullptr)) { |
| 690 return CreateAndLogError( |
| 691 RTCErrorType::INTERNAL_ERROR, |
| 692 "Failed to apply remote parameters to media channel."); |
| 693 } |
| 694 return RTCError(); |
| 695 } |
| 696 |
| 697 RTCError RtpTransportControllerShim::ValidateAndApplyVideoReceiverParameters( |
| 698 const RtpParameters& parameters) { |
| 699 RTC_DCHECK(video_channel_); |
| 700 RTC_DCHECK(have_video_receiver_); |
| 701 |
| 702 std::vector<cricket::VideoCodec> cricket_codecs; |
| 703 RTCError err = ValidateAndConvertCodecs(parameters.codecs, &cricket_codecs); |
| 704 if (!err.ok()) { |
| 705 return err; |
| 706 } |
| 707 |
| 708 cricket::RtpHeaderExtensions cricket_extensions; |
| 709 err = ValidateAndConvertHeaderExtensions(parameters.header_extensions, |
| 710 &cricket_extensions); |
| 711 if (!err.ok()) { |
| 712 return err; |
| 713 } |
| 714 |
| 715 cricket::StreamParamsVec cricket_streams; |
| 716 cricket::RtpTransceiverDirection local_direction = |
| 717 cricket::RtpTransceiverDirection::FromMediaContentDirection( |
| 718 local_video_description_.direction()); |
| 719 int bandwidth = cricket::kAutoBandwidth; |
| 720 err = ValidateAndConvertReceiverEncodings( |
| 721 parameters.encodings, &cricket_streams, &local_direction.recv); |
| 722 if (!err.ok()) { |
| 723 return err; |
| 724 } |
| 725 |
| 726 // Validation is done, so we can attempt applying the descriptions. Received |
| 727 // codecs and header extensions go in local description, streams go in |
| 728 // remote. |
| 729 local_video_description_.set_codecs(cricket_codecs); |
| 730 local_video_description_.set_rtp_header_extensions(cricket_extensions); |
| 731 local_video_description_.set_bandwidth(bandwidth); |
| 732 remote_video_description_.mutable_streams() = cricket_streams; |
| 733 // Direction set based on encoding "active" flag. |
| 734 local_video_description_.set_direction( |
| 735 local_direction.ToMediaContentDirection()); |
| 736 remote_video_description_.set_direction( |
| 737 local_direction.MakeReversed().ToMediaContentDirection()); |
| 738 |
| 739 if (!video_channel_->SetLocalContent(&local_video_description_, |
| 740 cricket::CA_OFFER, nullptr)) { |
| 741 return CreateAndLogError( |
| 742 RTCErrorType::INTERNAL_ERROR, |
| 743 "Failed to apply local parameters to media channel."); |
| 744 } |
| 745 if (!video_channel_->SetRemoteContent(&remote_video_description_, |
| 746 cricket::CA_ANSWER, nullptr)) { |
| 747 return CreateAndLogError( |
| 748 RTCErrorType::INTERNAL_ERROR, |
| 749 "Failed to apply remote parameters to media channel."); |
| 750 } |
| 751 return RTCError(); |
| 752 } |
| 753 |
| 754 RtpTransportControllerShim::RtpTransportControllerShim( |
| 755 const cricket::MediaConfig& config, |
| 756 cricket::ChannelManager* channel_manager, |
| 757 webrtc::RtcEventLog* event_log, |
| 758 rtc::Thread* signaling_thread, |
| 759 rtc::Thread* worker_thread) |
| 760 : signaling_thread_(signaling_thread), |
| 761 media_controller_(MediaControllerInterface::Create(config, |
| 762 worker_thread, |
| 763 channel_manager, |
| 764 event_log)) { |
| 765 RTC_DCHECK_RUN_ON(signaling_thread_); |
| 766 RTC_DCHECK(channel_manager); |
| 767 // MediaControllerInterface::Create should never fail. |
| 768 RTC_DCHECK(media_controller_); |
| 769 } |
| 770 |
| 771 void RtpTransportControllerShim::CreateVoiceChannel() { |
| 772 voice_channel_ = media_controller_->channel_manager()->CreateVoiceChannel( |
| 773 media_controller_.get(), |
| 774 inner_audio_transport_->GetRtpPacketTransport()->GetInternal(), |
| 775 inner_audio_transport_->GetRtcpPacketTransport() |
| 776 ? inner_audio_transport_->GetRtcpPacketTransport()->GetInternal() |
| 777 : nullptr, |
| 778 signaling_thread_, "audio", false, cricket::AudioOptions()); |
| 779 RTC_DCHECK(voice_channel_); |
| 780 voice_channel_->Enable(true); |
| 781 } |
| 782 |
| 783 void RtpTransportControllerShim::CreateVideoChannel() { |
| 784 video_channel_ = media_controller_->channel_manager()->CreateVideoChannel( |
| 785 media_controller_.get(), |
| 786 inner_video_transport_->GetRtpPacketTransport()->GetInternal(), |
| 787 inner_video_transport_->GetRtcpPacketTransport() |
| 788 ? inner_video_transport_->GetRtcpPacketTransport()->GetInternal() |
| 789 : nullptr, |
| 790 signaling_thread_, "audio", false, cricket::VideoOptions()); |
| 791 RTC_DCHECK(video_channel_); |
| 792 video_channel_->Enable(true); |
| 793 } |
| 794 |
| 795 void RtpTransportControllerShim::DestroyVoiceChannel() { |
| 796 RTC_DCHECK(voice_channel_); |
| 797 media_controller_->channel_manager()->DestroyVoiceChannel(voice_channel_); |
| 798 voice_channel_ = nullptr; |
| 799 } |
| 800 |
| 801 void RtpTransportControllerShim::DestroyVideoChannel() { |
| 802 RTC_DCHECK(video_channel_); |
| 803 media_controller_->channel_manager()->DestroyVideoChannel(video_channel_); |
| 804 video_channel_ = nullptr; |
| 805 } |
| 806 |
| 807 void RtpTransportControllerShim::CopyRtcpParametersToDescriptions( |
| 808 const RtcpParameters& params, |
| 809 cricket::MediaContentDescription* local, |
| 810 cricket::MediaContentDescription* remote) { |
| 811 local->set_rtcp_mux(params.mux); |
| 812 remote->set_rtcp_mux(params.mux); |
| 813 local->set_rtcp_reduced_size(params.reduced_size); |
| 814 remote->set_rtcp_reduced_size(params.reduced_size); |
| 815 for (cricket::StreamParams& stream_params : local->mutable_streams()) { |
| 816 stream_params.cname = params.cname; |
| 817 } |
| 818 } |
| 819 |
| 820 uint32_t RtpTransportControllerShim::GenerateUnusedSsrc( |
| 821 const cricket::StreamParams& new_params) const { |
| 822 uint32_t ssrc; |
| 823 do { |
| 824 ssrc = rtc::CreateRandomNonZeroId(); |
| 825 } while ( |
| 826 cricket::GetStreamBySsrc(local_audio_description_.streams(), ssrc) || |
| 827 cricket::GetStreamBySsrc(remote_audio_description_.streams(), ssrc) || |
| 828 cricket::GetStreamBySsrc(local_video_description_.streams(), ssrc) || |
| 829 cricket::GetStreamBySsrc(remote_video_description_.streams(), ssrc) || |
| 830 new_params.has_ssrc(ssrc)); |
| 831 return ssrc; |
| 832 } |
| 833 |
| 834 RTCError RtpTransportControllerShim::ValidateAndConvertSenderEncodings( |
| 835 const std::vector<RtpEncodingParameters> encodings, |
| 836 const std::string& cname, |
| 837 const cricket::MediaContentDescription& description, |
| 838 cricket::StreamParamsVec* cricket_streams, |
| 839 bool* sending, |
| 840 int* bandwidth) const { |
| 841 if (encodings.size() > 1u) { |
| 842 return CreateAndLogError(RTCErrorType::UNSUPPORTED_PARAMETER, |
| 843 "ORTC API implementation doesn't currently " |
| 844 "support simulcast or layered encodings."); |
| 845 } |
| 846 if (encodings.size() == 1u) { |
| 847 const RtpEncodingParameters& encoding = encodings[0]; |
| 848 cricket::StreamParams stream_params; |
| 849 stream_params.cname = cname; |
| 850 if (encoding.ssrc) { |
| 851 stream_params.add_ssrc(*encoding.ssrc); |
| 852 } else { |
| 853 // SSRC not provided; generate it or use the existing one. |
| 854 if (!description.streams().empty()) { |
| 855 stream_params.add_ssrc(description.streams()[0].first_ssrc()); |
| 856 } else { |
| 857 stream_params.add_ssrc(GenerateUnusedSsrc(stream_params)); |
| 858 } |
| 859 } |
| 860 if (encoding.rtx) { |
| 861 if (encoding.rtx->ssrc) { |
| 862 stream_params.AddFidSsrc(stream_params.first_ssrc(), |
| 863 *encoding.rtx->ssrc); |
| 864 } else { |
| 865 // SSRC not provided; generate it or use the existing one. |
| 866 if (!description.streams().empty() && |
| 867 description.streams()[0].has_ssrc_group( |
| 868 cricket::kFidSsrcGroupSemantics)) { |
| 869 stream_params.AddFidSsrc( |
| 870 stream_params.first_ssrc(), |
| 871 description.streams()[0] |
| 872 .get_ssrc_group(cricket::kFidSsrcGroupSemantics) |
| 873 ->ssrcs[1]); |
| 874 } else { |
| 875 stream_params.AddFidSsrc(stream_params.first_ssrc(), |
| 876 GenerateUnusedSsrc(stream_params)); |
| 877 } |
| 878 } |
| 879 } |
| 880 cricket_streams->push_back(std::move(stream_params)); |
| 881 if (encoding.max_bitrate_bps) { |
| 882 *bandwidth = *encoding.max_bitrate_bps; |
| 883 } |
| 884 *sending = encoding.active; |
| 885 } else { |
| 886 *sending = false; |
| 887 } |
| 888 return RTCError(); |
| 889 } |
| 890 |
| 891 } // namespace webrtc |
OLD | NEW |