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" |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 25 | #include "pc/rtpmediautils.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 26 | #include "rtc_base/checks.h" |
Sebastian Jansson | 9a03dd8 | 2018-02-22 10:31:14 +0100 | [diff] [blame] | 27 | #include "rtc_base/ptr_util.h" |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 28 | |
| 29 | namespace webrtc { |
| 30 | |
| 31 | // Note: It's assumed that each individual list doesn't have conflicts, since |
| 32 | // they should have been detected already by rtpparametersconversion.cc. This |
| 33 | // only needs to detect conflicts *between* A and B. |
| 34 | template <typename C1, typename C2> |
| 35 | static RTCError CheckForIdConflicts( |
| 36 | const std::vector<C1>& codecs_a, |
| 37 | const cricket::RtpHeaderExtensions& extensions_a, |
| 38 | const cricket::StreamParamsVec& streams_a, |
| 39 | const std::vector<C2>& codecs_b, |
| 40 | const cricket::RtpHeaderExtensions& extensions_b, |
| 41 | const cricket::StreamParamsVec& streams_b) { |
| 42 | std::ostringstream oss; |
| 43 | // Since it's assumed that C1 and C2 are different types, codecs_a and |
| 44 | // codecs_b should never contain the same payload type, and thus we can just |
| 45 | // use a set. |
| 46 | std::set<int> seen_payload_types; |
| 47 | for (const C1& codec : codecs_a) { |
| 48 | seen_payload_types.insert(codec.id); |
| 49 | } |
| 50 | for (const C2& codec : codecs_b) { |
| 51 | if (!seen_payload_types.insert(codec.id).second) { |
| 52 | oss << "Same payload type used for audio and video codecs: " << codec.id; |
| 53 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, oss.str()); |
| 54 | } |
| 55 | } |
| 56 | // Audio and video *may* use the same header extensions, so use a map. |
| 57 | std::unordered_map<int, std::string> seen_extensions; |
| 58 | for (const webrtc::RtpExtension& extension : extensions_a) { |
| 59 | seen_extensions[extension.id] = extension.uri; |
| 60 | } |
| 61 | for (const webrtc::RtpExtension& extension : extensions_b) { |
| 62 | if (seen_extensions.find(extension.id) != seen_extensions.end() && |
| 63 | seen_extensions.at(extension.id) != extension.uri) { |
| 64 | oss << "Same ID used for different RTP header extensions: " |
| 65 | << extension.id; |
| 66 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, oss.str()); |
| 67 | } |
| 68 | } |
| 69 | std::set<uint32_t> seen_ssrcs; |
| 70 | for (const cricket::StreamParams& stream : streams_a) { |
| 71 | seen_ssrcs.insert(stream.ssrcs.begin(), stream.ssrcs.end()); |
| 72 | } |
| 73 | for (const cricket::StreamParams& stream : streams_b) { |
| 74 | for (uint32_t ssrc : stream.ssrcs) { |
| 75 | if (!seen_ssrcs.insert(ssrc).second) { |
| 76 | oss << "Same SSRC used for audio and video senders: " << ssrc; |
| 77 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, oss.str()); |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | return RTCError::OK(); |
| 82 | } |
| 83 | |
| 84 | BEGIN_OWNED_PROXY_MAP(RtpTransportController) |
| 85 | PROXY_SIGNALING_THREAD_DESTRUCTOR() |
| 86 | PROXY_CONSTMETHOD0(std::vector<RtpTransportInterface*>, GetTransports) |
| 87 | protected: |
| 88 | RtpTransportControllerAdapter* GetInternal() override { |
| 89 | return internal(); |
| 90 | } |
| 91 | END_PROXY_MAP() |
| 92 | |
| 93 | // static |
| 94 | std::unique_ptr<RtpTransportControllerInterface> |
| 95 | RtpTransportControllerAdapter::CreateProxied( |
| 96 | const cricket::MediaConfig& config, |
| 97 | cricket::ChannelManager* channel_manager, |
| 98 | webrtc::RtcEventLog* event_log, |
| 99 | rtc::Thread* signaling_thread, |
| 100 | rtc::Thread* worker_thread) { |
| 101 | std::unique_ptr<RtpTransportControllerAdapter> wrapped( |
| 102 | new RtpTransportControllerAdapter(config, channel_manager, event_log, |
| 103 | signaling_thread, worker_thread)); |
| 104 | return RtpTransportControllerProxyWithInternal< |
| 105 | RtpTransportControllerAdapter>::Create(signaling_thread, worker_thread, |
| 106 | std::move(wrapped)); |
| 107 | } |
| 108 | |
| 109 | RtpTransportControllerAdapter::~RtpTransportControllerAdapter() { |
| 110 | RTC_DCHECK_RUN_ON(signaling_thread_); |
| 111 | if (!transport_proxies_.empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 112 | RTC_LOG(LS_ERROR) |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 113 | << "Destroying RtpTransportControllerAdapter while RtpTransports " |
| 114 | "are still using it; this is unsafe."; |
| 115 | } |
| 116 | if (voice_channel_) { |
| 117 | // This would mean audio RTP senders/receivers that are using us haven't |
| 118 | // been destroyed. This isn't safe (see error log above). |
| 119 | DestroyVoiceChannel(); |
| 120 | } |
| 121 | if (voice_channel_) { |
| 122 | // This would mean video RTP senders/receivers that are using us haven't |
| 123 | // been destroyed. This isn't safe (see error log above). |
| 124 | DestroyVideoChannel(); |
| 125 | } |
nisse | eaabdf6 | 2017-05-05 02:23:02 -0700 | [diff] [blame] | 126 | // Call must be destroyed on the worker thread. |
| 127 | worker_thread_->Invoke<void>( |
| 128 | RTC_FROM_HERE, |
| 129 | rtc::Bind(&RtpTransportControllerAdapter::Close_w, this)); |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | RTCErrorOr<std::unique_ptr<RtpTransportInterface>> |
| 133 | RtpTransportControllerAdapter::CreateProxiedRtpTransport( |
sprang | db2a9fc | 2017-08-09 06:42:32 -0700 | [diff] [blame] | 134 | const RtpTransportParameters& parameters, |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 135 | PacketTransportInterface* rtp, |
| 136 | PacketTransportInterface* rtcp) { |
sprang | db2a9fc | 2017-08-09 06:42:32 -0700 | [diff] [blame] | 137 | if (!transport_proxies_.empty() && (parameters.keepalive != keepalive_)) { |
| 138 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_MODIFICATION, |
| 139 | "Cannot create RtpTransport with different keep-alive " |
| 140 | "from the RtpTransports already associated with this " |
| 141 | "transport controller."); |
| 142 | } |
| 143 | auto result = RtpTransportAdapter::CreateProxied(parameters, rtp, rtcp, this); |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 144 | if (result.ok()) { |
| 145 | transport_proxies_.push_back(result.value().get()); |
| 146 | transport_proxies_.back()->GetInternal()->SignalDestroyed.connect( |
| 147 | this, &RtpTransportControllerAdapter::OnRtpTransportDestroyed); |
| 148 | } |
| 149 | return result; |
| 150 | } |
| 151 | |
zhihuang | d3501ad | 2017-03-03 14:39:06 -0800 | [diff] [blame] | 152 | RTCErrorOr<std::unique_ptr<SrtpTransportInterface>> |
| 153 | RtpTransportControllerAdapter::CreateProxiedSrtpTransport( |
sprang | db2a9fc | 2017-08-09 06:42:32 -0700 | [diff] [blame] | 154 | const RtpTransportParameters& parameters, |
zhihuang | d3501ad | 2017-03-03 14:39:06 -0800 | [diff] [blame] | 155 | PacketTransportInterface* rtp, |
| 156 | PacketTransportInterface* rtcp) { |
| 157 | auto result = |
sprang | db2a9fc | 2017-08-09 06:42:32 -0700 | [diff] [blame] | 158 | RtpTransportAdapter::CreateSrtpProxied(parameters, rtp, rtcp, this); |
zhihuang | d3501ad | 2017-03-03 14:39:06 -0800 | [diff] [blame] | 159 | if (result.ok()) { |
| 160 | transport_proxies_.push_back(result.value().get()); |
| 161 | transport_proxies_.back()->GetInternal()->SignalDestroyed.connect( |
| 162 | this, &RtpTransportControllerAdapter::OnRtpTransportDestroyed); |
| 163 | } |
| 164 | return result; |
| 165 | } |
| 166 | |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 167 | RTCErrorOr<std::unique_ptr<OrtcRtpSenderInterface>> |
| 168 | RtpTransportControllerAdapter::CreateProxiedRtpSender( |
| 169 | cricket::MediaType kind, |
| 170 | RtpTransportInterface* transport_proxy) { |
| 171 | RTC_DCHECK(transport_proxy); |
| 172 | RTC_DCHECK(std::find(transport_proxies_.begin(), transport_proxies_.end(), |
| 173 | transport_proxy) != transport_proxies_.end()); |
| 174 | std::unique_ptr<OrtcRtpSenderAdapter> new_sender( |
| 175 | new OrtcRtpSenderAdapter(kind, transport_proxy, this)); |
| 176 | RTCError err; |
| 177 | switch (kind) { |
| 178 | case cricket::MEDIA_TYPE_AUDIO: |
| 179 | err = AttachAudioSender(new_sender.get(), transport_proxy->GetInternal()); |
| 180 | break; |
| 181 | case cricket::MEDIA_TYPE_VIDEO: |
| 182 | err = AttachVideoSender(new_sender.get(), transport_proxy->GetInternal()); |
| 183 | break; |
| 184 | case cricket::MEDIA_TYPE_DATA: |
| 185 | RTC_NOTREACHED(); |
| 186 | } |
| 187 | if (!err.ok()) { |
| 188 | return std::move(err); |
| 189 | } |
| 190 | |
| 191 | return OrtcRtpSenderAdapter::CreateProxy(std::move(new_sender)); |
| 192 | } |
| 193 | |
| 194 | RTCErrorOr<std::unique_ptr<OrtcRtpReceiverInterface>> |
| 195 | RtpTransportControllerAdapter::CreateProxiedRtpReceiver( |
| 196 | cricket::MediaType kind, |
| 197 | RtpTransportInterface* transport_proxy) { |
| 198 | RTC_DCHECK(transport_proxy); |
| 199 | RTC_DCHECK(std::find(transport_proxies_.begin(), transport_proxies_.end(), |
| 200 | transport_proxy) != transport_proxies_.end()); |
| 201 | std::unique_ptr<OrtcRtpReceiverAdapter> new_receiver( |
| 202 | new OrtcRtpReceiverAdapter(kind, transport_proxy, this)); |
| 203 | RTCError err; |
| 204 | switch (kind) { |
| 205 | case cricket::MEDIA_TYPE_AUDIO: |
| 206 | err = AttachAudioReceiver(new_receiver.get(), |
| 207 | transport_proxy->GetInternal()); |
| 208 | break; |
| 209 | case cricket::MEDIA_TYPE_VIDEO: |
| 210 | err = AttachVideoReceiver(new_receiver.get(), |
| 211 | transport_proxy->GetInternal()); |
| 212 | break; |
| 213 | case cricket::MEDIA_TYPE_DATA: |
| 214 | RTC_NOTREACHED(); |
| 215 | } |
| 216 | if (!err.ok()) { |
| 217 | return std::move(err); |
| 218 | } |
| 219 | |
| 220 | return OrtcRtpReceiverAdapter::CreateProxy(std::move(new_receiver)); |
| 221 | } |
| 222 | |
| 223 | std::vector<RtpTransportInterface*> |
| 224 | RtpTransportControllerAdapter::GetTransports() const { |
| 225 | RTC_DCHECK_RUN_ON(signaling_thread_); |
| 226 | return transport_proxies_; |
| 227 | } |
| 228 | |
sprang | db2a9fc | 2017-08-09 06:42:32 -0700 | [diff] [blame] | 229 | RTCError RtpTransportControllerAdapter::SetRtpTransportParameters( |
| 230 | const RtpTransportParameters& parameters, |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 231 | RtpTransportInterface* inner_transport) { |
sprang | db2a9fc | 2017-08-09 06:42:32 -0700 | [diff] [blame] | 232 | if ((video_channel_ != nullptr || voice_channel_ != nullptr) && |
| 233 | (parameters.keepalive != keepalive_)) { |
| 234 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_MODIFICATION, |
| 235 | "Cannot change keep-alive settings after creating " |
| 236 | "media streams or additional transports for the same " |
| 237 | "transport controller."); |
| 238 | } |
| 239 | // Call must be configured on the worker thread. |
| 240 | worker_thread_->Invoke<void>( |
| 241 | RTC_FROM_HERE, |
| 242 | rtc::Bind(&RtpTransportControllerAdapter::SetRtpTransportParameters_w, |
| 243 | this, parameters)); |
| 244 | |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 245 | do { |
| 246 | if (inner_transport == inner_audio_transport_) { |
sprang | db2a9fc | 2017-08-09 06:42:32 -0700 | [diff] [blame] | 247 | CopyRtcpParametersToDescriptions(parameters.rtcp, |
| 248 | &local_audio_description_, |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 249 | &remote_audio_description_); |
| 250 | if (!voice_channel_->SetLocalContent(&local_audio_description_, |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 251 | SdpType::kOffer, nullptr)) { |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 252 | break; |
| 253 | } |
| 254 | if (!voice_channel_->SetRemoteContent(&remote_audio_description_, |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 255 | SdpType::kAnswer, nullptr)) { |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 256 | break; |
| 257 | } |
| 258 | } else if (inner_transport == inner_video_transport_) { |
sprang | db2a9fc | 2017-08-09 06:42:32 -0700 | [diff] [blame] | 259 | CopyRtcpParametersToDescriptions(parameters.rtcp, |
| 260 | &local_video_description_, |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 261 | &remote_video_description_); |
| 262 | if (!video_channel_->SetLocalContent(&local_video_description_, |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 263 | SdpType::kOffer, nullptr)) { |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 264 | break; |
| 265 | } |
| 266 | if (!video_channel_->SetRemoteContent(&remote_video_description_, |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 267 | SdpType::kAnswer, nullptr)) { |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 268 | break; |
| 269 | } |
| 270 | } |
| 271 | return RTCError::OK(); |
| 272 | } while (false); |
| 273 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 274 | "Failed to apply new RTCP parameters."); |
| 275 | } |
| 276 | |
sprang | db2a9fc | 2017-08-09 06:42:32 -0700 | [diff] [blame] | 277 | void RtpTransportControllerAdapter::SetRtpTransportParameters_w( |
| 278 | const RtpTransportParameters& parameters) { |
| 279 | call_send_rtp_transport_controller_->SetKeepAliveConfig(parameters.keepalive); |
| 280 | } |
| 281 | |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 282 | RTCError RtpTransportControllerAdapter::ValidateAndApplyAudioSenderParameters( |
| 283 | const RtpParameters& parameters, |
| 284 | uint32_t* primary_ssrc) { |
| 285 | RTC_DCHECK(voice_channel_); |
| 286 | RTC_DCHECK(have_audio_sender_); |
| 287 | |
| 288 | auto codecs_result = ToCricketCodecs<cricket::AudioCodec>(parameters.codecs); |
| 289 | if (!codecs_result.ok()) { |
| 290 | return codecs_result.MoveError(); |
| 291 | } |
| 292 | |
| 293 | auto extensions_result = |
| 294 | ToCricketRtpHeaderExtensions(parameters.header_extensions); |
| 295 | if (!extensions_result.ok()) { |
| 296 | return extensions_result.MoveError(); |
| 297 | } |
| 298 | |
| 299 | auto stream_params_result = MakeSendStreamParamsVec( |
sprang | db2a9fc | 2017-08-09 06:42:32 -0700 | [diff] [blame] | 300 | parameters.encodings, inner_audio_transport_->GetParameters().rtcp.cname, |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 301 | local_audio_description_); |
| 302 | if (!stream_params_result.ok()) { |
| 303 | return stream_params_result.MoveError(); |
| 304 | } |
| 305 | |
| 306 | // Check that audio/video sender aren't using the same IDs to refer to |
| 307 | // different things, if they share the same transport. |
| 308 | if (inner_audio_transport_ == inner_video_transport_) { |
| 309 | RTCError err = CheckForIdConflicts( |
| 310 | codecs_result.value(), extensions_result.value(), |
| 311 | stream_params_result.value(), remote_video_description_.codecs(), |
| 312 | remote_video_description_.rtp_header_extensions(), |
| 313 | local_video_description_.streams()); |
| 314 | if (!err.ok()) { |
| 315 | return err; |
| 316 | } |
| 317 | } |
| 318 | |
Steve Anton | 4e70a72 | 2017-11-28 14:57:10 -0800 | [diff] [blame] | 319 | bool local_send = false; |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 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 | } |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 325 | local_send = parameters.encodings[0].active; |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 326 | } |
Steve Anton | 4e70a72 | 2017-11-28 14:57:10 -0800 | [diff] [blame] | 327 | const bool local_recv = |
| 328 | RtpTransceiverDirectionHasRecv(local_audio_description_.direction()); |
| 329 | const auto local_direction = |
| 330 | RtpTransceiverDirectionFromSendRecv(local_send, local_recv); |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 331 | if (primary_ssrc && !stream_params_result.value().empty()) { |
| 332 | *primary_ssrc = stream_params_result.value()[0].first_ssrc(); |
| 333 | } |
| 334 | |
| 335 | // Validation is done, so we can attempt applying the descriptions. Sent |
| 336 | // codecs and header extensions go in remote description, streams go in |
| 337 | // local. |
| 338 | // |
| 339 | // If there are no codecs or encodings, just leave the previous set of |
| 340 | // codecs. The media engine doesn't like an empty set of codecs. |
| 341 | if (local_audio_description_.streams().empty() && |
| 342 | remote_audio_description_.codecs().empty()) { |
| 343 | } else { |
| 344 | remote_audio_description_.set_codecs(codecs_result.MoveValue()); |
| 345 | } |
| 346 | remote_audio_description_.set_rtp_header_extensions( |
| 347 | extensions_result.MoveValue()); |
| 348 | remote_audio_description_.set_bandwidth(bandwidth); |
| 349 | local_audio_description_.mutable_streams() = stream_params_result.MoveValue(); |
| 350 | // Direction set based on encoding "active" flag. |
Steve Anton | 4e70a72 | 2017-11-28 14:57:10 -0800 | [diff] [blame] | 351 | local_audio_description_.set_direction(local_direction); |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 352 | remote_audio_description_.set_direction( |
Steve Anton | 4e70a72 | 2017-11-28 14:57:10 -0800 | [diff] [blame] | 353 | RtpTransceiverDirectionReversed(local_direction)); |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 354 | |
| 355 | // Set remote content first, to ensure the stream is created with the correct |
| 356 | // codec. |
| 357 | if (!voice_channel_->SetRemoteContent(&remote_audio_description_, |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 358 | SdpType::kOffer, nullptr)) { |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 359 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 360 | "Failed to apply remote parameters to media channel."); |
| 361 | } |
| 362 | if (!voice_channel_->SetLocalContent(&local_audio_description_, |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 363 | SdpType::kAnswer, nullptr)) { |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 364 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 365 | "Failed to apply local parameters to media channel."); |
| 366 | } |
| 367 | return RTCError::OK(); |
| 368 | } |
| 369 | |
| 370 | RTCError RtpTransportControllerAdapter::ValidateAndApplyVideoSenderParameters( |
| 371 | const RtpParameters& parameters, |
| 372 | uint32_t* primary_ssrc) { |
| 373 | RTC_DCHECK(video_channel_); |
| 374 | RTC_DCHECK(have_video_sender_); |
| 375 | |
| 376 | auto codecs_result = ToCricketCodecs<cricket::VideoCodec>(parameters.codecs); |
| 377 | if (!codecs_result.ok()) { |
| 378 | return codecs_result.MoveError(); |
| 379 | } |
| 380 | |
| 381 | auto extensions_result = |
| 382 | ToCricketRtpHeaderExtensions(parameters.header_extensions); |
| 383 | if (!extensions_result.ok()) { |
| 384 | return extensions_result.MoveError(); |
| 385 | } |
| 386 | |
| 387 | auto stream_params_result = MakeSendStreamParamsVec( |
sprang | db2a9fc | 2017-08-09 06:42:32 -0700 | [diff] [blame] | 388 | parameters.encodings, inner_video_transport_->GetParameters().rtcp.cname, |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 389 | local_video_description_); |
| 390 | if (!stream_params_result.ok()) { |
| 391 | return stream_params_result.MoveError(); |
| 392 | } |
| 393 | |
| 394 | // Check that audio/video sender aren't using the same IDs to refer to |
| 395 | // different things, if they share the same transport. |
| 396 | if (inner_audio_transport_ == inner_video_transport_) { |
| 397 | RTCError err = CheckForIdConflicts( |
| 398 | codecs_result.value(), extensions_result.value(), |
| 399 | stream_params_result.value(), remote_audio_description_.codecs(), |
| 400 | remote_audio_description_.rtp_header_extensions(), |
| 401 | local_audio_description_.streams()); |
| 402 | if (!err.ok()) { |
| 403 | return err; |
| 404 | } |
| 405 | } |
| 406 | |
Steve Anton | 4e70a72 | 2017-11-28 14:57:10 -0800 | [diff] [blame] | 407 | bool local_send = false; |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 408 | int bandwidth = cricket::kAutoBandwidth; |
| 409 | if (parameters.encodings.size() == 1u) { |
| 410 | if (parameters.encodings[0].max_bitrate_bps) { |
| 411 | bandwidth = *parameters.encodings[0].max_bitrate_bps; |
| 412 | } |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 413 | local_send = parameters.encodings[0].active; |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 414 | } |
Steve Anton | 4e70a72 | 2017-11-28 14:57:10 -0800 | [diff] [blame] | 415 | const bool local_recv = |
| 416 | RtpTransceiverDirectionHasRecv(local_audio_description_.direction()); |
| 417 | const auto local_direction = |
| 418 | RtpTransceiverDirectionFromSendRecv(local_send, local_recv); |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 419 | if (primary_ssrc && !stream_params_result.value().empty()) { |
| 420 | *primary_ssrc = stream_params_result.value()[0].first_ssrc(); |
| 421 | } |
| 422 | |
| 423 | // Validation is done, so we can attempt applying the descriptions. Sent |
| 424 | // codecs and header extensions go in remote description, streams go in |
| 425 | // local. |
| 426 | // |
| 427 | // If there are no codecs or encodings, just leave the previous set of |
| 428 | // codecs. The media engine doesn't like an empty set of codecs. |
| 429 | if (local_video_description_.streams().empty() && |
| 430 | remote_video_description_.codecs().empty()) { |
| 431 | } else { |
| 432 | remote_video_description_.set_codecs(codecs_result.MoveValue()); |
| 433 | } |
| 434 | remote_video_description_.set_rtp_header_extensions( |
| 435 | extensions_result.MoveValue()); |
| 436 | remote_video_description_.set_bandwidth(bandwidth); |
| 437 | local_video_description_.mutable_streams() = stream_params_result.MoveValue(); |
| 438 | // Direction set based on encoding "active" flag. |
Steve Anton | 4e70a72 | 2017-11-28 14:57:10 -0800 | [diff] [blame] | 439 | local_video_description_.set_direction(local_direction); |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 440 | remote_video_description_.set_direction( |
Steve Anton | 4e70a72 | 2017-11-28 14:57:10 -0800 | [diff] [blame] | 441 | RtpTransceiverDirectionReversed(local_direction)); |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 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_, |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 446 | SdpType::kOffer, nullptr)) { |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 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_, |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 451 | SdpType::kAnswer, nullptr)) { |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 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 | |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 474 | auto stream_params_result = ToCricketStreamParamsVec(parameters.encodings); |
| 475 | if (!stream_params_result.ok()) { |
| 476 | return stream_params_result.MoveError(); |
| 477 | } |
| 478 | |
| 479 | // Check that audio/video receive aren't using the same IDs to refer to |
| 480 | // different things, if they share the same transport. |
| 481 | if (inner_audio_transport_ == inner_video_transport_) { |
| 482 | RTCError err = CheckForIdConflicts( |
| 483 | codecs_result.value(), extensions_result.value(), |
| 484 | stream_params_result.value(), local_video_description_.codecs(), |
| 485 | local_video_description_.rtp_header_extensions(), |
| 486 | remote_video_description_.streams()); |
| 487 | if (!err.ok()) { |
| 488 | return err; |
| 489 | } |
| 490 | } |
| 491 | |
Steve Anton | 4e70a72 | 2017-11-28 14:57:10 -0800 | [diff] [blame] | 492 | const bool local_send = |
| 493 | RtpTransceiverDirectionHasSend(local_audio_description_.direction()); |
| 494 | const bool local_recv = |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 495 | !parameters.encodings.empty() && parameters.encodings[0].active; |
Steve Anton | 4e70a72 | 2017-11-28 14:57:10 -0800 | [diff] [blame] | 496 | const auto local_direction = |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 497 | RtpTransceiverDirectionFromSendRecv(local_send, local_recv); |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 498 | |
| 499 | // Validation is done, so we can attempt applying the descriptions. Received |
| 500 | // codecs and header extensions go in local description, streams go in |
| 501 | // remote. |
| 502 | // |
| 503 | // If there are no codecs or encodings, just leave the previous set of |
| 504 | // codecs. The media engine doesn't like an empty set of codecs. |
| 505 | if (remote_audio_description_.streams().empty() && |
| 506 | local_audio_description_.codecs().empty()) { |
| 507 | } else { |
| 508 | local_audio_description_.set_codecs(codecs_result.MoveValue()); |
| 509 | } |
| 510 | local_audio_description_.set_rtp_header_extensions( |
| 511 | extensions_result.MoveValue()); |
| 512 | remote_audio_description_.mutable_streams() = |
| 513 | stream_params_result.MoveValue(); |
| 514 | // Direction set based on encoding "active" flag. |
Steve Anton | 4e70a72 | 2017-11-28 14:57:10 -0800 | [diff] [blame] | 515 | local_audio_description_.set_direction(local_direction); |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 516 | remote_audio_description_.set_direction( |
Steve Anton | 4e70a72 | 2017-11-28 14:57:10 -0800 | [diff] [blame] | 517 | RtpTransceiverDirectionReversed(local_direction)); |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 518 | |
| 519 | if (!voice_channel_->SetLocalContent(&local_audio_description_, |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 520 | SdpType::kOffer, nullptr)) { |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 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_, |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 525 | SdpType::kAnswer, nullptr)) { |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 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 | |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 548 | int bandwidth = cricket::kAutoBandwidth; |
| 549 | auto stream_params_result = ToCricketStreamParamsVec(parameters.encodings); |
| 550 | if (!stream_params_result.ok()) { |
| 551 | return stream_params_result.MoveError(); |
| 552 | } |
| 553 | |
| 554 | // Check that audio/video receiver aren't using the same IDs to refer to |
| 555 | // different things, if they share the same transport. |
| 556 | if (inner_audio_transport_ == inner_video_transport_) { |
| 557 | RTCError err = CheckForIdConflicts( |
| 558 | codecs_result.value(), extensions_result.value(), |
| 559 | stream_params_result.value(), local_audio_description_.codecs(), |
| 560 | local_audio_description_.rtp_header_extensions(), |
| 561 | remote_audio_description_.streams()); |
| 562 | if (!err.ok()) { |
| 563 | return err; |
| 564 | } |
| 565 | } |
| 566 | |
Steve Anton | 4e70a72 | 2017-11-28 14:57:10 -0800 | [diff] [blame] | 567 | const bool local_send = |
| 568 | RtpTransceiverDirectionHasSend(local_video_description_.direction()); |
| 569 | const bool local_recv = |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 570 | !parameters.encodings.empty() && parameters.encodings[0].active; |
Steve Anton | 4e70a72 | 2017-11-28 14:57:10 -0800 | [diff] [blame] | 571 | const auto local_direction = |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 572 | RtpTransceiverDirectionFromSendRecv(local_send, local_recv); |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 573 | |
| 574 | // Validation is done, so we can attempt applying the descriptions. Received |
| 575 | // codecs and header extensions go in local description, streams go in |
| 576 | // remote. |
| 577 | // |
| 578 | // If there are no codecs or encodings, just leave the previous set of |
| 579 | // codecs. The media engine doesn't like an empty set of codecs. |
| 580 | if (remote_video_description_.streams().empty() && |
| 581 | local_video_description_.codecs().empty()) { |
| 582 | } else { |
| 583 | local_video_description_.set_codecs(codecs_result.MoveValue()); |
| 584 | } |
| 585 | local_video_description_.set_rtp_header_extensions( |
| 586 | extensions_result.MoveValue()); |
| 587 | local_video_description_.set_bandwidth(bandwidth); |
| 588 | remote_video_description_.mutable_streams() = |
| 589 | stream_params_result.MoveValue(); |
| 590 | // Direction set based on encoding "active" flag. |
Steve Anton | 4e70a72 | 2017-11-28 14:57:10 -0800 | [diff] [blame] | 591 | local_video_description_.set_direction(local_direction); |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 592 | remote_video_description_.set_direction( |
Steve Anton | 4e70a72 | 2017-11-28 14:57:10 -0800 | [diff] [blame] | 593 | RtpTransceiverDirectionReversed(local_direction)); |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 594 | |
| 595 | if (!video_channel_->SetLocalContent(&local_video_description_, |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 596 | SdpType::kOffer, nullptr)) { |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 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_, |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 601 | SdpType::kAnswer, nullptr)) { |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 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; |
Sebastian Jansson | 9a03dd8 | 2018-02-22 10:31:14 +0100 | [diff] [blame] | 654 | std::unique_ptr<RtpTransportControllerSend> controller_send = |
| 655 | rtc::MakeUnique<RtpTransportControllerSend>( |
| 656 | Clock::GetRealTimeClock(), event_log_, call_config.bitrate_config); |
| 657 | call_send_rtp_transport_controller_ = controller_send.get(); |
| 658 | call_.reset(webrtc::Call::Create(call_config, std::move(controller_send))); |
nisse | eaabdf6 | 2017-05-05 02:23:02 -0700 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | void RtpTransportControllerAdapter::Close_w() { |
| 662 | call_.reset(); |
sprang | db2a9fc | 2017-08-09 06:42:32 -0700 | [diff] [blame] | 663 | call_send_rtp_transport_controller_ = nullptr; |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | RTCError RtpTransportControllerAdapter::AttachAudioSender( |
| 667 | OrtcRtpSenderAdapter* sender, |
| 668 | RtpTransportInterface* inner_transport) { |
| 669 | if (have_audio_sender_) { |
| 670 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_OPERATION, |
| 671 | "Using two audio RtpSenders with the same " |
| 672 | "RtpTransportControllerAdapter is not currently " |
| 673 | "supported."); |
| 674 | } |
| 675 | if (inner_audio_transport_ && inner_audio_transport_ != inner_transport) { |
| 676 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_OPERATION, |
| 677 | "Using different transports for the audio " |
| 678 | "RtpSender and RtpReceiver is not currently " |
| 679 | "supported."); |
| 680 | } |
zhihuang | d3501ad | 2017-03-03 14:39:06 -0800 | [diff] [blame] | 681 | RTCError err = MaybeSetCryptos(inner_transport, &local_audio_description_, |
| 682 | &remote_audio_description_); |
| 683 | if (!err.ok()) { |
| 684 | return err; |
| 685 | } |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 686 | // If setting new transport, extract its RTCP parameters and create voice |
| 687 | // channel. |
| 688 | if (!inner_audio_transport_) { |
sprang | db2a9fc | 2017-08-09 06:42:32 -0700 | [diff] [blame] | 689 | CopyRtcpParametersToDescriptions(inner_transport->GetParameters().rtcp, |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 690 | &local_audio_description_, |
| 691 | &remote_audio_description_); |
| 692 | inner_audio_transport_ = inner_transport; |
| 693 | CreateVoiceChannel(); |
| 694 | } |
| 695 | have_audio_sender_ = true; |
| 696 | sender->SignalDestroyed.connect( |
| 697 | this, &RtpTransportControllerAdapter::OnAudioSenderDestroyed); |
| 698 | return RTCError::OK(); |
| 699 | } |
| 700 | |
| 701 | RTCError RtpTransportControllerAdapter::AttachVideoSender( |
| 702 | OrtcRtpSenderAdapter* sender, |
| 703 | RtpTransportInterface* inner_transport) { |
| 704 | if (have_video_sender_) { |
| 705 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_OPERATION, |
| 706 | "Using two video RtpSenders with the same " |
| 707 | "RtpTransportControllerAdapter is not currently " |
| 708 | "supported."); |
| 709 | } |
| 710 | if (inner_video_transport_ && inner_video_transport_ != inner_transport) { |
| 711 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_OPERATION, |
| 712 | "Using different transports for the video " |
| 713 | "RtpSender and RtpReceiver is not currently " |
| 714 | "supported."); |
| 715 | } |
zhihuang | d3501ad | 2017-03-03 14:39:06 -0800 | [diff] [blame] | 716 | RTCError err = MaybeSetCryptos(inner_transport, &local_video_description_, |
| 717 | &remote_video_description_); |
| 718 | if (!err.ok()) { |
| 719 | return err; |
| 720 | } |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 721 | // If setting new transport, extract its RTCP parameters and create video |
| 722 | // channel. |
| 723 | if (!inner_video_transport_) { |
sprang | db2a9fc | 2017-08-09 06:42:32 -0700 | [diff] [blame] | 724 | CopyRtcpParametersToDescriptions(inner_transport->GetParameters().rtcp, |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 725 | &local_video_description_, |
| 726 | &remote_video_description_); |
| 727 | inner_video_transport_ = inner_transport; |
| 728 | CreateVideoChannel(); |
| 729 | } |
| 730 | have_video_sender_ = true; |
| 731 | sender->SignalDestroyed.connect( |
| 732 | this, &RtpTransportControllerAdapter::OnVideoSenderDestroyed); |
| 733 | return RTCError::OK(); |
| 734 | } |
| 735 | |
| 736 | RTCError RtpTransportControllerAdapter::AttachAudioReceiver( |
| 737 | OrtcRtpReceiverAdapter* receiver, |
| 738 | RtpTransportInterface* inner_transport) { |
| 739 | if (have_audio_receiver_) { |
| 740 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_OPERATION, |
| 741 | "Using two audio RtpReceivers with the same " |
| 742 | "RtpTransportControllerAdapter is not currently " |
| 743 | "supported."); |
| 744 | } |
| 745 | if (inner_audio_transport_ && inner_audio_transport_ != inner_transport) { |
| 746 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_OPERATION, |
| 747 | "Using different transports for the audio " |
| 748 | "RtpReceiver and RtpReceiver is not currently " |
| 749 | "supported."); |
| 750 | } |
zhihuang | d3501ad | 2017-03-03 14:39:06 -0800 | [diff] [blame] | 751 | RTCError err = MaybeSetCryptos(inner_transport, &local_audio_description_, |
| 752 | &remote_audio_description_); |
| 753 | if (!err.ok()) { |
| 754 | return err; |
| 755 | } |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 756 | // If setting new transport, extract its RTCP parameters and create voice |
| 757 | // channel. |
| 758 | if (!inner_audio_transport_) { |
sprang | db2a9fc | 2017-08-09 06:42:32 -0700 | [diff] [blame] | 759 | CopyRtcpParametersToDescriptions(inner_transport->GetParameters().rtcp, |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 760 | &local_audio_description_, |
| 761 | &remote_audio_description_); |
| 762 | inner_audio_transport_ = inner_transport; |
| 763 | CreateVoiceChannel(); |
| 764 | } |
| 765 | have_audio_receiver_ = true; |
| 766 | receiver->SignalDestroyed.connect( |
| 767 | this, &RtpTransportControllerAdapter::OnAudioReceiverDestroyed); |
| 768 | return RTCError::OK(); |
| 769 | } |
| 770 | |
| 771 | RTCError RtpTransportControllerAdapter::AttachVideoReceiver( |
| 772 | OrtcRtpReceiverAdapter* receiver, |
| 773 | RtpTransportInterface* inner_transport) { |
| 774 | if (have_video_receiver_) { |
| 775 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_OPERATION, |
| 776 | "Using two video RtpReceivers with the same " |
| 777 | "RtpTransportControllerAdapter is not currently " |
| 778 | "supported."); |
| 779 | } |
| 780 | if (inner_video_transport_ && inner_video_transport_ != inner_transport) { |
| 781 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_OPERATION, |
| 782 | "Using different transports for the video " |
| 783 | "RtpReceiver and RtpReceiver is not currently " |
| 784 | "supported."); |
| 785 | } |
zhihuang | d3501ad | 2017-03-03 14:39:06 -0800 | [diff] [blame] | 786 | RTCError err = MaybeSetCryptos(inner_transport, &local_video_description_, |
| 787 | &remote_video_description_); |
| 788 | if (!err.ok()) { |
| 789 | return err; |
| 790 | } |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 791 | // If setting new transport, extract its RTCP parameters and create video |
| 792 | // channel. |
| 793 | if (!inner_video_transport_) { |
sprang | db2a9fc | 2017-08-09 06:42:32 -0700 | [diff] [blame] | 794 | CopyRtcpParametersToDescriptions(inner_transport->GetParameters().rtcp, |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 795 | &local_video_description_, |
| 796 | &remote_video_description_); |
| 797 | inner_video_transport_ = inner_transport; |
| 798 | CreateVideoChannel(); |
| 799 | } |
| 800 | have_video_receiver_ = true; |
| 801 | receiver->SignalDestroyed.connect( |
| 802 | this, &RtpTransportControllerAdapter::OnVideoReceiverDestroyed); |
| 803 | return RTCError::OK(); |
| 804 | } |
| 805 | |
| 806 | void RtpTransportControllerAdapter::OnRtpTransportDestroyed( |
| 807 | RtpTransportAdapter* transport) { |
| 808 | RTC_DCHECK_RUN_ON(signaling_thread_); |
| 809 | auto it = std::find_if(transport_proxies_.begin(), transport_proxies_.end(), |
| 810 | [transport](RtpTransportInterface* proxy) { |
| 811 | return proxy->GetInternal() == transport; |
| 812 | }); |
| 813 | if (it == transport_proxies_.end()) { |
| 814 | RTC_NOTREACHED(); |
| 815 | return; |
| 816 | } |
| 817 | transport_proxies_.erase(it); |
| 818 | } |
| 819 | |
| 820 | void RtpTransportControllerAdapter::OnAudioSenderDestroyed() { |
| 821 | if (!have_audio_sender_) { |
| 822 | RTC_NOTREACHED(); |
| 823 | return; |
| 824 | } |
| 825 | // Empty parameters should result in sending being stopped. |
| 826 | RTCError err = |
| 827 | ValidateAndApplyAudioSenderParameters(RtpParameters(), nullptr); |
| 828 | RTC_DCHECK(err.ok()); |
| 829 | have_audio_sender_ = false; |
| 830 | if (!have_audio_receiver_) { |
| 831 | DestroyVoiceChannel(); |
| 832 | } |
| 833 | } |
| 834 | |
| 835 | void RtpTransportControllerAdapter::OnVideoSenderDestroyed() { |
| 836 | if (!have_video_sender_) { |
| 837 | RTC_NOTREACHED(); |
| 838 | return; |
| 839 | } |
| 840 | // Empty parameters should result in sending being stopped. |
| 841 | RTCError err = |
| 842 | ValidateAndApplyVideoSenderParameters(RtpParameters(), nullptr); |
| 843 | RTC_DCHECK(err.ok()); |
| 844 | have_video_sender_ = false; |
| 845 | if (!have_video_receiver_) { |
| 846 | DestroyVideoChannel(); |
| 847 | } |
| 848 | } |
| 849 | |
| 850 | void RtpTransportControllerAdapter::OnAudioReceiverDestroyed() { |
| 851 | if (!have_audio_receiver_) { |
| 852 | RTC_NOTREACHED(); |
| 853 | return; |
| 854 | } |
| 855 | // Empty parameters should result in receiving being stopped. |
| 856 | RTCError err = ValidateAndApplyAudioReceiverParameters(RtpParameters()); |
| 857 | RTC_DCHECK(err.ok()); |
| 858 | have_audio_receiver_ = false; |
| 859 | if (!have_audio_sender_) { |
| 860 | DestroyVoiceChannel(); |
| 861 | } |
| 862 | } |
| 863 | |
| 864 | void RtpTransportControllerAdapter::OnVideoReceiverDestroyed() { |
| 865 | if (!have_video_receiver_) { |
| 866 | RTC_NOTREACHED(); |
| 867 | return; |
| 868 | } |
| 869 | // Empty parameters should result in receiving being stopped. |
| 870 | RTCError err = ValidateAndApplyVideoReceiverParameters(RtpParameters()); |
| 871 | RTC_DCHECK(err.ok()); |
| 872 | have_video_receiver_ = false; |
| 873 | if (!have_video_sender_) { |
| 874 | DestroyVideoChannel(); |
| 875 | } |
| 876 | } |
| 877 | |
| 878 | void RtpTransportControllerAdapter::CreateVoiceChannel() { |
nisse | eaabdf6 | 2017-05-05 02:23:02 -0700 | [diff] [blame] | 879 | voice_channel_ = channel_manager_->CreateVoiceChannel( |
| 880 | call_.get(), media_config_, |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 881 | inner_audio_transport_->GetRtpPacketTransport()->GetInternal(), |
| 882 | inner_audio_transport_->GetRtcpPacketTransport() |
| 883 | ? inner_audio_transport_->GetRtcpPacketTransport()->GetInternal() |
| 884 | : nullptr, |
| 885 | signaling_thread_, "audio", false, cricket::AudioOptions()); |
| 886 | RTC_DCHECK(voice_channel_); |
| 887 | voice_channel_->Enable(true); |
| 888 | } |
| 889 | |
| 890 | void RtpTransportControllerAdapter::CreateVideoChannel() { |
nisse | eaabdf6 | 2017-05-05 02:23:02 -0700 | [diff] [blame] | 891 | video_channel_ = channel_manager_->CreateVideoChannel( |
| 892 | call_.get(), media_config_, |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 893 | inner_video_transport_->GetRtpPacketTransport()->GetInternal(), |
| 894 | inner_video_transport_->GetRtcpPacketTransport() |
| 895 | ? inner_video_transport_->GetRtcpPacketTransport()->GetInternal() |
| 896 | : nullptr, |
| 897 | signaling_thread_, "video", false, cricket::VideoOptions()); |
| 898 | RTC_DCHECK(video_channel_); |
| 899 | video_channel_->Enable(true); |
| 900 | } |
| 901 | |
| 902 | void RtpTransportControllerAdapter::DestroyVoiceChannel() { |
| 903 | RTC_DCHECK(voice_channel_); |
nisse | eaabdf6 | 2017-05-05 02:23:02 -0700 | [diff] [blame] | 904 | channel_manager_->DestroyVoiceChannel(voice_channel_); |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 905 | voice_channel_ = nullptr; |
| 906 | inner_audio_transport_ = nullptr; |
| 907 | } |
| 908 | |
| 909 | void RtpTransportControllerAdapter::DestroyVideoChannel() { |
| 910 | RTC_DCHECK(video_channel_); |
nisse | eaabdf6 | 2017-05-05 02:23:02 -0700 | [diff] [blame] | 911 | channel_manager_->DestroyVideoChannel(video_channel_); |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 912 | video_channel_ = nullptr; |
| 913 | inner_video_transport_ = nullptr; |
| 914 | } |
| 915 | |
| 916 | void RtpTransportControllerAdapter::CopyRtcpParametersToDescriptions( |
| 917 | const RtcpParameters& params, |
| 918 | cricket::MediaContentDescription* local, |
| 919 | cricket::MediaContentDescription* remote) { |
| 920 | local->set_rtcp_mux(params.mux); |
| 921 | remote->set_rtcp_mux(params.mux); |
| 922 | local->set_rtcp_reduced_size(params.reduced_size); |
| 923 | remote->set_rtcp_reduced_size(params.reduced_size); |
| 924 | for (cricket::StreamParams& stream_params : local->mutable_streams()) { |
| 925 | stream_params.cname = params.cname; |
| 926 | } |
| 927 | } |
| 928 | |
| 929 | uint32_t RtpTransportControllerAdapter::GenerateUnusedSsrc( |
| 930 | std::set<uint32_t>* new_ssrcs) const { |
| 931 | uint32_t ssrc; |
| 932 | do { |
| 933 | ssrc = rtc::CreateRandomNonZeroId(); |
| 934 | } while ( |
| 935 | cricket::GetStreamBySsrc(local_audio_description_.streams(), ssrc) || |
| 936 | cricket::GetStreamBySsrc(remote_audio_description_.streams(), ssrc) || |
| 937 | cricket::GetStreamBySsrc(local_video_description_.streams(), ssrc) || |
| 938 | cricket::GetStreamBySsrc(remote_video_description_.streams(), ssrc) || |
| 939 | !new_ssrcs->insert(ssrc).second); |
| 940 | return ssrc; |
| 941 | } |
| 942 | |
| 943 | RTCErrorOr<cricket::StreamParamsVec> |
| 944 | RtpTransportControllerAdapter::MakeSendStreamParamsVec( |
| 945 | std::vector<RtpEncodingParameters> encodings, |
| 946 | const std::string& cname, |
| 947 | const cricket::MediaContentDescription& description) const { |
| 948 | if (encodings.size() > 1u) { |
| 949 | LOG_AND_RETURN_ERROR(webrtc::RTCErrorType::UNSUPPORTED_PARAMETER, |
| 950 | "ORTC API implementation doesn't currently " |
| 951 | "support simulcast or layered encodings."); |
| 952 | } else if (encodings.empty()) { |
| 953 | return cricket::StreamParamsVec(); |
| 954 | } |
| 955 | RtpEncodingParameters& encoding = encodings[0]; |
| 956 | std::set<uint32_t> new_ssrcs; |
| 957 | if (encoding.ssrc) { |
| 958 | new_ssrcs.insert(*encoding.ssrc); |
| 959 | } |
| 960 | if (encoding.rtx && encoding.rtx->ssrc) { |
| 961 | new_ssrcs.insert(*encoding.rtx->ssrc); |
| 962 | } |
| 963 | // May need to fill missing SSRCs with generated ones. |
| 964 | if (!encoding.ssrc) { |
| 965 | if (!description.streams().empty()) { |
| 966 | encoding.ssrc.emplace(description.streams()[0].first_ssrc()); |
| 967 | } else { |
| 968 | encoding.ssrc.emplace(GenerateUnusedSsrc(&new_ssrcs)); |
| 969 | } |
| 970 | } |
| 971 | if (encoding.rtx && !encoding.rtx->ssrc) { |
| 972 | uint32_t existing_rtx_ssrc; |
| 973 | if (!description.streams().empty() && |
| 974 | description.streams()[0].GetFidSsrc( |
| 975 | description.streams()[0].first_ssrc(), &existing_rtx_ssrc)) { |
| 976 | encoding.rtx->ssrc.emplace(existing_rtx_ssrc); |
| 977 | } else { |
| 978 | encoding.rtx->ssrc.emplace(GenerateUnusedSsrc(&new_ssrcs)); |
| 979 | } |
| 980 | } |
| 981 | |
| 982 | auto result = ToCricketStreamParamsVec(encodings); |
| 983 | if (!result.ok()) { |
| 984 | return result.MoveError(); |
| 985 | } |
| 986 | // If conversion was successful, there should be one StreamParams. |
| 987 | RTC_DCHECK_EQ(1u, result.value().size()); |
| 988 | result.value()[0].cname = cname; |
| 989 | return result; |
| 990 | } |
| 991 | |
zhihuang | d3501ad | 2017-03-03 14:39:06 -0800 | [diff] [blame] | 992 | RTCError RtpTransportControllerAdapter::MaybeSetCryptos( |
| 993 | RtpTransportInterface* rtp_transport, |
| 994 | cricket::MediaContentDescription* local_description, |
| 995 | cricket::MediaContentDescription* remote_description) { |
| 996 | if (rtp_transport->GetInternal()->is_srtp_transport()) { |
| 997 | if (!rtp_transport->GetInternal()->send_key() || |
| 998 | !rtp_transport->GetInternal()->receive_key()) { |
| 999 | LOG_AND_RETURN_ERROR(webrtc::RTCErrorType::UNSUPPORTED_PARAMETER, |
| 1000 | "The SRTP send key or receive key is not set.") |
| 1001 | } |
| 1002 | std::vector<cricket::CryptoParams> cryptos; |
| 1003 | cryptos.push_back(*(rtp_transport->GetInternal()->receive_key())); |
| 1004 | local_description->set_cryptos(cryptos); |
| 1005 | |
| 1006 | cryptos.clear(); |
| 1007 | cryptos.push_back(*(rtp_transport->GetInternal()->send_key())); |
| 1008 | remote_description->set_cryptos(cryptos); |
| 1009 | } |
| 1010 | return RTCError::OK(); |
| 1011 | } |
| 1012 | |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 1013 | } // namespace webrtc |