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". |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 14 | #include <set> |
zhihuang | d3501ad | 2017-03-03 14:39:06 -0800 | [diff] [blame] | 15 | #include <sstream> |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 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 | |
zhihuang | d3501ad | 2017-03-03 14:39:06 -0800 | [diff] [blame] | 141 | RTCErrorOr<std::unique_ptr<SrtpTransportInterface>> |
| 142 | RtpTransportControllerAdapter::CreateProxiedSrtpTransport( |
| 143 | const RtcpParameters& rtcp_parameters, |
| 144 | PacketTransportInterface* rtp, |
| 145 | PacketTransportInterface* rtcp) { |
| 146 | auto result = |
| 147 | RtpTransportAdapter::CreateSrtpProxied(rtcp_parameters, rtp, rtcp, this); |
| 148 | if (result.ok()) { |
| 149 | transport_proxies_.push_back(result.value().get()); |
| 150 | transport_proxies_.back()->GetInternal()->SignalDestroyed.connect( |
| 151 | this, &RtpTransportControllerAdapter::OnRtpTransportDestroyed); |
| 152 | } |
| 153 | return result; |
| 154 | } |
| 155 | |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 156 | RTCErrorOr<std::unique_ptr<OrtcRtpSenderInterface>> |
| 157 | RtpTransportControllerAdapter::CreateProxiedRtpSender( |
| 158 | cricket::MediaType kind, |
| 159 | RtpTransportInterface* transport_proxy) { |
| 160 | RTC_DCHECK(transport_proxy); |
| 161 | RTC_DCHECK(std::find(transport_proxies_.begin(), transport_proxies_.end(), |
| 162 | transport_proxy) != transport_proxies_.end()); |
| 163 | std::unique_ptr<OrtcRtpSenderAdapter> new_sender( |
| 164 | new OrtcRtpSenderAdapter(kind, transport_proxy, this)); |
| 165 | RTCError err; |
| 166 | switch (kind) { |
| 167 | case cricket::MEDIA_TYPE_AUDIO: |
| 168 | err = AttachAudioSender(new_sender.get(), transport_proxy->GetInternal()); |
| 169 | break; |
| 170 | case cricket::MEDIA_TYPE_VIDEO: |
| 171 | err = AttachVideoSender(new_sender.get(), transport_proxy->GetInternal()); |
| 172 | break; |
| 173 | case cricket::MEDIA_TYPE_DATA: |
| 174 | RTC_NOTREACHED(); |
| 175 | } |
| 176 | if (!err.ok()) { |
| 177 | return std::move(err); |
| 178 | } |
| 179 | |
| 180 | return OrtcRtpSenderAdapter::CreateProxy(std::move(new_sender)); |
| 181 | } |
| 182 | |
| 183 | RTCErrorOr<std::unique_ptr<OrtcRtpReceiverInterface>> |
| 184 | RtpTransportControllerAdapter::CreateProxiedRtpReceiver( |
| 185 | cricket::MediaType kind, |
| 186 | RtpTransportInterface* transport_proxy) { |
| 187 | RTC_DCHECK(transport_proxy); |
| 188 | RTC_DCHECK(std::find(transport_proxies_.begin(), transport_proxies_.end(), |
| 189 | transport_proxy) != transport_proxies_.end()); |
| 190 | std::unique_ptr<OrtcRtpReceiverAdapter> new_receiver( |
| 191 | new OrtcRtpReceiverAdapter(kind, transport_proxy, this)); |
| 192 | RTCError err; |
| 193 | switch (kind) { |
| 194 | case cricket::MEDIA_TYPE_AUDIO: |
| 195 | err = AttachAudioReceiver(new_receiver.get(), |
| 196 | transport_proxy->GetInternal()); |
| 197 | break; |
| 198 | case cricket::MEDIA_TYPE_VIDEO: |
| 199 | err = AttachVideoReceiver(new_receiver.get(), |
| 200 | transport_proxy->GetInternal()); |
| 201 | break; |
| 202 | case cricket::MEDIA_TYPE_DATA: |
| 203 | RTC_NOTREACHED(); |
| 204 | } |
| 205 | if (!err.ok()) { |
| 206 | return std::move(err); |
| 207 | } |
| 208 | |
| 209 | return OrtcRtpReceiverAdapter::CreateProxy(std::move(new_receiver)); |
| 210 | } |
| 211 | |
| 212 | std::vector<RtpTransportInterface*> |
| 213 | RtpTransportControllerAdapter::GetTransports() const { |
| 214 | RTC_DCHECK_RUN_ON(signaling_thread_); |
| 215 | return transport_proxies_; |
| 216 | } |
| 217 | |
| 218 | RTCError RtpTransportControllerAdapter::SetRtcpParameters( |
| 219 | const RtcpParameters& parameters, |
| 220 | RtpTransportInterface* inner_transport) { |
| 221 | do { |
| 222 | if (inner_transport == inner_audio_transport_) { |
| 223 | CopyRtcpParametersToDescriptions(parameters, &local_audio_description_, |
| 224 | &remote_audio_description_); |
| 225 | if (!voice_channel_->SetLocalContent(&local_audio_description_, |
| 226 | cricket::CA_OFFER, nullptr)) { |
| 227 | break; |
| 228 | } |
| 229 | if (!voice_channel_->SetRemoteContent(&remote_audio_description_, |
| 230 | cricket::CA_ANSWER, nullptr)) { |
| 231 | break; |
| 232 | } |
| 233 | } else if (inner_transport == inner_video_transport_) { |
| 234 | CopyRtcpParametersToDescriptions(parameters, &local_video_description_, |
| 235 | &remote_video_description_); |
| 236 | if (!video_channel_->SetLocalContent(&local_video_description_, |
| 237 | cricket::CA_OFFER, nullptr)) { |
| 238 | break; |
| 239 | } |
| 240 | if (!video_channel_->SetRemoteContent(&remote_video_description_, |
| 241 | cricket::CA_ANSWER, nullptr)) { |
| 242 | break; |
| 243 | } |
| 244 | } |
| 245 | return RTCError::OK(); |
| 246 | } while (false); |
| 247 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 248 | "Failed to apply new RTCP parameters."); |
| 249 | } |
| 250 | |
| 251 | RTCError RtpTransportControllerAdapter::ValidateAndApplyAudioSenderParameters( |
| 252 | const RtpParameters& parameters, |
| 253 | uint32_t* primary_ssrc) { |
| 254 | RTC_DCHECK(voice_channel_); |
| 255 | RTC_DCHECK(have_audio_sender_); |
| 256 | |
| 257 | auto codecs_result = ToCricketCodecs<cricket::AudioCodec>(parameters.codecs); |
| 258 | if (!codecs_result.ok()) { |
| 259 | return codecs_result.MoveError(); |
| 260 | } |
| 261 | |
| 262 | auto extensions_result = |
| 263 | ToCricketRtpHeaderExtensions(parameters.header_extensions); |
| 264 | if (!extensions_result.ok()) { |
| 265 | return extensions_result.MoveError(); |
| 266 | } |
| 267 | |
| 268 | auto stream_params_result = MakeSendStreamParamsVec( |
| 269 | parameters.encodings, inner_audio_transport_->GetRtcpParameters().cname, |
| 270 | local_audio_description_); |
| 271 | if (!stream_params_result.ok()) { |
| 272 | return stream_params_result.MoveError(); |
| 273 | } |
| 274 | |
| 275 | // Check that audio/video sender aren't using the same IDs to refer to |
| 276 | // different things, if they share the same transport. |
| 277 | if (inner_audio_transport_ == inner_video_transport_) { |
| 278 | RTCError err = CheckForIdConflicts( |
| 279 | codecs_result.value(), extensions_result.value(), |
| 280 | stream_params_result.value(), remote_video_description_.codecs(), |
| 281 | remote_video_description_.rtp_header_extensions(), |
| 282 | local_video_description_.streams()); |
| 283 | if (!err.ok()) { |
| 284 | return err; |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | cricket::RtpTransceiverDirection local_direction = |
| 289 | cricket::RtpTransceiverDirection::FromMediaContentDirection( |
| 290 | local_audio_description_.direction()); |
| 291 | int bandwidth = cricket::kAutoBandwidth; |
| 292 | if (parameters.encodings.size() == 1u) { |
| 293 | if (parameters.encodings[0].max_bitrate_bps) { |
| 294 | bandwidth = *parameters.encodings[0].max_bitrate_bps; |
| 295 | } |
| 296 | local_direction.send = parameters.encodings[0].active; |
| 297 | } else { |
| 298 | local_direction.send = false; |
| 299 | } |
| 300 | if (primary_ssrc && !stream_params_result.value().empty()) { |
| 301 | *primary_ssrc = stream_params_result.value()[0].first_ssrc(); |
| 302 | } |
| 303 | |
| 304 | // Validation is done, so we can attempt applying the descriptions. Sent |
| 305 | // codecs and header extensions go in remote description, streams go in |
| 306 | // local. |
| 307 | // |
| 308 | // If there are no codecs or encodings, just leave the previous set of |
| 309 | // codecs. The media engine doesn't like an empty set of codecs. |
| 310 | if (local_audio_description_.streams().empty() && |
| 311 | remote_audio_description_.codecs().empty()) { |
| 312 | } else { |
| 313 | remote_audio_description_.set_codecs(codecs_result.MoveValue()); |
| 314 | } |
| 315 | remote_audio_description_.set_rtp_header_extensions( |
| 316 | extensions_result.MoveValue()); |
| 317 | remote_audio_description_.set_bandwidth(bandwidth); |
| 318 | local_audio_description_.mutable_streams() = stream_params_result.MoveValue(); |
| 319 | // Direction set based on encoding "active" flag. |
| 320 | local_audio_description_.set_direction( |
| 321 | local_direction.ToMediaContentDirection()); |
| 322 | remote_audio_description_.set_direction( |
| 323 | local_direction.Reversed().ToMediaContentDirection()); |
| 324 | |
| 325 | // Set remote content first, to ensure the stream is created with the correct |
| 326 | // codec. |
| 327 | if (!voice_channel_->SetRemoteContent(&remote_audio_description_, |
| 328 | cricket::CA_OFFER, nullptr)) { |
| 329 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 330 | "Failed to apply remote parameters to media channel."); |
| 331 | } |
| 332 | if (!voice_channel_->SetLocalContent(&local_audio_description_, |
| 333 | cricket::CA_ANSWER, nullptr)) { |
| 334 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 335 | "Failed to apply local parameters to media channel."); |
| 336 | } |
| 337 | return RTCError::OK(); |
| 338 | } |
| 339 | |
| 340 | RTCError RtpTransportControllerAdapter::ValidateAndApplyVideoSenderParameters( |
| 341 | const RtpParameters& parameters, |
| 342 | uint32_t* primary_ssrc) { |
| 343 | RTC_DCHECK(video_channel_); |
| 344 | RTC_DCHECK(have_video_sender_); |
| 345 | |
| 346 | auto codecs_result = ToCricketCodecs<cricket::VideoCodec>(parameters.codecs); |
| 347 | if (!codecs_result.ok()) { |
| 348 | return codecs_result.MoveError(); |
| 349 | } |
| 350 | |
| 351 | auto extensions_result = |
| 352 | ToCricketRtpHeaderExtensions(parameters.header_extensions); |
| 353 | if (!extensions_result.ok()) { |
| 354 | return extensions_result.MoveError(); |
| 355 | } |
| 356 | |
| 357 | auto stream_params_result = MakeSendStreamParamsVec( |
| 358 | parameters.encodings, inner_video_transport_->GetRtcpParameters().cname, |
| 359 | local_video_description_); |
| 360 | if (!stream_params_result.ok()) { |
| 361 | return stream_params_result.MoveError(); |
| 362 | } |
| 363 | |
| 364 | // Check that audio/video sender aren't using the same IDs to refer to |
| 365 | // different things, if they share the same transport. |
| 366 | if (inner_audio_transport_ == inner_video_transport_) { |
| 367 | RTCError err = CheckForIdConflicts( |
| 368 | codecs_result.value(), extensions_result.value(), |
| 369 | stream_params_result.value(), remote_audio_description_.codecs(), |
| 370 | remote_audio_description_.rtp_header_extensions(), |
| 371 | local_audio_description_.streams()); |
| 372 | if (!err.ok()) { |
| 373 | return err; |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | cricket::RtpTransceiverDirection local_direction = |
| 378 | cricket::RtpTransceiverDirection::FromMediaContentDirection( |
| 379 | local_video_description_.direction()); |
| 380 | int bandwidth = cricket::kAutoBandwidth; |
| 381 | if (parameters.encodings.size() == 1u) { |
| 382 | if (parameters.encodings[0].max_bitrate_bps) { |
| 383 | bandwidth = *parameters.encodings[0].max_bitrate_bps; |
| 384 | } |
| 385 | local_direction.send = parameters.encodings[0].active; |
| 386 | } else { |
| 387 | local_direction.send = false; |
| 388 | } |
| 389 | if (primary_ssrc && !stream_params_result.value().empty()) { |
| 390 | *primary_ssrc = stream_params_result.value()[0].first_ssrc(); |
| 391 | } |
| 392 | |
| 393 | // Validation is done, so we can attempt applying the descriptions. Sent |
| 394 | // codecs and header extensions go in remote description, streams go in |
| 395 | // local. |
| 396 | // |
| 397 | // If there are no codecs or encodings, just leave the previous set of |
| 398 | // codecs. The media engine doesn't like an empty set of codecs. |
| 399 | if (local_video_description_.streams().empty() && |
| 400 | remote_video_description_.codecs().empty()) { |
| 401 | } else { |
| 402 | remote_video_description_.set_codecs(codecs_result.MoveValue()); |
| 403 | } |
| 404 | remote_video_description_.set_rtp_header_extensions( |
| 405 | extensions_result.MoveValue()); |
| 406 | remote_video_description_.set_bandwidth(bandwidth); |
| 407 | local_video_description_.mutable_streams() = stream_params_result.MoveValue(); |
| 408 | // Direction set based on encoding "active" flag. |
| 409 | local_video_description_.set_direction( |
| 410 | local_direction.ToMediaContentDirection()); |
| 411 | remote_video_description_.set_direction( |
| 412 | local_direction.Reversed().ToMediaContentDirection()); |
| 413 | |
| 414 | // Set remote content first, to ensure the stream is created with the correct |
| 415 | // codec. |
| 416 | if (!video_channel_->SetRemoteContent(&remote_video_description_, |
| 417 | cricket::CA_OFFER, nullptr)) { |
| 418 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 419 | "Failed to apply remote parameters to media channel."); |
| 420 | } |
| 421 | if (!video_channel_->SetLocalContent(&local_video_description_, |
| 422 | cricket::CA_ANSWER, nullptr)) { |
| 423 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 424 | "Failed to apply local parameters to media channel."); |
| 425 | } |
| 426 | return RTCError::OK(); |
| 427 | } |
| 428 | |
| 429 | RTCError RtpTransportControllerAdapter::ValidateAndApplyAudioReceiverParameters( |
| 430 | const RtpParameters& parameters) { |
| 431 | RTC_DCHECK(voice_channel_); |
| 432 | RTC_DCHECK(have_audio_receiver_); |
| 433 | |
| 434 | auto codecs_result = ToCricketCodecs<cricket::AudioCodec>(parameters.codecs); |
| 435 | if (!codecs_result.ok()) { |
| 436 | return codecs_result.MoveError(); |
| 437 | } |
| 438 | |
| 439 | auto extensions_result = |
| 440 | ToCricketRtpHeaderExtensions(parameters.header_extensions); |
| 441 | if (!extensions_result.ok()) { |
| 442 | return extensions_result.MoveError(); |
| 443 | } |
| 444 | |
| 445 | cricket::RtpTransceiverDirection local_direction = |
| 446 | cricket::RtpTransceiverDirection::FromMediaContentDirection( |
| 447 | local_audio_description_.direction()); |
| 448 | auto stream_params_result = ToCricketStreamParamsVec(parameters.encodings); |
| 449 | if (!stream_params_result.ok()) { |
| 450 | return stream_params_result.MoveError(); |
| 451 | } |
| 452 | |
| 453 | // Check that audio/video receive aren't using the same IDs to refer to |
| 454 | // different things, if they share the same transport. |
| 455 | if (inner_audio_transport_ == inner_video_transport_) { |
| 456 | RTCError err = CheckForIdConflicts( |
| 457 | codecs_result.value(), extensions_result.value(), |
| 458 | stream_params_result.value(), local_video_description_.codecs(), |
| 459 | local_video_description_.rtp_header_extensions(), |
| 460 | remote_video_description_.streams()); |
| 461 | if (!err.ok()) { |
| 462 | return err; |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | local_direction.recv = |
| 467 | !parameters.encodings.empty() && parameters.encodings[0].active; |
| 468 | |
| 469 | // Validation is done, so we can attempt applying the descriptions. Received |
| 470 | // codecs and header extensions go in local description, streams go in |
| 471 | // remote. |
| 472 | // |
| 473 | // If there are no codecs or encodings, just leave the previous set of |
| 474 | // codecs. The media engine doesn't like an empty set of codecs. |
| 475 | if (remote_audio_description_.streams().empty() && |
| 476 | local_audio_description_.codecs().empty()) { |
| 477 | } else { |
| 478 | local_audio_description_.set_codecs(codecs_result.MoveValue()); |
| 479 | } |
| 480 | local_audio_description_.set_rtp_header_extensions( |
| 481 | extensions_result.MoveValue()); |
| 482 | remote_audio_description_.mutable_streams() = |
| 483 | stream_params_result.MoveValue(); |
| 484 | // Direction set based on encoding "active" flag. |
| 485 | local_audio_description_.set_direction( |
| 486 | local_direction.ToMediaContentDirection()); |
| 487 | remote_audio_description_.set_direction( |
| 488 | local_direction.Reversed().ToMediaContentDirection()); |
| 489 | |
| 490 | if (!voice_channel_->SetLocalContent(&local_audio_description_, |
| 491 | cricket::CA_OFFER, nullptr)) { |
| 492 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 493 | "Failed to apply local parameters to media channel."); |
| 494 | } |
| 495 | if (!voice_channel_->SetRemoteContent(&remote_audio_description_, |
| 496 | cricket::CA_ANSWER, nullptr)) { |
| 497 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 498 | "Failed to apply remote parameters to media channel."); |
| 499 | } |
| 500 | return RTCError::OK(); |
| 501 | } |
| 502 | |
| 503 | RTCError RtpTransportControllerAdapter::ValidateAndApplyVideoReceiverParameters( |
| 504 | const RtpParameters& parameters) { |
| 505 | RTC_DCHECK(video_channel_); |
| 506 | RTC_DCHECK(have_video_receiver_); |
| 507 | |
| 508 | auto codecs_result = ToCricketCodecs<cricket::VideoCodec>(parameters.codecs); |
| 509 | if (!codecs_result.ok()) { |
| 510 | return codecs_result.MoveError(); |
| 511 | } |
| 512 | |
| 513 | auto extensions_result = |
| 514 | ToCricketRtpHeaderExtensions(parameters.header_extensions); |
| 515 | if (!extensions_result.ok()) { |
| 516 | return extensions_result.MoveError(); |
| 517 | } |
| 518 | |
| 519 | cricket::RtpTransceiverDirection local_direction = |
| 520 | cricket::RtpTransceiverDirection::FromMediaContentDirection( |
| 521 | local_video_description_.direction()); |
| 522 | int bandwidth = cricket::kAutoBandwidth; |
| 523 | auto stream_params_result = ToCricketStreamParamsVec(parameters.encodings); |
| 524 | if (!stream_params_result.ok()) { |
| 525 | return stream_params_result.MoveError(); |
| 526 | } |
| 527 | |
| 528 | // Check that audio/video receiver aren't using the same IDs to refer to |
| 529 | // different things, if they share the same transport. |
| 530 | if (inner_audio_transport_ == inner_video_transport_) { |
| 531 | RTCError err = CheckForIdConflicts( |
| 532 | codecs_result.value(), extensions_result.value(), |
| 533 | stream_params_result.value(), local_audio_description_.codecs(), |
| 534 | local_audio_description_.rtp_header_extensions(), |
| 535 | remote_audio_description_.streams()); |
| 536 | if (!err.ok()) { |
| 537 | return err; |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | local_direction.recv = |
| 542 | !parameters.encodings.empty() && parameters.encodings[0].active; |
| 543 | |
| 544 | // Validation is done, so we can attempt applying the descriptions. Received |
| 545 | // codecs and header extensions go in local description, streams go in |
| 546 | // remote. |
| 547 | // |
| 548 | // If there are no codecs or encodings, just leave the previous set of |
| 549 | // codecs. The media engine doesn't like an empty set of codecs. |
| 550 | if (remote_video_description_.streams().empty() && |
| 551 | local_video_description_.codecs().empty()) { |
| 552 | } else { |
| 553 | local_video_description_.set_codecs(codecs_result.MoveValue()); |
| 554 | } |
| 555 | local_video_description_.set_rtp_header_extensions( |
| 556 | extensions_result.MoveValue()); |
| 557 | local_video_description_.set_bandwidth(bandwidth); |
| 558 | remote_video_description_.mutable_streams() = |
| 559 | stream_params_result.MoveValue(); |
| 560 | // Direction set based on encoding "active" flag. |
| 561 | local_video_description_.set_direction( |
| 562 | local_direction.ToMediaContentDirection()); |
| 563 | remote_video_description_.set_direction( |
| 564 | local_direction.Reversed().ToMediaContentDirection()); |
| 565 | |
| 566 | if (!video_channel_->SetLocalContent(&local_video_description_, |
| 567 | cricket::CA_OFFER, nullptr)) { |
| 568 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 569 | "Failed to apply local parameters to media channel."); |
| 570 | } |
| 571 | if (!video_channel_->SetRemoteContent(&remote_video_description_, |
| 572 | cricket::CA_ANSWER, nullptr)) { |
| 573 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 574 | "Failed to apply remote parameters to media channel."); |
| 575 | } |
| 576 | return RTCError::OK(); |
| 577 | } |
| 578 | |
| 579 | RtpTransportControllerAdapter::RtpTransportControllerAdapter( |
| 580 | const cricket::MediaConfig& config, |
| 581 | cricket::ChannelManager* channel_manager, |
| 582 | webrtc::RtcEventLog* event_log, |
| 583 | rtc::Thread* signaling_thread, |
| 584 | rtc::Thread* worker_thread) |
| 585 | : signaling_thread_(signaling_thread), |
| 586 | worker_thread_(worker_thread), |
| 587 | media_controller_(MediaControllerInterface::Create(config, |
| 588 | worker_thread, |
| 589 | channel_manager, |
| 590 | event_log)) { |
| 591 | RTC_DCHECK_RUN_ON(signaling_thread_); |
| 592 | RTC_DCHECK(channel_manager); |
| 593 | // MediaControllerInterface::Create should never fail. |
| 594 | RTC_DCHECK(media_controller_); |
| 595 | // Add "dummy" codecs to the descriptions, because the media engines |
| 596 | // currently reject empty lists of codecs. Note that these codecs will never |
| 597 | // actually be used, because when parameters are set, the dummy codecs will |
| 598 | // be replaced by actual codecs before any send/receive streams are created. |
| 599 | static const cricket::AudioCodec dummy_audio(0, cricket::kPcmuCodecName, 8000, |
| 600 | 0, 1); |
| 601 | static const cricket::VideoCodec dummy_video(96, cricket::kVp8CodecName); |
| 602 | local_audio_description_.AddCodec(dummy_audio); |
| 603 | remote_audio_description_.AddCodec(dummy_audio); |
| 604 | local_video_description_.AddCodec(dummy_video); |
| 605 | remote_video_description_.AddCodec(dummy_video); |
| 606 | } |
| 607 | |
| 608 | RTCError RtpTransportControllerAdapter::AttachAudioSender( |
| 609 | OrtcRtpSenderAdapter* sender, |
| 610 | RtpTransportInterface* inner_transport) { |
| 611 | if (have_audio_sender_) { |
| 612 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_OPERATION, |
| 613 | "Using two audio RtpSenders with the same " |
| 614 | "RtpTransportControllerAdapter is not currently " |
| 615 | "supported."); |
| 616 | } |
| 617 | if (inner_audio_transport_ && inner_audio_transport_ != inner_transport) { |
| 618 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_OPERATION, |
| 619 | "Using different transports for the audio " |
| 620 | "RtpSender and RtpReceiver is not currently " |
| 621 | "supported."); |
| 622 | } |
zhihuang | d3501ad | 2017-03-03 14:39:06 -0800 | [diff] [blame] | 623 | RTCError err = MaybeSetCryptos(inner_transport, &local_audio_description_, |
| 624 | &remote_audio_description_); |
| 625 | if (!err.ok()) { |
| 626 | return err; |
| 627 | } |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 628 | // If setting new transport, extract its RTCP parameters and create voice |
| 629 | // channel. |
| 630 | if (!inner_audio_transport_) { |
| 631 | CopyRtcpParametersToDescriptions(inner_transport->GetRtcpParameters(), |
| 632 | &local_audio_description_, |
| 633 | &remote_audio_description_); |
| 634 | inner_audio_transport_ = inner_transport; |
| 635 | CreateVoiceChannel(); |
| 636 | } |
| 637 | have_audio_sender_ = true; |
| 638 | sender->SignalDestroyed.connect( |
| 639 | this, &RtpTransportControllerAdapter::OnAudioSenderDestroyed); |
| 640 | return RTCError::OK(); |
| 641 | } |
| 642 | |
| 643 | RTCError RtpTransportControllerAdapter::AttachVideoSender( |
| 644 | OrtcRtpSenderAdapter* sender, |
| 645 | RtpTransportInterface* inner_transport) { |
| 646 | if (have_video_sender_) { |
| 647 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_OPERATION, |
| 648 | "Using two video RtpSenders with the same " |
| 649 | "RtpTransportControllerAdapter is not currently " |
| 650 | "supported."); |
| 651 | } |
| 652 | if (inner_video_transport_ && inner_video_transport_ != inner_transport) { |
| 653 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_OPERATION, |
| 654 | "Using different transports for the video " |
| 655 | "RtpSender and RtpReceiver is not currently " |
| 656 | "supported."); |
| 657 | } |
zhihuang | d3501ad | 2017-03-03 14:39:06 -0800 | [diff] [blame] | 658 | RTCError err = MaybeSetCryptos(inner_transport, &local_video_description_, |
| 659 | &remote_video_description_); |
| 660 | if (!err.ok()) { |
| 661 | return err; |
| 662 | } |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 663 | // If setting new transport, extract its RTCP parameters and create video |
| 664 | // channel. |
| 665 | if (!inner_video_transport_) { |
| 666 | CopyRtcpParametersToDescriptions(inner_transport->GetRtcpParameters(), |
| 667 | &local_video_description_, |
| 668 | &remote_video_description_); |
| 669 | inner_video_transport_ = inner_transport; |
| 670 | CreateVideoChannel(); |
| 671 | } |
| 672 | have_video_sender_ = true; |
| 673 | sender->SignalDestroyed.connect( |
| 674 | this, &RtpTransportControllerAdapter::OnVideoSenderDestroyed); |
| 675 | return RTCError::OK(); |
| 676 | } |
| 677 | |
| 678 | RTCError RtpTransportControllerAdapter::AttachAudioReceiver( |
| 679 | OrtcRtpReceiverAdapter* receiver, |
| 680 | RtpTransportInterface* inner_transport) { |
| 681 | if (have_audio_receiver_) { |
| 682 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_OPERATION, |
| 683 | "Using two audio RtpReceivers with the same " |
| 684 | "RtpTransportControllerAdapter is not currently " |
| 685 | "supported."); |
| 686 | } |
| 687 | if (inner_audio_transport_ && inner_audio_transport_ != inner_transport) { |
| 688 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_OPERATION, |
| 689 | "Using different transports for the audio " |
| 690 | "RtpReceiver and RtpReceiver is not currently " |
| 691 | "supported."); |
| 692 | } |
zhihuang | d3501ad | 2017-03-03 14:39:06 -0800 | [diff] [blame] | 693 | RTCError err = MaybeSetCryptos(inner_transport, &local_audio_description_, |
| 694 | &remote_audio_description_); |
| 695 | if (!err.ok()) { |
| 696 | return err; |
| 697 | } |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 698 | // If setting new transport, extract its RTCP parameters and create voice |
| 699 | // channel. |
| 700 | if (!inner_audio_transport_) { |
| 701 | CopyRtcpParametersToDescriptions(inner_transport->GetRtcpParameters(), |
| 702 | &local_audio_description_, |
| 703 | &remote_audio_description_); |
| 704 | inner_audio_transport_ = inner_transport; |
| 705 | CreateVoiceChannel(); |
| 706 | } |
| 707 | have_audio_receiver_ = true; |
| 708 | receiver->SignalDestroyed.connect( |
| 709 | this, &RtpTransportControllerAdapter::OnAudioReceiverDestroyed); |
| 710 | return RTCError::OK(); |
| 711 | } |
| 712 | |
| 713 | RTCError RtpTransportControllerAdapter::AttachVideoReceiver( |
| 714 | OrtcRtpReceiverAdapter* receiver, |
| 715 | RtpTransportInterface* inner_transport) { |
| 716 | if (have_video_receiver_) { |
| 717 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_OPERATION, |
| 718 | "Using two video RtpReceivers with the same " |
| 719 | "RtpTransportControllerAdapter is not currently " |
| 720 | "supported."); |
| 721 | } |
| 722 | if (inner_video_transport_ && inner_video_transport_ != inner_transport) { |
| 723 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_OPERATION, |
| 724 | "Using different transports for the video " |
| 725 | "RtpReceiver and RtpReceiver is not currently " |
| 726 | "supported."); |
| 727 | } |
zhihuang | d3501ad | 2017-03-03 14:39:06 -0800 | [diff] [blame] | 728 | RTCError err = MaybeSetCryptos(inner_transport, &local_video_description_, |
| 729 | &remote_video_description_); |
| 730 | if (!err.ok()) { |
| 731 | return err; |
| 732 | } |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 733 | // If setting new transport, extract its RTCP parameters and create video |
| 734 | // channel. |
| 735 | if (!inner_video_transport_) { |
| 736 | CopyRtcpParametersToDescriptions(inner_transport->GetRtcpParameters(), |
| 737 | &local_video_description_, |
| 738 | &remote_video_description_); |
| 739 | inner_video_transport_ = inner_transport; |
| 740 | CreateVideoChannel(); |
| 741 | } |
| 742 | have_video_receiver_ = true; |
| 743 | receiver->SignalDestroyed.connect( |
| 744 | this, &RtpTransportControllerAdapter::OnVideoReceiverDestroyed); |
| 745 | return RTCError::OK(); |
| 746 | } |
| 747 | |
| 748 | void RtpTransportControllerAdapter::OnRtpTransportDestroyed( |
| 749 | RtpTransportAdapter* transport) { |
| 750 | RTC_DCHECK_RUN_ON(signaling_thread_); |
| 751 | auto it = std::find_if(transport_proxies_.begin(), transport_proxies_.end(), |
| 752 | [transport](RtpTransportInterface* proxy) { |
| 753 | return proxy->GetInternal() == transport; |
| 754 | }); |
| 755 | if (it == transport_proxies_.end()) { |
| 756 | RTC_NOTREACHED(); |
| 757 | return; |
| 758 | } |
| 759 | transport_proxies_.erase(it); |
| 760 | } |
| 761 | |
| 762 | void RtpTransportControllerAdapter::OnAudioSenderDestroyed() { |
| 763 | if (!have_audio_sender_) { |
| 764 | RTC_NOTREACHED(); |
| 765 | return; |
| 766 | } |
| 767 | // Empty parameters should result in sending being stopped. |
| 768 | RTCError err = |
| 769 | ValidateAndApplyAudioSenderParameters(RtpParameters(), nullptr); |
| 770 | RTC_DCHECK(err.ok()); |
| 771 | have_audio_sender_ = false; |
| 772 | if (!have_audio_receiver_) { |
| 773 | DestroyVoiceChannel(); |
| 774 | } |
| 775 | } |
| 776 | |
| 777 | void RtpTransportControllerAdapter::OnVideoSenderDestroyed() { |
| 778 | if (!have_video_sender_) { |
| 779 | RTC_NOTREACHED(); |
| 780 | return; |
| 781 | } |
| 782 | // Empty parameters should result in sending being stopped. |
| 783 | RTCError err = |
| 784 | ValidateAndApplyVideoSenderParameters(RtpParameters(), nullptr); |
| 785 | RTC_DCHECK(err.ok()); |
| 786 | have_video_sender_ = false; |
| 787 | if (!have_video_receiver_) { |
| 788 | DestroyVideoChannel(); |
| 789 | } |
| 790 | } |
| 791 | |
| 792 | void RtpTransportControllerAdapter::OnAudioReceiverDestroyed() { |
| 793 | if (!have_audio_receiver_) { |
| 794 | RTC_NOTREACHED(); |
| 795 | return; |
| 796 | } |
| 797 | // Empty parameters should result in receiving being stopped. |
| 798 | RTCError err = ValidateAndApplyAudioReceiverParameters(RtpParameters()); |
| 799 | RTC_DCHECK(err.ok()); |
| 800 | have_audio_receiver_ = false; |
| 801 | if (!have_audio_sender_) { |
| 802 | DestroyVoiceChannel(); |
| 803 | } |
| 804 | } |
| 805 | |
| 806 | void RtpTransportControllerAdapter::OnVideoReceiverDestroyed() { |
| 807 | if (!have_video_receiver_) { |
| 808 | RTC_NOTREACHED(); |
| 809 | return; |
| 810 | } |
| 811 | // Empty parameters should result in receiving being stopped. |
| 812 | RTCError err = ValidateAndApplyVideoReceiverParameters(RtpParameters()); |
| 813 | RTC_DCHECK(err.ok()); |
| 814 | have_video_receiver_ = false; |
| 815 | if (!have_video_sender_) { |
| 816 | DestroyVideoChannel(); |
| 817 | } |
| 818 | } |
| 819 | |
| 820 | void RtpTransportControllerAdapter::CreateVoiceChannel() { |
| 821 | voice_channel_ = media_controller_->channel_manager()->CreateVoiceChannel( |
| 822 | media_controller_.get(), |
| 823 | inner_audio_transport_->GetRtpPacketTransport()->GetInternal(), |
| 824 | inner_audio_transport_->GetRtcpPacketTransport() |
| 825 | ? inner_audio_transport_->GetRtcpPacketTransport()->GetInternal() |
| 826 | : nullptr, |
| 827 | signaling_thread_, "audio", false, cricket::AudioOptions()); |
| 828 | RTC_DCHECK(voice_channel_); |
| 829 | voice_channel_->Enable(true); |
| 830 | } |
| 831 | |
| 832 | void RtpTransportControllerAdapter::CreateVideoChannel() { |
| 833 | video_channel_ = media_controller_->channel_manager()->CreateVideoChannel( |
| 834 | media_controller_.get(), |
| 835 | inner_video_transport_->GetRtpPacketTransport()->GetInternal(), |
| 836 | inner_video_transport_->GetRtcpPacketTransport() |
| 837 | ? inner_video_transport_->GetRtcpPacketTransport()->GetInternal() |
| 838 | : nullptr, |
| 839 | signaling_thread_, "video", false, cricket::VideoOptions()); |
| 840 | RTC_DCHECK(video_channel_); |
| 841 | video_channel_->Enable(true); |
| 842 | } |
| 843 | |
| 844 | void RtpTransportControllerAdapter::DestroyVoiceChannel() { |
| 845 | RTC_DCHECK(voice_channel_); |
| 846 | media_controller_->channel_manager()->DestroyVoiceChannel(voice_channel_); |
| 847 | voice_channel_ = nullptr; |
| 848 | inner_audio_transport_ = nullptr; |
| 849 | } |
| 850 | |
| 851 | void RtpTransportControllerAdapter::DestroyVideoChannel() { |
| 852 | RTC_DCHECK(video_channel_); |
| 853 | media_controller_->channel_manager()->DestroyVideoChannel(video_channel_); |
| 854 | video_channel_ = nullptr; |
| 855 | inner_video_transport_ = nullptr; |
| 856 | } |
| 857 | |
| 858 | void RtpTransportControllerAdapter::CopyRtcpParametersToDescriptions( |
| 859 | const RtcpParameters& params, |
| 860 | cricket::MediaContentDescription* local, |
| 861 | cricket::MediaContentDescription* remote) { |
| 862 | local->set_rtcp_mux(params.mux); |
| 863 | remote->set_rtcp_mux(params.mux); |
| 864 | local->set_rtcp_reduced_size(params.reduced_size); |
| 865 | remote->set_rtcp_reduced_size(params.reduced_size); |
| 866 | for (cricket::StreamParams& stream_params : local->mutable_streams()) { |
| 867 | stream_params.cname = params.cname; |
| 868 | } |
| 869 | } |
| 870 | |
| 871 | uint32_t RtpTransportControllerAdapter::GenerateUnusedSsrc( |
| 872 | std::set<uint32_t>* new_ssrcs) const { |
| 873 | uint32_t ssrc; |
| 874 | do { |
| 875 | ssrc = rtc::CreateRandomNonZeroId(); |
| 876 | } while ( |
| 877 | cricket::GetStreamBySsrc(local_audio_description_.streams(), ssrc) || |
| 878 | cricket::GetStreamBySsrc(remote_audio_description_.streams(), ssrc) || |
| 879 | cricket::GetStreamBySsrc(local_video_description_.streams(), ssrc) || |
| 880 | cricket::GetStreamBySsrc(remote_video_description_.streams(), ssrc) || |
| 881 | !new_ssrcs->insert(ssrc).second); |
| 882 | return ssrc; |
| 883 | } |
| 884 | |
| 885 | RTCErrorOr<cricket::StreamParamsVec> |
| 886 | RtpTransportControllerAdapter::MakeSendStreamParamsVec( |
| 887 | std::vector<RtpEncodingParameters> encodings, |
| 888 | const std::string& cname, |
| 889 | const cricket::MediaContentDescription& description) const { |
| 890 | if (encodings.size() > 1u) { |
| 891 | LOG_AND_RETURN_ERROR(webrtc::RTCErrorType::UNSUPPORTED_PARAMETER, |
| 892 | "ORTC API implementation doesn't currently " |
| 893 | "support simulcast or layered encodings."); |
| 894 | } else if (encodings.empty()) { |
| 895 | return cricket::StreamParamsVec(); |
| 896 | } |
| 897 | RtpEncodingParameters& encoding = encodings[0]; |
| 898 | std::set<uint32_t> new_ssrcs; |
| 899 | if (encoding.ssrc) { |
| 900 | new_ssrcs.insert(*encoding.ssrc); |
| 901 | } |
| 902 | if (encoding.rtx && encoding.rtx->ssrc) { |
| 903 | new_ssrcs.insert(*encoding.rtx->ssrc); |
| 904 | } |
| 905 | // May need to fill missing SSRCs with generated ones. |
| 906 | if (!encoding.ssrc) { |
| 907 | if (!description.streams().empty()) { |
| 908 | encoding.ssrc.emplace(description.streams()[0].first_ssrc()); |
| 909 | } else { |
| 910 | encoding.ssrc.emplace(GenerateUnusedSsrc(&new_ssrcs)); |
| 911 | } |
| 912 | } |
| 913 | if (encoding.rtx && !encoding.rtx->ssrc) { |
| 914 | uint32_t existing_rtx_ssrc; |
| 915 | if (!description.streams().empty() && |
| 916 | description.streams()[0].GetFidSsrc( |
| 917 | description.streams()[0].first_ssrc(), &existing_rtx_ssrc)) { |
| 918 | encoding.rtx->ssrc.emplace(existing_rtx_ssrc); |
| 919 | } else { |
| 920 | encoding.rtx->ssrc.emplace(GenerateUnusedSsrc(&new_ssrcs)); |
| 921 | } |
| 922 | } |
| 923 | |
| 924 | auto result = ToCricketStreamParamsVec(encodings); |
| 925 | if (!result.ok()) { |
| 926 | return result.MoveError(); |
| 927 | } |
| 928 | // If conversion was successful, there should be one StreamParams. |
| 929 | RTC_DCHECK_EQ(1u, result.value().size()); |
| 930 | result.value()[0].cname = cname; |
| 931 | return result; |
| 932 | } |
| 933 | |
zhihuang | d3501ad | 2017-03-03 14:39:06 -0800 | [diff] [blame] | 934 | RTCError RtpTransportControllerAdapter::MaybeSetCryptos( |
| 935 | RtpTransportInterface* rtp_transport, |
| 936 | cricket::MediaContentDescription* local_description, |
| 937 | cricket::MediaContentDescription* remote_description) { |
| 938 | if (rtp_transport->GetInternal()->is_srtp_transport()) { |
| 939 | if (!rtp_transport->GetInternal()->send_key() || |
| 940 | !rtp_transport->GetInternal()->receive_key()) { |
| 941 | LOG_AND_RETURN_ERROR(webrtc::RTCErrorType::UNSUPPORTED_PARAMETER, |
| 942 | "The SRTP send key or receive key is not set.") |
| 943 | } |
| 944 | std::vector<cricket::CryptoParams> cryptos; |
| 945 | cryptos.push_back(*(rtp_transport->GetInternal()->receive_key())); |
| 946 | local_description->set_cryptos(cryptos); |
| 947 | |
| 948 | cryptos.clear(); |
| 949 | cryptos.push_back(*(rtp_transport->GetInternal()->send_key())); |
| 950 | remote_description->set_cryptos(cryptos); |
| 951 | } |
| 952 | return RTCError::OK(); |
| 953 | } |
| 954 | |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 955 | } // namespace webrtc |