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