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