deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2017 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 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 | std::unique_ptr<RtpTransportControllerAdapter> wrapped( |
| 100 | new RtpTransportControllerAdapter(config, channel_manager, event_log, |
| 101 | signaling_thread, worker_thread)); |
| 102 | return RtpTransportControllerProxyWithInternal< |
| 103 | RtpTransportControllerAdapter>::Create(signaling_thread, worker_thread, |
| 104 | std::move(wrapped)); |
| 105 | } |
| 106 | |
| 107 | RtpTransportControllerAdapter::~RtpTransportControllerAdapter() { |
| 108 | RTC_DCHECK_RUN_ON(signaling_thread_); |
| 109 | if (!transport_proxies_.empty()) { |
| 110 | LOG(LS_ERROR) |
| 111 | << "Destroying RtpTransportControllerAdapter while RtpTransports " |
| 112 | "are still using it; this is unsafe."; |
| 113 | } |
| 114 | if (voice_channel_) { |
| 115 | // This would mean audio RTP senders/receivers that are using us haven't |
| 116 | // been destroyed. This isn't safe (see error log above). |
| 117 | DestroyVoiceChannel(); |
| 118 | } |
| 119 | if (voice_channel_) { |
| 120 | // This would mean video RTP senders/receivers that are using us haven't |
| 121 | // been destroyed. This isn't safe (see error log above). |
| 122 | DestroyVideoChannel(); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | RTCErrorOr<std::unique_ptr<RtpTransportInterface>> |
| 127 | RtpTransportControllerAdapter::CreateProxiedRtpTransport( |
| 128 | const RtcpParameters& rtcp_parameters, |
| 129 | PacketTransportInterface* rtp, |
| 130 | PacketTransportInterface* rtcp) { |
| 131 | auto result = |
| 132 | RtpTransportAdapter::CreateProxied(rtcp_parameters, rtp, rtcp, this); |
| 133 | if (result.ok()) { |
| 134 | transport_proxies_.push_back(result.value().get()); |
| 135 | transport_proxies_.back()->GetInternal()->SignalDestroyed.connect( |
| 136 | this, &RtpTransportControllerAdapter::OnRtpTransportDestroyed); |
| 137 | } |
| 138 | return result; |
| 139 | } |
| 140 | |
| 141 | RTCErrorOr<std::unique_ptr<OrtcRtpSenderInterface>> |
| 142 | RtpTransportControllerAdapter::CreateProxiedRtpSender( |
| 143 | cricket::MediaType kind, |
| 144 | RtpTransportInterface* transport_proxy) { |
| 145 | RTC_DCHECK(transport_proxy); |
| 146 | RTC_DCHECK(std::find(transport_proxies_.begin(), transport_proxies_.end(), |
| 147 | transport_proxy) != transport_proxies_.end()); |
| 148 | std::unique_ptr<OrtcRtpSenderAdapter> new_sender( |
| 149 | new OrtcRtpSenderAdapter(kind, transport_proxy, this)); |
| 150 | RTCError err; |
| 151 | switch (kind) { |
| 152 | case cricket::MEDIA_TYPE_AUDIO: |
| 153 | err = AttachAudioSender(new_sender.get(), transport_proxy->GetInternal()); |
| 154 | break; |
| 155 | case cricket::MEDIA_TYPE_VIDEO: |
| 156 | err = AttachVideoSender(new_sender.get(), transport_proxy->GetInternal()); |
| 157 | break; |
| 158 | case cricket::MEDIA_TYPE_DATA: |
| 159 | RTC_NOTREACHED(); |
| 160 | } |
| 161 | if (!err.ok()) { |
| 162 | return std::move(err); |
| 163 | } |
| 164 | |
| 165 | return OrtcRtpSenderAdapter::CreateProxy(std::move(new_sender)); |
| 166 | } |
| 167 | |
| 168 | RTCErrorOr<std::unique_ptr<OrtcRtpReceiverInterface>> |
| 169 | RtpTransportControllerAdapter::CreateProxiedRtpReceiver( |
| 170 | cricket::MediaType kind, |
| 171 | RtpTransportInterface* transport_proxy) { |
| 172 | RTC_DCHECK(transport_proxy); |
| 173 | RTC_DCHECK(std::find(transport_proxies_.begin(), transport_proxies_.end(), |
| 174 | transport_proxy) != transport_proxies_.end()); |
| 175 | std::unique_ptr<OrtcRtpReceiverAdapter> new_receiver( |
| 176 | new OrtcRtpReceiverAdapter(kind, transport_proxy, this)); |
| 177 | RTCError err; |
| 178 | switch (kind) { |
| 179 | case cricket::MEDIA_TYPE_AUDIO: |
| 180 | err = AttachAudioReceiver(new_receiver.get(), |
| 181 | transport_proxy->GetInternal()); |
| 182 | break; |
| 183 | case cricket::MEDIA_TYPE_VIDEO: |
| 184 | err = AttachVideoReceiver(new_receiver.get(), |
| 185 | transport_proxy->GetInternal()); |
| 186 | break; |
| 187 | case cricket::MEDIA_TYPE_DATA: |
| 188 | RTC_NOTREACHED(); |
| 189 | } |
| 190 | if (!err.ok()) { |
| 191 | return std::move(err); |
| 192 | } |
| 193 | |
| 194 | return OrtcRtpReceiverAdapter::CreateProxy(std::move(new_receiver)); |
| 195 | } |
| 196 | |
| 197 | std::vector<RtpTransportInterface*> |
| 198 | RtpTransportControllerAdapter::GetTransports() const { |
| 199 | RTC_DCHECK_RUN_ON(signaling_thread_); |
| 200 | return transport_proxies_; |
| 201 | } |
| 202 | |
| 203 | RTCError RtpTransportControllerAdapter::SetRtcpParameters( |
| 204 | const RtcpParameters& parameters, |
| 205 | RtpTransportInterface* inner_transport) { |
| 206 | do { |
| 207 | if (inner_transport == inner_audio_transport_) { |
| 208 | CopyRtcpParametersToDescriptions(parameters, &local_audio_description_, |
| 209 | &remote_audio_description_); |
| 210 | if (!voice_channel_->SetLocalContent(&local_audio_description_, |
| 211 | cricket::CA_OFFER, nullptr)) { |
| 212 | break; |
| 213 | } |
| 214 | if (!voice_channel_->SetRemoteContent(&remote_audio_description_, |
| 215 | cricket::CA_ANSWER, nullptr)) { |
| 216 | break; |
| 217 | } |
| 218 | } else if (inner_transport == inner_video_transport_) { |
| 219 | CopyRtcpParametersToDescriptions(parameters, &local_video_description_, |
| 220 | &remote_video_description_); |
| 221 | if (!video_channel_->SetLocalContent(&local_video_description_, |
| 222 | cricket::CA_OFFER, nullptr)) { |
| 223 | break; |
| 224 | } |
| 225 | if (!video_channel_->SetRemoteContent(&remote_video_description_, |
| 226 | cricket::CA_ANSWER, nullptr)) { |
| 227 | break; |
| 228 | } |
| 229 | } |
| 230 | return RTCError::OK(); |
| 231 | } while (false); |
| 232 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 233 | "Failed to apply new RTCP parameters."); |
| 234 | } |
| 235 | |
| 236 | RTCError RtpTransportControllerAdapter::ValidateAndApplyAudioSenderParameters( |
| 237 | const RtpParameters& parameters, |
| 238 | uint32_t* primary_ssrc) { |
| 239 | RTC_DCHECK(voice_channel_); |
| 240 | RTC_DCHECK(have_audio_sender_); |
| 241 | |
| 242 | auto codecs_result = ToCricketCodecs<cricket::AudioCodec>(parameters.codecs); |
| 243 | if (!codecs_result.ok()) { |
| 244 | return codecs_result.MoveError(); |
| 245 | } |
| 246 | |
| 247 | auto extensions_result = |
| 248 | ToCricketRtpHeaderExtensions(parameters.header_extensions); |
| 249 | if (!extensions_result.ok()) { |
| 250 | return extensions_result.MoveError(); |
| 251 | } |
| 252 | |
| 253 | auto stream_params_result = MakeSendStreamParamsVec( |
| 254 | parameters.encodings, inner_audio_transport_->GetRtcpParameters().cname, |
| 255 | local_audio_description_); |
| 256 | if (!stream_params_result.ok()) { |
| 257 | return stream_params_result.MoveError(); |
| 258 | } |
| 259 | |
| 260 | // Check that audio/video sender aren't using the same IDs to refer to |
| 261 | // different things, if they share the same transport. |
| 262 | if (inner_audio_transport_ == inner_video_transport_) { |
| 263 | RTCError err = CheckForIdConflicts( |
| 264 | codecs_result.value(), extensions_result.value(), |
| 265 | stream_params_result.value(), remote_video_description_.codecs(), |
| 266 | remote_video_description_.rtp_header_extensions(), |
| 267 | local_video_description_.streams()); |
| 268 | if (!err.ok()) { |
| 269 | return err; |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | cricket::RtpTransceiverDirection local_direction = |
| 274 | cricket::RtpTransceiverDirection::FromMediaContentDirection( |
| 275 | local_audio_description_.direction()); |
| 276 | int bandwidth = cricket::kAutoBandwidth; |
| 277 | if (parameters.encodings.size() == 1u) { |
| 278 | if (parameters.encodings[0].max_bitrate_bps) { |
| 279 | bandwidth = *parameters.encodings[0].max_bitrate_bps; |
| 280 | } |
| 281 | local_direction.send = parameters.encodings[0].active; |
| 282 | } else { |
| 283 | local_direction.send = false; |
| 284 | } |
| 285 | if (primary_ssrc && !stream_params_result.value().empty()) { |
| 286 | *primary_ssrc = stream_params_result.value()[0].first_ssrc(); |
| 287 | } |
| 288 | |
| 289 | // Validation is done, so we can attempt applying the descriptions. Sent |
| 290 | // codecs and header extensions go in remote description, streams go in |
| 291 | // local. |
| 292 | // |
| 293 | // If there are no codecs or encodings, just leave the previous set of |
| 294 | // codecs. The media engine doesn't like an empty set of codecs. |
| 295 | if (local_audio_description_.streams().empty() && |
| 296 | remote_audio_description_.codecs().empty()) { |
| 297 | } else { |
| 298 | remote_audio_description_.set_codecs(codecs_result.MoveValue()); |
| 299 | } |
| 300 | remote_audio_description_.set_rtp_header_extensions( |
| 301 | extensions_result.MoveValue()); |
| 302 | remote_audio_description_.set_bandwidth(bandwidth); |
| 303 | local_audio_description_.mutable_streams() = stream_params_result.MoveValue(); |
| 304 | // Direction set based on encoding "active" flag. |
| 305 | local_audio_description_.set_direction( |
| 306 | local_direction.ToMediaContentDirection()); |
| 307 | remote_audio_description_.set_direction( |
| 308 | local_direction.Reversed().ToMediaContentDirection()); |
| 309 | |
| 310 | // Set remote content first, to ensure the stream is created with the correct |
| 311 | // codec. |
| 312 | if (!voice_channel_->SetRemoteContent(&remote_audio_description_, |
| 313 | cricket::CA_OFFER, nullptr)) { |
| 314 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 315 | "Failed to apply remote parameters to media channel."); |
| 316 | } |
| 317 | if (!voice_channel_->SetLocalContent(&local_audio_description_, |
| 318 | cricket::CA_ANSWER, nullptr)) { |
| 319 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 320 | "Failed to apply local parameters to media channel."); |
| 321 | } |
| 322 | return RTCError::OK(); |
| 323 | } |
| 324 | |
| 325 | RTCError RtpTransportControllerAdapter::ValidateAndApplyVideoSenderParameters( |
| 326 | const RtpParameters& parameters, |
| 327 | uint32_t* primary_ssrc) { |
| 328 | RTC_DCHECK(video_channel_); |
| 329 | RTC_DCHECK(have_video_sender_); |
| 330 | |
| 331 | auto codecs_result = ToCricketCodecs<cricket::VideoCodec>(parameters.codecs); |
| 332 | if (!codecs_result.ok()) { |
| 333 | return codecs_result.MoveError(); |
| 334 | } |
| 335 | |
| 336 | auto extensions_result = |
| 337 | ToCricketRtpHeaderExtensions(parameters.header_extensions); |
| 338 | if (!extensions_result.ok()) { |
| 339 | return extensions_result.MoveError(); |
| 340 | } |
| 341 | |
| 342 | auto stream_params_result = MakeSendStreamParamsVec( |
| 343 | parameters.encodings, inner_video_transport_->GetRtcpParameters().cname, |
| 344 | local_video_description_); |
| 345 | if (!stream_params_result.ok()) { |
| 346 | return stream_params_result.MoveError(); |
| 347 | } |
| 348 | |
| 349 | // Check that audio/video sender aren't using the same IDs to refer to |
| 350 | // different things, if they share the same transport. |
| 351 | if (inner_audio_transport_ == inner_video_transport_) { |
| 352 | RTCError err = CheckForIdConflicts( |
| 353 | codecs_result.value(), extensions_result.value(), |
| 354 | stream_params_result.value(), remote_audio_description_.codecs(), |
| 355 | remote_audio_description_.rtp_header_extensions(), |
| 356 | local_audio_description_.streams()); |
| 357 | if (!err.ok()) { |
| 358 | return err; |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | cricket::RtpTransceiverDirection local_direction = |
| 363 | cricket::RtpTransceiverDirection::FromMediaContentDirection( |
| 364 | local_video_description_.direction()); |
| 365 | int bandwidth = cricket::kAutoBandwidth; |
| 366 | if (parameters.encodings.size() == 1u) { |
| 367 | if (parameters.encodings[0].max_bitrate_bps) { |
| 368 | bandwidth = *parameters.encodings[0].max_bitrate_bps; |
| 369 | } |
| 370 | local_direction.send = parameters.encodings[0].active; |
| 371 | } else { |
| 372 | local_direction.send = false; |
| 373 | } |
| 374 | if (primary_ssrc && !stream_params_result.value().empty()) { |
| 375 | *primary_ssrc = stream_params_result.value()[0].first_ssrc(); |
| 376 | } |
| 377 | |
| 378 | // Validation is done, so we can attempt applying the descriptions. Sent |
| 379 | // codecs and header extensions go in remote description, streams go in |
| 380 | // local. |
| 381 | // |
| 382 | // If there are no codecs or encodings, just leave the previous set of |
| 383 | // codecs. The media engine doesn't like an empty set of codecs. |
| 384 | if (local_video_description_.streams().empty() && |
| 385 | remote_video_description_.codecs().empty()) { |
| 386 | } else { |
| 387 | remote_video_description_.set_codecs(codecs_result.MoveValue()); |
| 388 | } |
| 389 | remote_video_description_.set_rtp_header_extensions( |
| 390 | extensions_result.MoveValue()); |
| 391 | remote_video_description_.set_bandwidth(bandwidth); |
| 392 | local_video_description_.mutable_streams() = stream_params_result.MoveValue(); |
| 393 | // Direction set based on encoding "active" flag. |
| 394 | local_video_description_.set_direction( |
| 395 | local_direction.ToMediaContentDirection()); |
| 396 | remote_video_description_.set_direction( |
| 397 | local_direction.Reversed().ToMediaContentDirection()); |
| 398 | |
| 399 | // Set remote content first, to ensure the stream is created with the correct |
| 400 | // codec. |
| 401 | if (!video_channel_->SetRemoteContent(&remote_video_description_, |
| 402 | cricket::CA_OFFER, nullptr)) { |
| 403 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 404 | "Failed to apply remote parameters to media channel."); |
| 405 | } |
| 406 | if (!video_channel_->SetLocalContent(&local_video_description_, |
| 407 | cricket::CA_ANSWER, nullptr)) { |
| 408 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 409 | "Failed to apply local parameters to media channel."); |
| 410 | } |
| 411 | return RTCError::OK(); |
| 412 | } |
| 413 | |
| 414 | RTCError RtpTransportControllerAdapter::ValidateAndApplyAudioReceiverParameters( |
| 415 | const RtpParameters& parameters) { |
| 416 | RTC_DCHECK(voice_channel_); |
| 417 | RTC_DCHECK(have_audio_receiver_); |
| 418 | |
| 419 | auto codecs_result = ToCricketCodecs<cricket::AudioCodec>(parameters.codecs); |
| 420 | if (!codecs_result.ok()) { |
| 421 | return codecs_result.MoveError(); |
| 422 | } |
| 423 | |
| 424 | auto extensions_result = |
| 425 | ToCricketRtpHeaderExtensions(parameters.header_extensions); |
| 426 | if (!extensions_result.ok()) { |
| 427 | return extensions_result.MoveError(); |
| 428 | } |
| 429 | |
| 430 | cricket::RtpTransceiverDirection local_direction = |
| 431 | cricket::RtpTransceiverDirection::FromMediaContentDirection( |
| 432 | local_audio_description_.direction()); |
| 433 | auto stream_params_result = ToCricketStreamParamsVec(parameters.encodings); |
| 434 | if (!stream_params_result.ok()) { |
| 435 | return stream_params_result.MoveError(); |
| 436 | } |
| 437 | |
| 438 | // Check that audio/video receive aren't using the same IDs to refer to |
| 439 | // different things, if they share the same transport. |
| 440 | if (inner_audio_transport_ == inner_video_transport_) { |
| 441 | RTCError err = CheckForIdConflicts( |
| 442 | codecs_result.value(), extensions_result.value(), |
| 443 | stream_params_result.value(), local_video_description_.codecs(), |
| 444 | local_video_description_.rtp_header_extensions(), |
| 445 | remote_video_description_.streams()); |
| 446 | if (!err.ok()) { |
| 447 | return err; |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | local_direction.recv = |
| 452 | !parameters.encodings.empty() && parameters.encodings[0].active; |
| 453 | |
| 454 | // Validation is done, so we can attempt applying the descriptions. Received |
| 455 | // codecs and header extensions go in local description, streams go in |
| 456 | // remote. |
| 457 | // |
| 458 | // If there are no codecs or encodings, just leave the previous set of |
| 459 | // codecs. The media engine doesn't like an empty set of codecs. |
| 460 | if (remote_audio_description_.streams().empty() && |
| 461 | local_audio_description_.codecs().empty()) { |
| 462 | } else { |
| 463 | local_audio_description_.set_codecs(codecs_result.MoveValue()); |
| 464 | } |
| 465 | local_audio_description_.set_rtp_header_extensions( |
| 466 | extensions_result.MoveValue()); |
| 467 | remote_audio_description_.mutable_streams() = |
| 468 | stream_params_result.MoveValue(); |
| 469 | // Direction set based on encoding "active" flag. |
| 470 | local_audio_description_.set_direction( |
| 471 | local_direction.ToMediaContentDirection()); |
| 472 | remote_audio_description_.set_direction( |
| 473 | local_direction.Reversed().ToMediaContentDirection()); |
| 474 | |
| 475 | if (!voice_channel_->SetLocalContent(&local_audio_description_, |
| 476 | cricket::CA_OFFER, nullptr)) { |
| 477 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 478 | "Failed to apply local parameters to media channel."); |
| 479 | } |
| 480 | if (!voice_channel_->SetRemoteContent(&remote_audio_description_, |
| 481 | cricket::CA_ANSWER, nullptr)) { |
| 482 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 483 | "Failed to apply remote parameters to media channel."); |
| 484 | } |
| 485 | return RTCError::OK(); |
| 486 | } |
| 487 | |
| 488 | RTCError RtpTransportControllerAdapter::ValidateAndApplyVideoReceiverParameters( |
| 489 | const RtpParameters& parameters) { |
| 490 | RTC_DCHECK(video_channel_); |
| 491 | RTC_DCHECK(have_video_receiver_); |
| 492 | |
| 493 | auto codecs_result = ToCricketCodecs<cricket::VideoCodec>(parameters.codecs); |
| 494 | if (!codecs_result.ok()) { |
| 495 | return codecs_result.MoveError(); |
| 496 | } |
| 497 | |
| 498 | auto extensions_result = |
| 499 | ToCricketRtpHeaderExtensions(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 = ToCricketStreamParamsVec(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::OnRtpTransportDestroyed( |
| 714 | RtpTransportAdapter* transport) { |
| 715 | RTC_DCHECK_RUN_ON(signaling_thread_); |
| 716 | auto it = std::find_if(transport_proxies_.begin(), transport_proxies_.end(), |
| 717 | [transport](RtpTransportInterface* proxy) { |
| 718 | return proxy->GetInternal() == transport; |
| 719 | }); |
| 720 | if (it == transport_proxies_.end()) { |
| 721 | RTC_NOTREACHED(); |
| 722 | return; |
| 723 | } |
| 724 | transport_proxies_.erase(it); |
| 725 | } |
| 726 | |
| 727 | void RtpTransportControllerAdapter::OnAudioSenderDestroyed() { |
| 728 | if (!have_audio_sender_) { |
| 729 | RTC_NOTREACHED(); |
| 730 | return; |
| 731 | } |
| 732 | // Empty parameters should result in sending being stopped. |
| 733 | RTCError err = |
| 734 | ValidateAndApplyAudioSenderParameters(RtpParameters(), nullptr); |
| 735 | RTC_DCHECK(err.ok()); |
| 736 | have_audio_sender_ = false; |
| 737 | if (!have_audio_receiver_) { |
| 738 | DestroyVoiceChannel(); |
| 739 | } |
| 740 | } |
| 741 | |
| 742 | void RtpTransportControllerAdapter::OnVideoSenderDestroyed() { |
| 743 | if (!have_video_sender_) { |
| 744 | RTC_NOTREACHED(); |
| 745 | return; |
| 746 | } |
| 747 | // Empty parameters should result in sending being stopped. |
| 748 | RTCError err = |
| 749 | ValidateAndApplyVideoSenderParameters(RtpParameters(), nullptr); |
| 750 | RTC_DCHECK(err.ok()); |
| 751 | have_video_sender_ = false; |
| 752 | if (!have_video_receiver_) { |
| 753 | DestroyVideoChannel(); |
| 754 | } |
| 755 | } |
| 756 | |
| 757 | void RtpTransportControllerAdapter::OnAudioReceiverDestroyed() { |
| 758 | if (!have_audio_receiver_) { |
| 759 | RTC_NOTREACHED(); |
| 760 | return; |
| 761 | } |
| 762 | // Empty parameters should result in receiving being stopped. |
| 763 | RTCError err = ValidateAndApplyAudioReceiverParameters(RtpParameters()); |
| 764 | RTC_DCHECK(err.ok()); |
| 765 | have_audio_receiver_ = false; |
| 766 | if (!have_audio_sender_) { |
| 767 | DestroyVoiceChannel(); |
| 768 | } |
| 769 | } |
| 770 | |
| 771 | void RtpTransportControllerAdapter::OnVideoReceiverDestroyed() { |
| 772 | if (!have_video_receiver_) { |
| 773 | RTC_NOTREACHED(); |
| 774 | return; |
| 775 | } |
| 776 | // Empty parameters should result in receiving being stopped. |
| 777 | RTCError err = ValidateAndApplyVideoReceiverParameters(RtpParameters()); |
| 778 | RTC_DCHECK(err.ok()); |
| 779 | have_video_receiver_ = false; |
| 780 | if (!have_video_sender_) { |
| 781 | DestroyVideoChannel(); |
| 782 | } |
| 783 | } |
| 784 | |
| 785 | void RtpTransportControllerAdapter::CreateVoiceChannel() { |
| 786 | voice_channel_ = media_controller_->channel_manager()->CreateVoiceChannel( |
| 787 | media_controller_.get(), |
| 788 | inner_audio_transport_->GetRtpPacketTransport()->GetInternal(), |
| 789 | inner_audio_transport_->GetRtcpPacketTransport() |
| 790 | ? inner_audio_transport_->GetRtcpPacketTransport()->GetInternal() |
| 791 | : nullptr, |
| 792 | signaling_thread_, "audio", false, cricket::AudioOptions()); |
| 793 | RTC_DCHECK(voice_channel_); |
| 794 | voice_channel_->Enable(true); |
| 795 | } |
| 796 | |
| 797 | void RtpTransportControllerAdapter::CreateVideoChannel() { |
| 798 | video_channel_ = media_controller_->channel_manager()->CreateVideoChannel( |
| 799 | media_controller_.get(), |
| 800 | inner_video_transport_->GetRtpPacketTransport()->GetInternal(), |
| 801 | inner_video_transport_->GetRtcpPacketTransport() |
| 802 | ? inner_video_transport_->GetRtcpPacketTransport()->GetInternal() |
| 803 | : nullptr, |
| 804 | signaling_thread_, "video", false, cricket::VideoOptions()); |
| 805 | RTC_DCHECK(video_channel_); |
| 806 | video_channel_->Enable(true); |
| 807 | } |
| 808 | |
| 809 | void RtpTransportControllerAdapter::DestroyVoiceChannel() { |
| 810 | RTC_DCHECK(voice_channel_); |
| 811 | media_controller_->channel_manager()->DestroyVoiceChannel(voice_channel_); |
| 812 | voice_channel_ = nullptr; |
| 813 | inner_audio_transport_ = nullptr; |
| 814 | } |
| 815 | |
| 816 | void RtpTransportControllerAdapter::DestroyVideoChannel() { |
| 817 | RTC_DCHECK(video_channel_); |
| 818 | media_controller_->channel_manager()->DestroyVideoChannel(video_channel_); |
| 819 | video_channel_ = nullptr; |
| 820 | inner_video_transport_ = nullptr; |
| 821 | } |
| 822 | |
| 823 | void RtpTransportControllerAdapter::CopyRtcpParametersToDescriptions( |
| 824 | const RtcpParameters& params, |
| 825 | cricket::MediaContentDescription* local, |
| 826 | cricket::MediaContentDescription* remote) { |
| 827 | local->set_rtcp_mux(params.mux); |
| 828 | remote->set_rtcp_mux(params.mux); |
| 829 | local->set_rtcp_reduced_size(params.reduced_size); |
| 830 | remote->set_rtcp_reduced_size(params.reduced_size); |
| 831 | for (cricket::StreamParams& stream_params : local->mutable_streams()) { |
| 832 | stream_params.cname = params.cname; |
| 833 | } |
| 834 | } |
| 835 | |
| 836 | uint32_t RtpTransportControllerAdapter::GenerateUnusedSsrc( |
| 837 | std::set<uint32_t>* new_ssrcs) const { |
| 838 | uint32_t ssrc; |
| 839 | do { |
| 840 | ssrc = rtc::CreateRandomNonZeroId(); |
| 841 | } while ( |
| 842 | cricket::GetStreamBySsrc(local_audio_description_.streams(), ssrc) || |
| 843 | cricket::GetStreamBySsrc(remote_audio_description_.streams(), ssrc) || |
| 844 | cricket::GetStreamBySsrc(local_video_description_.streams(), ssrc) || |
| 845 | cricket::GetStreamBySsrc(remote_video_description_.streams(), ssrc) || |
| 846 | !new_ssrcs->insert(ssrc).second); |
| 847 | return ssrc; |
| 848 | } |
| 849 | |
| 850 | RTCErrorOr<cricket::StreamParamsVec> |
| 851 | RtpTransportControllerAdapter::MakeSendStreamParamsVec( |
| 852 | std::vector<RtpEncodingParameters> encodings, |
| 853 | const std::string& cname, |
| 854 | const cricket::MediaContentDescription& description) const { |
| 855 | if (encodings.size() > 1u) { |
| 856 | LOG_AND_RETURN_ERROR(webrtc::RTCErrorType::UNSUPPORTED_PARAMETER, |
| 857 | "ORTC API implementation doesn't currently " |
| 858 | "support simulcast or layered encodings."); |
| 859 | } else if (encodings.empty()) { |
| 860 | return cricket::StreamParamsVec(); |
| 861 | } |
| 862 | RtpEncodingParameters& encoding = encodings[0]; |
| 863 | std::set<uint32_t> new_ssrcs; |
| 864 | if (encoding.ssrc) { |
| 865 | new_ssrcs.insert(*encoding.ssrc); |
| 866 | } |
| 867 | if (encoding.rtx && encoding.rtx->ssrc) { |
| 868 | new_ssrcs.insert(*encoding.rtx->ssrc); |
| 869 | } |
| 870 | // May need to fill missing SSRCs with generated ones. |
| 871 | if (!encoding.ssrc) { |
| 872 | if (!description.streams().empty()) { |
| 873 | encoding.ssrc.emplace(description.streams()[0].first_ssrc()); |
| 874 | } else { |
| 875 | encoding.ssrc.emplace(GenerateUnusedSsrc(&new_ssrcs)); |
| 876 | } |
| 877 | } |
| 878 | if (encoding.rtx && !encoding.rtx->ssrc) { |
| 879 | uint32_t existing_rtx_ssrc; |
| 880 | if (!description.streams().empty() && |
| 881 | description.streams()[0].GetFidSsrc( |
| 882 | description.streams()[0].first_ssrc(), &existing_rtx_ssrc)) { |
| 883 | encoding.rtx->ssrc.emplace(existing_rtx_ssrc); |
| 884 | } else { |
| 885 | encoding.rtx->ssrc.emplace(GenerateUnusedSsrc(&new_ssrcs)); |
| 886 | } |
| 887 | } |
| 888 | |
| 889 | auto result = ToCricketStreamParamsVec(encodings); |
| 890 | if (!result.ok()) { |
| 891 | return result.MoveError(); |
| 892 | } |
| 893 | // If conversion was successful, there should be one StreamParams. |
| 894 | RTC_DCHECK_EQ(1u, result.value().size()); |
| 895 | result.value()[0].cname = cname; |
| 896 | return result; |
| 897 | } |
| 898 | |
| 899 | } // namespace webrtc |