1 /*
2  *  Copyright 2020 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include "pc/sdp_offer_answer.h"
12 
13 #include <algorithm>
14 #include <iterator>
15 #include <map>
16 #include <queue>
17 #include <type_traits>
18 #include <utility>
19 
20 #include "absl/algorithm/container.h"
21 #include "absl/memory/memory.h"
22 #include "absl/strings/string_view.h"
23 #include "api/array_view.h"
24 #include "api/crypto/crypto_options.h"
25 #include "api/data_channel_interface.h"
26 #include "api/dtls_transport_interface.h"
27 #include "api/media_stream_proxy.h"
28 #include "api/rtp_parameters.h"
29 #include "api/rtp_receiver_interface.h"
30 #include "api/rtp_sender_interface.h"
31 #include "api/uma_metrics.h"
32 #include "api/video/builtin_video_bitrate_allocator_factory.h"
33 #include "media/base/codec.h"
34 #include "media/base/media_engine.h"
35 #include "media/base/rid_description.h"
36 #include "p2p/base/p2p_constants.h"
37 #include "p2p/base/p2p_transport_channel.h"
38 #include "p2p/base/port.h"
39 #include "p2p/base/transport_description.h"
40 #include "p2p/base/transport_description_factory.h"
41 #include "p2p/base/transport_info.h"
42 #include "pc/connection_context.h"
43 #include "pc/data_channel_utils.h"
44 #include "pc/media_protocol_names.h"
45 #include "pc/media_stream.h"
46 #include "pc/peer_connection.h"
47 #include "pc/peer_connection_message_handler.h"
48 #include "pc/rtp_data_channel.h"
49 #include "pc/rtp_media_utils.h"
50 #include "pc/rtp_sender.h"
51 #include "pc/rtp_transport_internal.h"
52 #include "pc/sctp_transport.h"
53 #include "pc/simulcast_description.h"
54 #include "pc/stats_collector.h"
55 #include "pc/usage_pattern.h"
56 #include "pc/webrtc_session_description_factory.h"
57 #include "rtc_base/bind.h"
58 #include "rtc_base/helpers.h"
59 #include "rtc_base/location.h"
60 #include "rtc_base/logging.h"
61 #include "rtc_base/ref_counted_object.h"
62 #include "rtc_base/rtc_certificate.h"
63 #include "rtc_base/socket_address.h"
64 #include "rtc_base/ssl_stream_adapter.h"
65 #include "rtc_base/string_encode.h"
66 #include "rtc_base/strings/string_builder.h"
67 #include "rtc_base/third_party/sigslot/sigslot.h"
68 #include "rtc_base/trace_event.h"
69 #include "system_wrappers/include/metrics.h"
70 
71 using cricket::ContentInfo;
72 using cricket::ContentInfos;
73 using cricket::MediaContentDescription;
74 using cricket::MediaProtocolType;
75 using cricket::RidDescription;
76 using cricket::RidDirection;
77 using cricket::SessionDescription;
78 using cricket::SimulcastDescription;
79 using cricket::SimulcastLayer;
80 using cricket::SimulcastLayerList;
81 using cricket::StreamParams;
82 using cricket::TransportInfo;
83 
84 using cricket::LOCAL_PORT_TYPE;
85 using cricket::PRFLX_PORT_TYPE;
86 using cricket::RELAY_PORT_TYPE;
87 using cricket::STUN_PORT_TYPE;
88 
89 namespace webrtc {
90 
91 namespace {
92 
93 typedef webrtc::PeerConnectionInterface::RTCOfferAnswerOptions
94     RTCOfferAnswerOptions;
95 
96 // Error messages
97 const char kInvalidSdp[] = "Invalid session description.";
98 const char kInvalidCandidates[] = "Description contains invalid candidates.";
99 const char kBundleWithoutRtcpMux[] =
100     "rtcp-mux must be enabled when BUNDLE "
101     "is enabled.";
102 const char kMlineMismatchInAnswer[] =
103     "The order of m-lines in answer doesn't match order in offer. Rejecting "
104     "answer.";
105 const char kMlineMismatchInSubsequentOffer[] =
106     "The order of m-lines in subsequent offer doesn't match order from "
107     "previous offer/answer.";
108 const char kSdpWithoutIceUfragPwd[] =
109     "Called with SDP without ice-ufrag and ice-pwd.";
110 const char kSdpWithoutDtlsFingerprint[] =
111     "Called with SDP without DTLS fingerprint.";
112 const char kSdpWithoutSdesCrypto[] = "Called with SDP without SDES crypto.";
113 
114 const char kSessionError[] = "Session error code: ";
115 const char kSessionErrorDesc[] = "Session error description: ";
116 
117 // UMA metric names.
118 const char kSimulcastVersionApplyLocalDescription[] =
119     "WebRTC.PeerConnection.Simulcast.ApplyLocalDescription";
120 const char kSimulcastVersionApplyRemoteDescription[] =
121     "WebRTC.PeerConnection.Simulcast.ApplyRemoteDescription";
122 const char kSimulcastDisabled[] = "WebRTC.PeerConnection.Simulcast.Disabled";
123 
124 // The length of RTCP CNAMEs.
125 static const int kRtcpCnameLength = 16;
126 
127 const char kDefaultStreamId[] = "default";
128 // NOTE: Duplicated in peer_connection.cc:
129 static const char kDefaultAudioSenderId[] = "defaulta0";
130 static const char kDefaultVideoSenderId[] = "defaultv0";
131 
NoteAddIceCandidateResult(int result)132 void NoteAddIceCandidateResult(int result) {
133   RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.AddIceCandidate", result,
134                             kAddIceCandidateMax);
135 }
136 
NoteKeyProtocolAndMedia(KeyExchangeProtocolType protocol_type,cricket::MediaType media_type)137 void NoteKeyProtocolAndMedia(KeyExchangeProtocolType protocol_type,
138                              cricket::MediaType media_type) {
139   // Array of structs needed to map {KeyExchangeProtocolType,
140   // cricket::MediaType} to KeyExchangeProtocolMedia without using std::map in
141   // order to avoid -Wglobal-constructors and -Wexit-time-destructors.
142   static constexpr struct {
143     KeyExchangeProtocolType protocol_type;
144     cricket::MediaType media_type;
145     KeyExchangeProtocolMedia protocol_media;
146   } kEnumCounterKeyProtocolMediaMap[] = {
147       {kEnumCounterKeyProtocolDtls, cricket::MEDIA_TYPE_AUDIO,
148        kEnumCounterKeyProtocolMediaTypeDtlsAudio},
149       {kEnumCounterKeyProtocolDtls, cricket::MEDIA_TYPE_VIDEO,
150        kEnumCounterKeyProtocolMediaTypeDtlsVideo},
151       {kEnumCounterKeyProtocolDtls, cricket::MEDIA_TYPE_DATA,
152        kEnumCounterKeyProtocolMediaTypeDtlsData},
153       {kEnumCounterKeyProtocolSdes, cricket::MEDIA_TYPE_AUDIO,
154        kEnumCounterKeyProtocolMediaTypeSdesAudio},
155       {kEnumCounterKeyProtocolSdes, cricket::MEDIA_TYPE_VIDEO,
156        kEnumCounterKeyProtocolMediaTypeSdesVideo},
157       {kEnumCounterKeyProtocolSdes, cricket::MEDIA_TYPE_DATA,
158        kEnumCounterKeyProtocolMediaTypeSdesData},
159   };
160 
161   RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.KeyProtocol", protocol_type,
162                             kEnumCounterKeyProtocolMax);
163 
164   for (const auto& i : kEnumCounterKeyProtocolMediaMap) {
165     if (i.protocol_type == protocol_type && i.media_type == media_type) {
166       RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.KeyProtocolByMedia",
167                                 i.protocol_media,
168                                 kEnumCounterKeyProtocolMediaTypeMax);
169     }
170   }
171 }
172 
173 // Returns true if |new_desc| requests an ICE restart (i.e., new ufrag/pwd).
CheckForRemoteIceRestart(const SessionDescriptionInterface * old_desc,const SessionDescriptionInterface * new_desc,const std::string & content_name)174 bool CheckForRemoteIceRestart(const SessionDescriptionInterface* old_desc,
175                               const SessionDescriptionInterface* new_desc,
176                               const std::string& content_name) {
177   if (!old_desc) {
178     return false;
179   }
180   const SessionDescription* new_sd = new_desc->description();
181   const SessionDescription* old_sd = old_desc->description();
182   const ContentInfo* cinfo = new_sd->GetContentByName(content_name);
183   if (!cinfo || cinfo->rejected) {
184     return false;
185   }
186   // If the content isn't rejected, check if ufrag and password has changed.
187   const cricket::TransportDescription* new_transport_desc =
188       new_sd->GetTransportDescriptionByName(content_name);
189   const cricket::TransportDescription* old_transport_desc =
190       old_sd->GetTransportDescriptionByName(content_name);
191   if (!new_transport_desc || !old_transport_desc) {
192     // No transport description exists. This is not an ICE restart.
193     return false;
194   }
195   if (cricket::IceCredentialsChanged(
196           old_transport_desc->ice_ufrag, old_transport_desc->ice_pwd,
197           new_transport_desc->ice_ufrag, new_transport_desc->ice_pwd)) {
198     RTC_LOG(LS_INFO) << "Remote peer requests ICE restart for " << content_name
199                      << ".";
200     return true;
201   }
202   return false;
203 }
204 
205 // Generates a string error message for SetLocalDescription/SetRemoteDescription
206 // from an RTCError.
GetSetDescriptionErrorMessage(cricket::ContentSource source,SdpType type,const RTCError & error)207 std::string GetSetDescriptionErrorMessage(cricket::ContentSource source,
208                                           SdpType type,
209                                           const RTCError& error) {
210   rtc::StringBuilder oss;
211   oss << "Failed to set " << (source == cricket::CS_LOCAL ? "local" : "remote")
212       << " " << SdpTypeToString(type) << " sdp: " << error.message();
213   return oss.Release();
214 }
215 
GetStreamIdsString(rtc::ArrayView<const std::string> stream_ids)216 std::string GetStreamIdsString(rtc::ArrayView<const std::string> stream_ids) {
217   std::string output = "streams=[";
218   const char* separator = "";
219   for (const auto& stream_id : stream_ids) {
220     output.append(separator).append(stream_id);
221     separator = ", ";
222   }
223   output.append("]");
224   return output;
225 }
226 
ReportSimulcastApiVersion(const char * name,const SessionDescription & session)227 void ReportSimulcastApiVersion(const char* name,
228                                const SessionDescription& session) {
229   bool has_legacy = false;
230   bool has_spec_compliant = false;
231   for (const ContentInfo& content : session.contents()) {
232     if (!content.media_description()) {
233       continue;
234     }
235     has_spec_compliant |= content.media_description()->HasSimulcast();
236     for (const StreamParams& sp : content.media_description()->streams()) {
237       has_legacy |= sp.has_ssrc_group(cricket::kSimSsrcGroupSemantics);
238     }
239   }
240 
241   if (has_legacy) {
242     RTC_HISTOGRAM_ENUMERATION(name, kSimulcastApiVersionLegacy,
243                               kSimulcastApiVersionMax);
244   }
245   if (has_spec_compliant) {
246     RTC_HISTOGRAM_ENUMERATION(name, kSimulcastApiVersionSpecCompliant,
247                               kSimulcastApiVersionMax);
248   }
249   if (!has_legacy && !has_spec_compliant) {
250     RTC_HISTOGRAM_ENUMERATION(name, kSimulcastApiVersionNone,
251                               kSimulcastApiVersionMax);
252   }
253 }
254 
FindTransceiverMSection(RtpTransceiverProxyWithInternal<RtpTransceiver> * transceiver,const SessionDescriptionInterface * session_description)255 const ContentInfo* FindTransceiverMSection(
256     RtpTransceiverProxyWithInternal<RtpTransceiver>* transceiver,
257     const SessionDescriptionInterface* session_description) {
258   return transceiver->mid()
259              ? session_description->description()->GetContentByName(
260                    *transceiver->mid())
261              : nullptr;
262 }
263 
264 // If the direction is "recvonly" or "inactive", treat the description
265 // as containing no streams.
266 // See: https://code.google.com/p/webrtc/issues/detail?id=5054
GetActiveStreams(const cricket::MediaContentDescription * desc)267 std::vector<cricket::StreamParams> GetActiveStreams(
268     const cricket::MediaContentDescription* desc) {
269   return RtpTransceiverDirectionHasSend(desc->direction())
270              ? desc->streams()
271              : std::vector<cricket::StreamParams>();
272 }
273 
274 // Logic to decide if an m= section can be recycled. This means that the new
275 // m= section is not rejected, but the old local or remote m= section is
276 // rejected. |old_content_one| and |old_content_two| refer to the m= section
277 // of the old remote and old local descriptions in no particular order.
278 // We need to check both the old local and remote because either
279 // could be the most current from the latest negotation.
IsMediaSectionBeingRecycled(SdpType type,const ContentInfo & content,const ContentInfo * old_content_one,const ContentInfo * old_content_two)280 bool IsMediaSectionBeingRecycled(SdpType type,
281                                  const ContentInfo& content,
282                                  const ContentInfo* old_content_one,
283                                  const ContentInfo* old_content_two) {
284   return type == SdpType::kOffer && !content.rejected &&
285          ((old_content_one && old_content_one->rejected) ||
286           (old_content_two && old_content_two->rejected));
287 }
288 
289 // Verify that the order of media sections in |new_desc| matches
290 // |current_desc|. The number of m= sections in |new_desc| should be no
291 // less than |current_desc|. In the case of checking an answer's
292 // |new_desc|, the |current_desc| is the last offer that was set as the
293 // local or remote. In the case of checking an offer's |new_desc| we
294 // check against the local and remote descriptions stored from the last
295 // negotiation, because either of these could be the most up to date for
296 // possible rejected m sections. These are the |current_desc| and
297 // |secondary_current_desc|.
MediaSectionsInSameOrder(const SessionDescription & current_desc,const SessionDescription * secondary_current_desc,const SessionDescription & new_desc,const SdpType type)298 bool MediaSectionsInSameOrder(const SessionDescription& current_desc,
299                               const SessionDescription* secondary_current_desc,
300                               const SessionDescription& new_desc,
301                               const SdpType type) {
302   if (current_desc.contents().size() > new_desc.contents().size()) {
303     return false;
304   }
305 
306   for (size_t i = 0; i < current_desc.contents().size(); ++i) {
307     const cricket::ContentInfo* secondary_content_info = nullptr;
308     if (secondary_current_desc &&
309         i < secondary_current_desc->contents().size()) {
310       secondary_content_info = &secondary_current_desc->contents()[i];
311     }
312     if (IsMediaSectionBeingRecycled(type, new_desc.contents()[i],
313                                     &current_desc.contents()[i],
314                                     secondary_content_info)) {
315       // For new offer descriptions, if the media section can be recycled, it's
316       // valid for the MID and media type to change.
317       continue;
318     }
319     if (new_desc.contents()[i].name != current_desc.contents()[i].name) {
320       return false;
321     }
322     const MediaContentDescription* new_desc_mdesc =
323         new_desc.contents()[i].media_description();
324     const MediaContentDescription* current_desc_mdesc =
325         current_desc.contents()[i].media_description();
326     if (new_desc_mdesc->type() != current_desc_mdesc->type()) {
327       return false;
328     }
329   }
330   return true;
331 }
332 
MediaSectionsHaveSameCount(const SessionDescription & desc1,const SessionDescription & desc2)333 bool MediaSectionsHaveSameCount(const SessionDescription& desc1,
334                                 const SessionDescription& desc2) {
335   return desc1.contents().size() == desc2.contents().size();
336 }
337 // Checks that each non-rejected content has SDES crypto keys or a DTLS
338 // fingerprint, unless it's in a BUNDLE group, in which case only the
339 // BUNDLE-tag section (first media section/description in the BUNDLE group)
340 // needs a ufrag and pwd. Mismatches, such as replying with a DTLS fingerprint
341 // to SDES keys, will be caught in JsepTransport negotiation, and backstopped
342 // by Channel's |srtp_required| check.
VerifyCrypto(const SessionDescription * desc,bool dtls_enabled)343 RTCError VerifyCrypto(const SessionDescription* desc, bool dtls_enabled) {
344   const cricket::ContentGroup* bundle =
345       desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
346   for (const cricket::ContentInfo& content_info : desc->contents()) {
347     if (content_info.rejected) {
348       continue;
349     }
350     // Note what media is used with each crypto protocol, for all sections.
351     NoteKeyProtocolAndMedia(dtls_enabled ? webrtc::kEnumCounterKeyProtocolDtls
352                                          : webrtc::kEnumCounterKeyProtocolSdes,
353                             content_info.media_description()->type());
354     const std::string& mid = content_info.name;
355     if (bundle && bundle->HasContentName(mid) &&
356         mid != *(bundle->FirstContentName())) {
357       // This isn't the first media section in the BUNDLE group, so it's not
358       // required to have crypto attributes, since only the crypto attributes
359       // from the first section actually get used.
360       continue;
361     }
362 
363     // If the content isn't rejected or bundled into another m= section, crypto
364     // must be present.
365     const MediaContentDescription* media = content_info.media_description();
366     const TransportInfo* tinfo = desc->GetTransportInfoByName(mid);
367     if (!media || !tinfo) {
368       // Something is not right.
369       LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, kInvalidSdp);
370     }
371     if (dtls_enabled) {
372       if (!tinfo->description.identity_fingerprint) {
373         RTC_LOG(LS_WARNING)
374             << "Session description must have DTLS fingerprint if "
375                "DTLS enabled.";
376         return RTCError(RTCErrorType::INVALID_PARAMETER,
377                         kSdpWithoutDtlsFingerprint);
378       }
379     } else {
380       if (media->cryptos().empty()) {
381         RTC_LOG(LS_WARNING)
382             << "Session description must have SDES when DTLS disabled.";
383         return RTCError(RTCErrorType::INVALID_PARAMETER, kSdpWithoutSdesCrypto);
384       }
385     }
386   }
387   return RTCError::OK();
388 }
389 
390 // Checks that each non-rejected content has ice-ufrag and ice-pwd set, unless
391 // it's in a BUNDLE group, in which case only the BUNDLE-tag section (first
392 // media section/description in the BUNDLE group) needs a ufrag and pwd.
VerifyIceUfragPwdPresent(const SessionDescription * desc)393 bool VerifyIceUfragPwdPresent(const SessionDescription* desc) {
394   const cricket::ContentGroup* bundle =
395       desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
396   for (const cricket::ContentInfo& content_info : desc->contents()) {
397     if (content_info.rejected) {
398       continue;
399     }
400     const std::string& mid = content_info.name;
401     if (bundle && bundle->HasContentName(mid) &&
402         mid != *(bundle->FirstContentName())) {
403       // This isn't the first media section in the BUNDLE group, so it's not
404       // required to have ufrag/password, since only the ufrag/password from
405       // the first section actually get used.
406       continue;
407     }
408 
409     // If the content isn't rejected or bundled into another m= section,
410     // ice-ufrag and ice-pwd must be present.
411     const TransportInfo* tinfo = desc->GetTransportInfoByName(mid);
412     if (!tinfo) {
413       // Something is not right.
414       RTC_LOG(LS_ERROR) << kInvalidSdp;
415       return false;
416     }
417     if (tinfo->description.ice_ufrag.empty() ||
418         tinfo->description.ice_pwd.empty()) {
419       RTC_LOG(LS_ERROR) << "Session description must have ice ufrag and pwd.";
420       return false;
421     }
422   }
423   return true;
424 }
425 
ValidateMids(const cricket::SessionDescription & description)426 static RTCError ValidateMids(const cricket::SessionDescription& description) {
427   std::set<std::string> mids;
428   for (const cricket::ContentInfo& content : description.contents()) {
429     if (content.name.empty()) {
430       LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
431                            "A media section is missing a MID attribute.");
432     }
433     if (!mids.insert(content.name).second) {
434       LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
435                            "Duplicate a=mid value '" + content.name + "'.");
436     }
437   }
438   return RTCError::OK();
439 }
440 
IsValidOfferToReceiveMedia(int value)441 bool IsValidOfferToReceiveMedia(int value) {
442   typedef PeerConnectionInterface::RTCOfferAnswerOptions Options;
443   return (value >= Options::kUndefined) &&
444          (value <= Options::kMaxOfferToReceiveMedia);
445 }
446 
ValidateOfferAnswerOptions(const PeerConnectionInterface::RTCOfferAnswerOptions & rtc_options)447 bool ValidateOfferAnswerOptions(
448     const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options) {
449   return IsValidOfferToReceiveMedia(rtc_options.offer_to_receive_audio) &&
450          IsValidOfferToReceiveMedia(rtc_options.offer_to_receive_video);
451 }
452 
453 // Map internal signaling state name to spec name:
454 //  https://w3c.github.io/webrtc-pc/#rtcsignalingstate-enum
GetSignalingStateString(PeerConnectionInterface::SignalingState state)455 std::string GetSignalingStateString(
456     PeerConnectionInterface::SignalingState state) {
457   switch (state) {
458     case PeerConnectionInterface::kStable:
459       return "stable";
460     case PeerConnectionInterface::kHaveLocalOffer:
461       return "have-local-offer";
462     case PeerConnectionInterface::kHaveLocalPrAnswer:
463       return "have-local-pranswer";
464     case PeerConnectionInterface::kHaveRemoteOffer:
465       return "have-remote-offer";
466     case PeerConnectionInterface::kHaveRemotePrAnswer:
467       return "have-remote-pranswer";
468     case PeerConnectionInterface::kClosed:
469       return "closed";
470   }
471   RTC_NOTREACHED();
472   return "";
473 }
474 
475 // This method will extract any send encodings that were sent by the remote
476 // connection. This is currently only relevant for Simulcast scenario (where
477 // the number of layers may be communicated by the server).
GetSendEncodingsFromRemoteDescription(const MediaContentDescription & desc)478 static std::vector<RtpEncodingParameters> GetSendEncodingsFromRemoteDescription(
479     const MediaContentDescription& desc) {
480   if (!desc.HasSimulcast()) {
481     return {};
482   }
483   std::vector<RtpEncodingParameters> result;
484   const SimulcastDescription& simulcast = desc.simulcast_description();
485 
486   // This is a remote description, the parameters we are after should appear
487   // as receive streams.
488   for (const auto& alternatives : simulcast.receive_layers()) {
489     RTC_DCHECK(!alternatives.empty());
490     // There is currently no way to specify or choose from alternatives.
491     // We will always use the first alternative, which is the most preferred.
492     const SimulcastLayer& layer = alternatives[0];
493     RtpEncodingParameters parameters;
494     parameters.rid = layer.rid;
495     parameters.active = !layer.is_paused;
496     result.push_back(parameters);
497   }
498 
499   return result;
500 }
501 
UpdateSimulcastLayerStatusInSender(const std::vector<SimulcastLayer> & layers,rtc::scoped_refptr<RtpSenderInternal> sender)502 static RTCError UpdateSimulcastLayerStatusInSender(
503     const std::vector<SimulcastLayer>& layers,
504     rtc::scoped_refptr<RtpSenderInternal> sender) {
505   RTC_DCHECK(sender);
506   RtpParameters parameters = sender->GetParametersInternal();
507   std::vector<std::string> disabled_layers;
508 
509   // The simulcast envelope cannot be changed, only the status of the streams.
510   // So we will iterate over the send encodings rather than the layers.
511   for (RtpEncodingParameters& encoding : parameters.encodings) {
512     auto iter = std::find_if(layers.begin(), layers.end(),
513                              [&encoding](const SimulcastLayer& layer) {
514                                return layer.rid == encoding.rid;
515                              });
516     // A layer that cannot be found may have been removed by the remote party.
517     if (iter == layers.end()) {
518       disabled_layers.push_back(encoding.rid);
519       continue;
520     }
521 
522     encoding.active = !iter->is_paused;
523   }
524 
525   RTCError result = sender->SetParametersInternal(parameters);
526   if (result.ok()) {
527     result = sender->DisableEncodingLayers(disabled_layers);
528   }
529 
530   return result;
531 }
532 
SimulcastIsRejected(const ContentInfo * local_content,const MediaContentDescription & answer_media_desc)533 static bool SimulcastIsRejected(
534     const ContentInfo* local_content,
535     const MediaContentDescription& answer_media_desc) {
536   bool simulcast_offered = local_content &&
537                            local_content->media_description() &&
538                            local_content->media_description()->HasSimulcast();
539   bool simulcast_answered = answer_media_desc.HasSimulcast();
540   bool rids_supported = RtpExtension::FindHeaderExtensionByUri(
541       answer_media_desc.rtp_header_extensions(), RtpExtension::kRidUri);
542   return simulcast_offered && (!simulcast_answered || !rids_supported);
543 }
544 
DisableSimulcastInSender(rtc::scoped_refptr<RtpSenderInternal> sender)545 static RTCError DisableSimulcastInSender(
546     rtc::scoped_refptr<RtpSenderInternal> sender) {
547   RTC_DCHECK(sender);
548   RtpParameters parameters = sender->GetParametersInternal();
549   if (parameters.encodings.size() <= 1) {
550     return RTCError::OK();
551   }
552 
553   std::vector<std::string> disabled_layers;
554   std::transform(
555       parameters.encodings.begin() + 1, parameters.encodings.end(),
556       std::back_inserter(disabled_layers),
557       [](const RtpEncodingParameters& encoding) { return encoding.rid; });
558   return sender->DisableEncodingLayers(disabled_layers);
559 }
560 
561 // The SDP parser used to populate these values by default for the 'content
562 // name' if an a=mid line was absent.
GetDefaultMidForPlanB(cricket::MediaType media_type)563 static absl::string_view GetDefaultMidForPlanB(cricket::MediaType media_type) {
564   switch (media_type) {
565     case cricket::MEDIA_TYPE_AUDIO:
566       return cricket::CN_AUDIO;
567     case cricket::MEDIA_TYPE_VIDEO:
568       return cricket::CN_VIDEO;
569     case cricket::MEDIA_TYPE_DATA:
570       return cricket::CN_DATA;
571     case cricket::MEDIA_TYPE_UNSUPPORTED:
572       return "not supported";
573   }
574   RTC_NOTREACHED();
575   return "";
576 }
577 
578 // Add options to |[audio/video]_media_description_options| from |senders|.
AddPlanBRtpSenderOptions(const std::vector<rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>> & senders,cricket::MediaDescriptionOptions * audio_media_description_options,cricket::MediaDescriptionOptions * video_media_description_options,int num_sim_layers)579 void AddPlanBRtpSenderOptions(
580     const std::vector<rtc::scoped_refptr<
581         RtpSenderProxyWithInternal<RtpSenderInternal>>>& senders,
582     cricket::MediaDescriptionOptions* audio_media_description_options,
583     cricket::MediaDescriptionOptions* video_media_description_options,
584     int num_sim_layers) {
585   for (const auto& sender : senders) {
586     if (sender->media_type() == cricket::MEDIA_TYPE_AUDIO) {
587       if (audio_media_description_options) {
588         audio_media_description_options->AddAudioSender(
589             sender->id(), sender->internal()->stream_ids());
590       }
591     } else {
592       RTC_DCHECK(sender->media_type() == cricket::MEDIA_TYPE_VIDEO);
593       if (video_media_description_options) {
594         video_media_description_options->AddVideoSender(
595             sender->id(), sender->internal()->stream_ids(), {},
596             SimulcastLayerList(), num_sim_layers);
597       }
598     }
599   }
600 }
601 
602 static cricket::MediaDescriptionOptions
GetMediaDescriptionOptionsForTransceiver(rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> transceiver,const std::string & mid,bool is_create_offer)603 GetMediaDescriptionOptionsForTransceiver(
604     rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
605         transceiver,
606     const std::string& mid,
607     bool is_create_offer) {
608   // NOTE: a stopping transceiver should be treated as a stopped one in
609   // createOffer as specified in
610   // https://w3c.github.io/webrtc-pc/#dom-rtcpeerconnection-createoffer.
611   bool stopped =
612       is_create_offer ? transceiver->stopping() : transceiver->stopped();
613   cricket::MediaDescriptionOptions media_description_options(
614       transceiver->media_type(), mid, transceiver->direction(), stopped);
615   media_description_options.codec_preferences =
616       transceiver->codec_preferences();
617   media_description_options.header_extensions =
618       transceiver->HeaderExtensionsToOffer();
619   // This behavior is specified in JSEP. The gist is that:
620   // 1. The MSID is included if the RtpTransceiver's direction is sendonly or
621   //    sendrecv.
622   // 2. If the MSID is included, then it must be included in any subsequent
623   //    offer/answer exactly the same until the RtpTransceiver is stopped.
624   if (stopped || (!RtpTransceiverDirectionHasSend(transceiver->direction()) &&
625                   !transceiver->internal()->has_ever_been_used_to_send())) {
626     return media_description_options;
627   }
628 
629   cricket::SenderOptions sender_options;
630   sender_options.track_id = transceiver->sender()->id();
631   sender_options.stream_ids = transceiver->sender()->stream_ids();
632 
633   // The following sets up RIDs and Simulcast.
634   // RIDs are included if Simulcast is requested or if any RID was specified.
635   RtpParameters send_parameters =
636       transceiver->internal()->sender_internal()->GetParametersInternal();
637   bool has_rids = std::any_of(send_parameters.encodings.begin(),
638                               send_parameters.encodings.end(),
639                               [](const RtpEncodingParameters& encoding) {
640                                 return !encoding.rid.empty();
641                               });
642 
643   std::vector<RidDescription> send_rids;
644   SimulcastLayerList send_layers;
645   for (const RtpEncodingParameters& encoding : send_parameters.encodings) {
646     if (encoding.rid.empty()) {
647       continue;
648     }
649     send_rids.push_back(RidDescription(encoding.rid, RidDirection::kSend));
650     send_layers.AddLayer(SimulcastLayer(encoding.rid, !encoding.active));
651   }
652 
653   if (has_rids) {
654     sender_options.rids = send_rids;
655   }
656 
657   sender_options.simulcast_layers = send_layers;
658   // When RIDs are configured, we must set num_sim_layers to 0 to.
659   // Otherwise, num_sim_layers must be 1 because either there is no
660   // simulcast, or simulcast is acheived by munging the SDP.
661   sender_options.num_sim_layers = has_rids ? 0 : 1;
662   media_description_options.sender_options.push_back(sender_options);
663 
664   return media_description_options;
665 }
666 
667 // Returns the ContentInfo at mline index |i|, or null if none exists.
GetContentByIndex(const SessionDescriptionInterface * sdesc,size_t i)668 static const ContentInfo* GetContentByIndex(
669     const SessionDescriptionInterface* sdesc,
670     size_t i) {
671   if (!sdesc) {
672     return nullptr;
673   }
674   const ContentInfos& contents = sdesc->description()->contents();
675   return (i < contents.size() ? &contents[i] : nullptr);
676 }
677 
678 // From |rtc_options|, fill parts of |session_options| shared by all generated
679 // m= sectionss (in other words, nothing that involves a map/array).
ExtractSharedMediaSessionOptions(const PeerConnectionInterface::RTCOfferAnswerOptions & rtc_options,cricket::MediaSessionOptions * session_options)680 void ExtractSharedMediaSessionOptions(
681     const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options,
682     cricket::MediaSessionOptions* session_options) {
683   session_options->vad_enabled = rtc_options.voice_activity_detection;
684   session_options->bundle_enabled = rtc_options.use_rtp_mux;
685   session_options->raw_packetization_for_video =
686       rtc_options.raw_packetization_for_video;
687 }
688 
689 // Generate a RTCP CNAME when a PeerConnection is created.
GenerateRtcpCname()690 std::string GenerateRtcpCname() {
691   std::string cname;
692   if (!rtc::CreateRandomString(kRtcpCnameLength, &cname)) {
693     RTC_LOG(LS_ERROR) << "Failed to generate CNAME.";
694     RTC_NOTREACHED();
695   }
696   return cname;
697 }
698 
699 // Add options to |session_options| from |rtp_data_channels|.
AddRtpDataChannelOptions(const std::map<std::string,rtc::scoped_refptr<RtpDataChannel>> & rtp_data_channels,cricket::MediaDescriptionOptions * data_media_description_options)700 void AddRtpDataChannelOptions(
701     const std::map<std::string, rtc::scoped_refptr<RtpDataChannel>>&
702         rtp_data_channels,
703     cricket::MediaDescriptionOptions* data_media_description_options) {
704   if (!data_media_description_options) {
705     return;
706   }
707   // Check for data channels.
708   for (const auto& kv : rtp_data_channels) {
709     const RtpDataChannel* channel = kv.second;
710     if (channel->state() == RtpDataChannel::kConnecting ||
711         channel->state() == RtpDataChannel::kOpen) {
712       // Legacy RTP data channels are signaled with the track/stream ID set to
713       // the data channel's label.
714       data_media_description_options->AddRtpDataChannel(channel->label(),
715                                                         channel->label());
716     }
717   }
718 }
719 
720 // Check if we can send |new_stream| on a PeerConnection.
CanAddLocalMediaStream(webrtc::StreamCollectionInterface * current_streams,webrtc::MediaStreamInterface * new_stream)721 bool CanAddLocalMediaStream(webrtc::StreamCollectionInterface* current_streams,
722                             webrtc::MediaStreamInterface* new_stream) {
723   if (!new_stream || !current_streams) {
724     return false;
725   }
726   if (current_streams->find(new_stream->id()) != nullptr) {
727     RTC_LOG(LS_ERROR) << "MediaStream with ID " << new_stream->id()
728                       << " is already added.";
729     return false;
730   }
731   return true;
732 }
733 
734 }  // namespace
735 
736 // Used by parameterless SetLocalDescription() to create an offer or answer.
737 // Upon completion of creating the session description, SetLocalDescription() is
738 // invoked with the result.
739 class SdpOfferAnswerHandler::ImplicitCreateSessionDescriptionObserver
740     : public CreateSessionDescriptionObserver {
741  public:
ImplicitCreateSessionDescriptionObserver(rtc::WeakPtr<SdpOfferAnswerHandler> sdp_handler,rtc::scoped_refptr<SetLocalDescriptionObserverInterface> set_local_description_observer)742   ImplicitCreateSessionDescriptionObserver(
743       rtc::WeakPtr<SdpOfferAnswerHandler> sdp_handler,
744       rtc::scoped_refptr<SetLocalDescriptionObserverInterface>
745           set_local_description_observer)
746       : sdp_handler_(std::move(sdp_handler)),
747         set_local_description_observer_(
748             std::move(set_local_description_observer)) {}
~ImplicitCreateSessionDescriptionObserver()749   ~ImplicitCreateSessionDescriptionObserver() override {
750     RTC_DCHECK(was_called_);
751   }
752 
SetOperationCompleteCallback(std::function<void ()> operation_complete_callback)753   void SetOperationCompleteCallback(
754       std::function<void()> operation_complete_callback) {
755     operation_complete_callback_ = std::move(operation_complete_callback);
756   }
757 
was_called() const758   bool was_called() const { return was_called_; }
759 
OnSuccess(SessionDescriptionInterface * desc_ptr)760   void OnSuccess(SessionDescriptionInterface* desc_ptr) override {
761     RTC_DCHECK(!was_called_);
762     std::unique_ptr<SessionDescriptionInterface> desc(desc_ptr);
763     was_called_ = true;
764 
765     // Abort early if |pc_| is no longer valid.
766     if (!sdp_handler_) {
767       operation_complete_callback_();
768       return;
769     }
770     // DoSetLocalDescription() is a synchronous operation that invokes
771     // |set_local_description_observer_| with the result.
772     sdp_handler_->DoSetLocalDescription(
773         std::move(desc), std::move(set_local_description_observer_));
774     operation_complete_callback_();
775   }
776 
OnFailure(RTCError error)777   void OnFailure(RTCError error) override {
778     RTC_DCHECK(!was_called_);
779     was_called_ = true;
780     set_local_description_observer_->OnSetLocalDescriptionComplete(RTCError(
781         error.type(), std::string("SetLocalDescription failed to create "
782                                   "session description - ") +
783                           error.message()));
784     operation_complete_callback_();
785   }
786 
787  private:
788   bool was_called_ = false;
789   rtc::WeakPtr<SdpOfferAnswerHandler> sdp_handler_;
790   rtc::scoped_refptr<SetLocalDescriptionObserverInterface>
791       set_local_description_observer_;
792   std::function<void()> operation_complete_callback_;
793 };
794 
795 // Wraps a CreateSessionDescriptionObserver and an OperationsChain operation
796 // complete callback. When the observer is invoked, the wrapped observer is
797 // invoked followed by invoking the completion callback.
798 class CreateSessionDescriptionObserverOperationWrapper
799     : public CreateSessionDescriptionObserver {
800  public:
CreateSessionDescriptionObserverOperationWrapper(rtc::scoped_refptr<CreateSessionDescriptionObserver> observer,std::function<void ()> operation_complete_callback)801   CreateSessionDescriptionObserverOperationWrapper(
802       rtc::scoped_refptr<CreateSessionDescriptionObserver> observer,
803       std::function<void()> operation_complete_callback)
804       : observer_(std::move(observer)),
805         operation_complete_callback_(std::move(operation_complete_callback)) {
806     RTC_DCHECK(observer_);
807   }
~CreateSessionDescriptionObserverOperationWrapper()808   ~CreateSessionDescriptionObserverOperationWrapper() override {
809 #if RTC_DCHECK_IS_ON
810     RTC_DCHECK(was_called_);
811 #endif
812   }
813 
OnSuccess(SessionDescriptionInterface * desc)814   void OnSuccess(SessionDescriptionInterface* desc) override {
815 #if RTC_DCHECK_IS_ON
816     RTC_DCHECK(!was_called_);
817     was_called_ = true;
818 #endif  // RTC_DCHECK_IS_ON
819     // Completing the operation before invoking the observer allows the observer
820     // to execute SetLocalDescription() without delay.
821     operation_complete_callback_();
822     observer_->OnSuccess(desc);
823   }
824 
OnFailure(RTCError error)825   void OnFailure(RTCError error) override {
826 #if RTC_DCHECK_IS_ON
827     RTC_DCHECK(!was_called_);
828     was_called_ = true;
829 #endif  // RTC_DCHECK_IS_ON
830     operation_complete_callback_();
831     observer_->OnFailure(std::move(error));
832   }
833 
834  private:
835 #if RTC_DCHECK_IS_ON
836   bool was_called_ = false;
837 #endif  // RTC_DCHECK_IS_ON
838   rtc::scoped_refptr<CreateSessionDescriptionObserver> observer_;
839   std::function<void()> operation_complete_callback_;
840 };
841 
842 // Wrapper for SetSessionDescriptionObserver that invokes the success or failure
843 // callback in a posted message handled by the peer connection. This introduces
844 // a delay that prevents recursive API calls by the observer, but this also
845 // means that the PeerConnection can be modified before the observer sees the
846 // result of the operation. This is ill-advised for synchronizing states.
847 //
848 // Implements both the SetLocalDescriptionObserverInterface and the
849 // SetRemoteDescriptionObserverInterface.
850 class SdpOfferAnswerHandler::SetSessionDescriptionObserverAdapter
851     : public SetLocalDescriptionObserverInterface,
852       public SetRemoteDescriptionObserverInterface {
853  public:
SetSessionDescriptionObserverAdapter(rtc::WeakPtr<SdpOfferAnswerHandler> handler,rtc::scoped_refptr<SetSessionDescriptionObserver> inner_observer)854   SetSessionDescriptionObserverAdapter(
855       rtc::WeakPtr<SdpOfferAnswerHandler> handler,
856       rtc::scoped_refptr<SetSessionDescriptionObserver> inner_observer)
857       : handler_(std::move(handler)),
858         inner_observer_(std::move(inner_observer)) {}
859 
860   // SetLocalDescriptionObserverInterface implementation.
OnSetLocalDescriptionComplete(RTCError error)861   void OnSetLocalDescriptionComplete(RTCError error) override {
862     OnSetDescriptionComplete(std::move(error));
863   }
864   // SetRemoteDescriptionObserverInterface implementation.
OnSetRemoteDescriptionComplete(RTCError error)865   void OnSetRemoteDescriptionComplete(RTCError error) override {
866     OnSetDescriptionComplete(std::move(error));
867   }
868 
869  private:
OnSetDescriptionComplete(RTCError error)870   void OnSetDescriptionComplete(RTCError error) {
871     if (!handler_)
872       return;
873     if (error.ok()) {
874       handler_->pc_->message_handler()->PostSetSessionDescriptionSuccess(
875           inner_observer_);
876     } else {
877       handler_->pc_->message_handler()->PostSetSessionDescriptionFailure(
878           inner_observer_, std::move(error));
879     }
880   }
881 
882   rtc::WeakPtr<SdpOfferAnswerHandler> handler_;
883   rtc::scoped_refptr<SetSessionDescriptionObserver> inner_observer_;
884 };
885 
886 class SdpOfferAnswerHandler::LocalIceCredentialsToReplace {
887  public:
888   // Sets the ICE credentials that need restarting to the ICE credentials of
889   // the current and pending descriptions.
SetIceCredentialsFromLocalDescriptions(const SessionDescriptionInterface * current_local_description,const SessionDescriptionInterface * pending_local_description)890   void SetIceCredentialsFromLocalDescriptions(
891       const SessionDescriptionInterface* current_local_description,
892       const SessionDescriptionInterface* pending_local_description) {
893     ice_credentials_.clear();
894     if (current_local_description) {
895       AppendIceCredentialsFromSessionDescription(*current_local_description);
896     }
897     if (pending_local_description) {
898       AppendIceCredentialsFromSessionDescription(*pending_local_description);
899     }
900   }
901 
ClearIceCredentials()902   void ClearIceCredentials() { ice_credentials_.clear(); }
903 
904   // Returns true if we have ICE credentials that need restarting.
HasIceCredentials() const905   bool HasIceCredentials() const { return !ice_credentials_.empty(); }
906 
907   // Returns true if |local_description| shares no ICE credentials with the
908   // ICE credentials that need restarting.
SatisfiesIceRestart(const SessionDescriptionInterface & local_description) const909   bool SatisfiesIceRestart(
910       const SessionDescriptionInterface& local_description) const {
911     for (const auto& transport_info :
912          local_description.description()->transport_infos()) {
913       if (ice_credentials_.find(std::make_pair(
914               transport_info.description.ice_ufrag,
915               transport_info.description.ice_pwd)) != ice_credentials_.end()) {
916         return false;
917       }
918     }
919     return true;
920   }
921 
922  private:
AppendIceCredentialsFromSessionDescription(const SessionDescriptionInterface & desc)923   void AppendIceCredentialsFromSessionDescription(
924       const SessionDescriptionInterface& desc) {
925     for (const auto& transport_info : desc.description()->transport_infos()) {
926       ice_credentials_.insert(
927           std::make_pair(transport_info.description.ice_ufrag,
928                          transport_info.description.ice_pwd));
929     }
930   }
931 
932   std::set<std::pair<std::string, std::string>> ice_credentials_;
933 };
934 
SdpOfferAnswerHandler(PeerConnection * pc)935 SdpOfferAnswerHandler::SdpOfferAnswerHandler(PeerConnection* pc)
936     : pc_(pc),
937       local_streams_(StreamCollection::Create()),
938       remote_streams_(StreamCollection::Create()),
939       operations_chain_(rtc::OperationsChain::Create()),
940       rtcp_cname_(GenerateRtcpCname()),
941       local_ice_credentials_to_replace_(new LocalIceCredentialsToReplace()),
942       weak_ptr_factory_(this) {
943   operations_chain_->SetOnChainEmptyCallback(
944       [this_weak_ptr = weak_ptr_factory_.GetWeakPtr()]() {
945         if (!this_weak_ptr)
946           return;
947         this_weak_ptr->OnOperationsChainEmpty();
948       });
949 }
950 
~SdpOfferAnswerHandler()951 SdpOfferAnswerHandler::~SdpOfferAnswerHandler() {}
952 
953 // Static
Create(PeerConnection * pc,const PeerConnectionInterface::RTCConfiguration & configuration,PeerConnectionDependencies & dependencies)954 std::unique_ptr<SdpOfferAnswerHandler> SdpOfferAnswerHandler::Create(
955     PeerConnection* pc,
956     const PeerConnectionInterface::RTCConfiguration& configuration,
957     PeerConnectionDependencies& dependencies) {
958   auto handler = absl::WrapUnique(new SdpOfferAnswerHandler(pc));
959   handler->Initialize(configuration, dependencies);
960   return handler;
961 }
962 
Initialize(const PeerConnectionInterface::RTCConfiguration & configuration,PeerConnectionDependencies & dependencies)963 void SdpOfferAnswerHandler::Initialize(
964     const PeerConnectionInterface::RTCConfiguration& configuration,
965     PeerConnectionDependencies& dependencies) {
966   RTC_DCHECK_RUN_ON(signaling_thread());
967   video_options_.screencast_min_bitrate_kbps =
968       configuration.screencast_min_bitrate;
969   audio_options_.combined_audio_video_bwe =
970       configuration.combined_audio_video_bwe;
971 
972   audio_options_.audio_jitter_buffer_max_packets =
973       configuration.audio_jitter_buffer_max_packets;
974 
975   audio_options_.audio_jitter_buffer_fast_accelerate =
976       configuration.audio_jitter_buffer_fast_accelerate;
977 
978   audio_options_.audio_jitter_buffer_min_delay_ms =
979       configuration.audio_jitter_buffer_min_delay_ms;
980 
981   audio_options_.audio_jitter_buffer_enable_rtx_handling =
982       configuration.audio_jitter_buffer_enable_rtx_handling;
983 
984   // Obtain a certificate from RTCConfiguration if any were provided (optional).
985   rtc::scoped_refptr<rtc::RTCCertificate> certificate;
986   if (!configuration.certificates.empty()) {
987     // TODO(hbos,torbjorng): Decide on certificate-selection strategy instead of
988     // just picking the first one. The decision should be made based on the DTLS
989     // handshake. The DTLS negotiations need to know about all certificates.
990     certificate = configuration.certificates[0];
991   }
992 
993   webrtc_session_desc_factory_ =
994       std::make_unique<WebRtcSessionDescriptionFactory>(
995           signaling_thread(), channel_manager(), this, pc_->session_id(),
996           pc_->dtls_enabled(), std::move(dependencies.cert_generator),
997           certificate, &ssrc_generator_,
998           [this](const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) {
999             transport_controller()->SetLocalCertificate(certificate);
1000           });
1001 
1002   if (pc_->options()->disable_encryption) {
1003     webrtc_session_desc_factory_->SetSdesPolicy(cricket::SEC_DISABLED);
1004   }
1005 
1006   webrtc_session_desc_factory_->set_enable_encrypted_rtp_header_extensions(
1007       pc_->GetCryptoOptions().srtp.enable_encrypted_rtp_header_extensions);
1008   webrtc_session_desc_factory_->set_is_unified_plan(IsUnifiedPlan());
1009 
1010   if (dependencies.video_bitrate_allocator_factory) {
1011     video_bitrate_allocator_factory_ =
1012         std::move(dependencies.video_bitrate_allocator_factory);
1013   } else {
1014     video_bitrate_allocator_factory_ =
1015         CreateBuiltinVideoBitrateAllocatorFactory();
1016   }
1017 }
1018 
1019 // ==================================================================
1020 // Access to pc_ variables
channel_manager() const1021 cricket::ChannelManager* SdpOfferAnswerHandler::channel_manager() const {
1022   return pc_->channel_manager();
1023 }
transceivers()1024 TransceiverList* SdpOfferAnswerHandler::transceivers() {
1025   if (!pc_->rtp_manager()) {
1026     return nullptr;
1027   }
1028   return pc_->rtp_manager()->transceivers();
1029 }
transceivers() const1030 const TransceiverList* SdpOfferAnswerHandler::transceivers() const {
1031   if (!pc_->rtp_manager()) {
1032     return nullptr;
1033   }
1034   return pc_->rtp_manager()->transceivers();
1035 }
transport_controller()1036 JsepTransportController* SdpOfferAnswerHandler::transport_controller() {
1037   return pc_->transport_controller();
1038 }
transport_controller() const1039 const JsepTransportController* SdpOfferAnswerHandler::transport_controller()
1040     const {
1041   return pc_->transport_controller();
1042 }
data_channel_controller()1043 DataChannelController* SdpOfferAnswerHandler::data_channel_controller() {
1044   return pc_->data_channel_controller();
1045 }
data_channel_controller() const1046 const DataChannelController* SdpOfferAnswerHandler::data_channel_controller()
1047     const {
1048   return pc_->data_channel_controller();
1049 }
port_allocator()1050 cricket::PortAllocator* SdpOfferAnswerHandler::port_allocator() {
1051   return pc_->port_allocator();
1052 }
port_allocator() const1053 const cricket::PortAllocator* SdpOfferAnswerHandler::port_allocator() const {
1054   return pc_->port_allocator();
1055 }
rtp_manager()1056 RtpTransmissionManager* SdpOfferAnswerHandler::rtp_manager() {
1057   return pc_->rtp_manager();
1058 }
rtp_manager() const1059 const RtpTransmissionManager* SdpOfferAnswerHandler::rtp_manager() const {
1060   return pc_->rtp_manager();
1061 }
1062 
1063 // ===================================================================
1064 
PrepareForShutdown()1065 void SdpOfferAnswerHandler::PrepareForShutdown() {
1066   RTC_DCHECK_RUN_ON(signaling_thread());
1067   weak_ptr_factory_.InvalidateWeakPtrs();
1068 }
1069 
Close()1070 void SdpOfferAnswerHandler::Close() {
1071   ChangeSignalingState(PeerConnectionInterface::kClosed);
1072 }
1073 
RestartIce()1074 void SdpOfferAnswerHandler::RestartIce() {
1075   RTC_DCHECK_RUN_ON(signaling_thread());
1076   local_ice_credentials_to_replace_->SetIceCredentialsFromLocalDescriptions(
1077       current_local_description(), pending_local_description());
1078   UpdateNegotiationNeeded();
1079 }
1080 
signaling_thread() const1081 rtc::Thread* SdpOfferAnswerHandler::signaling_thread() const {
1082   return pc_->signaling_thread();
1083 }
1084 
CreateOffer(CreateSessionDescriptionObserver * observer,const PeerConnectionInterface::RTCOfferAnswerOptions & options)1085 void SdpOfferAnswerHandler::CreateOffer(
1086     CreateSessionDescriptionObserver* observer,
1087     const PeerConnectionInterface::RTCOfferAnswerOptions& options) {
1088   RTC_DCHECK_RUN_ON(signaling_thread());
1089   // Chain this operation. If asynchronous operations are pending on the chain,
1090   // this operation will be queued to be invoked, otherwise the contents of the
1091   // lambda will execute immediately.
1092   operations_chain_->ChainOperation(
1093       [this_weak_ptr = weak_ptr_factory_.GetWeakPtr(),
1094        observer_refptr =
1095            rtc::scoped_refptr<CreateSessionDescriptionObserver>(observer),
1096        options](std::function<void()> operations_chain_callback) {
1097         // Abort early if |this_weak_ptr| is no longer valid.
1098         if (!this_weak_ptr) {
1099           observer_refptr->OnFailure(
1100               RTCError(RTCErrorType::INTERNAL_ERROR,
1101                        "CreateOffer failed because the session was shut down"));
1102           operations_chain_callback();
1103           return;
1104         }
1105         // The operation completes asynchronously when the wrapper is invoked.
1106         rtc::scoped_refptr<CreateSessionDescriptionObserverOperationWrapper>
1107             observer_wrapper(new rtc::RefCountedObject<
1108                              CreateSessionDescriptionObserverOperationWrapper>(
1109                 std::move(observer_refptr),
1110                 std::move(operations_chain_callback)));
1111         this_weak_ptr->DoCreateOffer(options, observer_wrapper);
1112       });
1113 }
1114 
SetLocalDescription(SetSessionDescriptionObserver * observer,SessionDescriptionInterface * desc_ptr)1115 void SdpOfferAnswerHandler::SetLocalDescription(
1116     SetSessionDescriptionObserver* observer,
1117     SessionDescriptionInterface* desc_ptr) {
1118   RTC_DCHECK_RUN_ON(signaling_thread());
1119   // Chain this operation. If asynchronous operations are pending on the chain,
1120   // this operation will be queued to be invoked, otherwise the contents of the
1121   // lambda will execute immediately.
1122   operations_chain_->ChainOperation(
1123       [this_weak_ptr = weak_ptr_factory_.GetWeakPtr(),
1124        observer_refptr =
1125            rtc::scoped_refptr<SetSessionDescriptionObserver>(observer),
1126        desc = std::unique_ptr<SessionDescriptionInterface>(desc_ptr)](
1127           std::function<void()> operations_chain_callback) mutable {
1128         // Abort early if |this_weak_ptr| is no longer valid.
1129         if (!this_weak_ptr) {
1130           // For consistency with SetSessionDescriptionObserverAdapter whose
1131           // posted messages doesn't get processed when the PC is destroyed, we
1132           // do not inform |observer_refptr| that the operation failed.
1133           operations_chain_callback();
1134           return;
1135         }
1136         // SetSessionDescriptionObserverAdapter takes care of making sure the
1137         // |observer_refptr| is invoked in a posted message.
1138         this_weak_ptr->DoSetLocalDescription(
1139             std::move(desc),
1140             rtc::scoped_refptr<SetLocalDescriptionObserverInterface>(
1141                 new rtc::RefCountedObject<SetSessionDescriptionObserverAdapter>(
1142                     this_weak_ptr, observer_refptr)));
1143         // For backwards-compatability reasons, we declare the operation as
1144         // completed here (rather than in a post), so that the operation chain
1145         // is not blocked by this operation when the observer is invoked. This
1146         // allows the observer to trigger subsequent offer/answer operations
1147         // synchronously if the operation chain is now empty.
1148         operations_chain_callback();
1149       });
1150 }
1151 
SetLocalDescription(std::unique_ptr<SessionDescriptionInterface> desc,rtc::scoped_refptr<SetLocalDescriptionObserverInterface> observer)1152 void SdpOfferAnswerHandler::SetLocalDescription(
1153     std::unique_ptr<SessionDescriptionInterface> desc,
1154     rtc::scoped_refptr<SetLocalDescriptionObserverInterface> observer) {
1155   RTC_DCHECK_RUN_ON(signaling_thread());
1156   // Chain this operation. If asynchronous operations are pending on the chain,
1157   // this operation will be queued to be invoked, otherwise the contents of the
1158   // lambda will execute immediately.
1159   operations_chain_->ChainOperation(
1160       [this_weak_ptr = weak_ptr_factory_.GetWeakPtr(), observer,
1161        desc = std::move(desc)](
1162           std::function<void()> operations_chain_callback) mutable {
1163         // Abort early if |this_weak_ptr| is no longer valid.
1164         if (!this_weak_ptr) {
1165           observer->OnSetLocalDescriptionComplete(RTCError(
1166               RTCErrorType::INTERNAL_ERROR,
1167               "SetLocalDescription failed because the session was shut down"));
1168           operations_chain_callback();
1169           return;
1170         }
1171         this_weak_ptr->DoSetLocalDescription(std::move(desc), observer);
1172         // DoSetLocalDescription() is implemented as a synchronous operation.
1173         // The |observer| will already have been informed that it completed, and
1174         // we can mark this operation as complete without any loose ends.
1175         operations_chain_callback();
1176       });
1177 }
1178 
SetLocalDescription(SetSessionDescriptionObserver * observer)1179 void SdpOfferAnswerHandler::SetLocalDescription(
1180     SetSessionDescriptionObserver* observer) {
1181   RTC_DCHECK_RUN_ON(signaling_thread());
1182   SetLocalDescription(
1183       new rtc::RefCountedObject<SetSessionDescriptionObserverAdapter>(
1184           weak_ptr_factory_.GetWeakPtr(), observer));
1185 }
1186 
SetLocalDescription(rtc::scoped_refptr<SetLocalDescriptionObserverInterface> observer)1187 void SdpOfferAnswerHandler::SetLocalDescription(
1188     rtc::scoped_refptr<SetLocalDescriptionObserverInterface> observer) {
1189   RTC_DCHECK_RUN_ON(signaling_thread());
1190   // The |create_sdp_observer| handles performing DoSetLocalDescription() with
1191   // the resulting description as well as completing the operation.
1192   rtc::scoped_refptr<ImplicitCreateSessionDescriptionObserver>
1193       create_sdp_observer(
1194           new rtc::RefCountedObject<ImplicitCreateSessionDescriptionObserver>(
1195               weak_ptr_factory_.GetWeakPtr(), observer));
1196   // Chain this operation. If asynchronous operations are pending on the chain,
1197   // this operation will be queued to be invoked, otherwise the contents of the
1198   // lambda will execute immediately.
1199   operations_chain_->ChainOperation(
1200       [this_weak_ptr = weak_ptr_factory_.GetWeakPtr(),
1201        create_sdp_observer](std::function<void()> operations_chain_callback) {
1202         // The |create_sdp_observer| is responsible for completing the
1203         // operation.
1204         create_sdp_observer->SetOperationCompleteCallback(
1205             std::move(operations_chain_callback));
1206         // Abort early if |this_weak_ptr| is no longer valid. This triggers the
1207         // same code path as if DoCreateOffer() or DoCreateAnswer() failed.
1208         if (!this_weak_ptr) {
1209           create_sdp_observer->OnFailure(RTCError(
1210               RTCErrorType::INTERNAL_ERROR,
1211               "SetLocalDescription failed because the session was shut down"));
1212           return;
1213         }
1214         switch (this_weak_ptr->signaling_state()) {
1215           case PeerConnectionInterface::kStable:
1216           case PeerConnectionInterface::kHaveLocalOffer:
1217           case PeerConnectionInterface::kHaveRemotePrAnswer:
1218             // TODO(hbos): If [LastCreatedOffer] exists and still represents the
1219             // current state of the system, use that instead of creating another
1220             // offer.
1221             this_weak_ptr->DoCreateOffer(
1222                 PeerConnectionInterface::RTCOfferAnswerOptions(),
1223                 create_sdp_observer);
1224             break;
1225           case PeerConnectionInterface::kHaveLocalPrAnswer:
1226           case PeerConnectionInterface::kHaveRemoteOffer:
1227             // TODO(hbos): If [LastCreatedAnswer] exists and still represents
1228             // the current state of the system, use that instead of creating
1229             // another answer.
1230             this_weak_ptr->DoCreateAnswer(
1231                 PeerConnectionInterface::RTCOfferAnswerOptions(),
1232                 create_sdp_observer);
1233             break;
1234           case PeerConnectionInterface::kClosed:
1235             create_sdp_observer->OnFailure(RTCError(
1236                 RTCErrorType::INVALID_STATE,
1237                 "SetLocalDescription called when PeerConnection is closed."));
1238             break;
1239         }
1240       });
1241 }
1242 
ApplyLocalDescription(std::unique_ptr<SessionDescriptionInterface> desc)1243 RTCError SdpOfferAnswerHandler::ApplyLocalDescription(
1244     std::unique_ptr<SessionDescriptionInterface> desc) {
1245   RTC_DCHECK_RUN_ON(signaling_thread());
1246   RTC_DCHECK(desc);
1247 
1248   // Update stats here so that we have the most recent stats for tracks and
1249   // streams that might be removed by updating the session description.
1250   pc_->stats()->UpdateStats(PeerConnectionInterface::kStatsOutputLevelStandard);
1251 
1252   // Take a reference to the old local description since it's used below to
1253   // compare against the new local description. When setting the new local
1254   // description, grab ownership of the replaced session description in case it
1255   // is the same as |old_local_description|, to keep it alive for the duration
1256   // of the method.
1257   const SessionDescriptionInterface* old_local_description =
1258       local_description();
1259   std::unique_ptr<SessionDescriptionInterface> replaced_local_description;
1260   SdpType type = desc->GetType();
1261   if (type == SdpType::kAnswer) {
1262     replaced_local_description = pending_local_description_
1263                                      ? std::move(pending_local_description_)
1264                                      : std::move(current_local_description_);
1265     current_local_description_ = std::move(desc);
1266     pending_local_description_ = nullptr;
1267     current_remote_description_ = std::move(pending_remote_description_);
1268   } else {
1269     replaced_local_description = std::move(pending_local_description_);
1270     pending_local_description_ = std::move(desc);
1271   }
1272   // The session description to apply now must be accessed by
1273   // |local_description()|.
1274   RTC_DCHECK(local_description());
1275 
1276   // Report statistics about any use of simulcast.
1277   ReportSimulcastApiVersion(kSimulcastVersionApplyLocalDescription,
1278                             *local_description()->description());
1279 
1280   if (!is_caller_) {
1281     if (remote_description()) {
1282       // Remote description was applied first, so this PC is the callee.
1283       is_caller_ = false;
1284     } else {
1285       // Local description is applied first, so this PC is the caller.
1286       is_caller_ = true;
1287     }
1288   }
1289 
1290   RTCError error = PushdownTransportDescription(cricket::CS_LOCAL, type);
1291   if (!error.ok()) {
1292     return error;
1293   }
1294 
1295   if (IsUnifiedPlan()) {
1296     RTCError error = UpdateTransceiversAndDataChannels(
1297         cricket::CS_LOCAL, *local_description(), old_local_description,
1298         remote_description());
1299     if (!error.ok()) {
1300       return error;
1301     }
1302     std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> remove_list;
1303     std::vector<rtc::scoped_refptr<MediaStreamInterface>> removed_streams;
1304     for (const auto& transceiver : transceivers()->List()) {
1305       if (transceiver->stopped()) {
1306         continue;
1307       }
1308 
1309       // 2.2.7.1.1.(6-9): Set sender and receiver's transport slots.
1310       // Note that code paths that don't set MID won't be able to use
1311       // information about DTLS transports.
1312       if (transceiver->mid()) {
1313         auto dtls_transport = transport_controller()->LookupDtlsTransportByMid(
1314             *transceiver->mid());
1315         transceiver->internal()->sender_internal()->set_transport(
1316             dtls_transport);
1317         transceiver->internal()->receiver_internal()->set_transport(
1318             dtls_transport);
1319       }
1320 
1321       const ContentInfo* content =
1322           FindMediaSectionForTransceiver(transceiver, local_description());
1323       if (!content) {
1324         continue;
1325       }
1326       const MediaContentDescription* media_desc = content->media_description();
1327       // 2.2.7.1.6: If description is of type "answer" or "pranswer", then run
1328       // the following steps:
1329       if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) {
1330         // 2.2.7.1.6.1: If direction is "sendonly" or "inactive", and
1331         // transceiver's [[FiredDirection]] slot is either "sendrecv" or
1332         // "recvonly", process the removal of a remote track for the media
1333         // description, given transceiver, removeList, and muteTracks.
1334         if (!RtpTransceiverDirectionHasRecv(media_desc->direction()) &&
1335             (transceiver->internal()->fired_direction() &&
1336              RtpTransceiverDirectionHasRecv(
1337                  *transceiver->internal()->fired_direction()))) {
1338           ProcessRemovalOfRemoteTrack(transceiver, &remove_list,
1339                                       &removed_streams);
1340         }
1341         // 2.2.7.1.6.2: Set transceiver's [[CurrentDirection]] and
1342         // [[FiredDirection]] slots to direction.
1343         transceiver->internal()->set_current_direction(media_desc->direction());
1344         transceiver->internal()->set_fired_direction(media_desc->direction());
1345       }
1346     }
1347     auto observer = pc_->Observer();
1348     for (const auto& transceiver : remove_list) {
1349       observer->OnRemoveTrack(transceiver->receiver());
1350     }
1351     for (const auto& stream : removed_streams) {
1352       observer->OnRemoveStream(stream);
1353     }
1354   } else {
1355     // Media channels will be created only when offer is set. These may use new
1356     // transports just created by PushdownTransportDescription.
1357     if (type == SdpType::kOffer) {
1358       // TODO(bugs.webrtc.org/4676) - Handle CreateChannel failure, as new local
1359       // description is applied. Restore back to old description.
1360       RTCError error = CreateChannels(*local_description()->description());
1361       if (!error.ok()) {
1362         return error;
1363       }
1364     }
1365     // Remove unused channels if MediaContentDescription is rejected.
1366     RemoveUnusedChannels(local_description()->description());
1367   }
1368 
1369   error = UpdateSessionState(type, cricket::CS_LOCAL,
1370                              local_description()->description());
1371   if (!error.ok()) {
1372     return error;
1373   }
1374 
1375   if (remote_description()) {
1376     // Now that we have a local description, we can push down remote candidates.
1377     UseCandidatesInSessionDescription(remote_description());
1378   }
1379 
1380   pending_ice_restarts_.clear();
1381   if (session_error() != SessionError::kNone) {
1382     LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, GetSessionErrorMsg());
1383   }
1384 
1385   // If setting the description decided our SSL role, allocate any necessary
1386   // SCTP sids.
1387   rtc::SSLRole role;
1388   if (IsSctpLike(pc_->data_channel_type()) && pc_->GetSctpSslRole(&role)) {
1389     data_channel_controller()->AllocateSctpSids(role);
1390   }
1391 
1392   if (IsUnifiedPlan()) {
1393     for (const auto& transceiver : transceivers()->List()) {
1394       if (transceiver->stopped()) {
1395         continue;
1396       }
1397       const ContentInfo* content =
1398           FindMediaSectionForTransceiver(transceiver, local_description());
1399       if (!content) {
1400         continue;
1401       }
1402       cricket::ChannelInterface* channel = transceiver->internal()->channel();
1403       if (content->rejected || !channel || channel->local_streams().empty()) {
1404         // 0 is a special value meaning "this sender has no associated send
1405         // stream". Need to call this so the sender won't attempt to configure
1406         // a no longer existing stream and run into DCHECKs in the lower
1407         // layers.
1408         transceiver->internal()->sender_internal()->SetSsrc(0);
1409       } else {
1410         // Get the StreamParams from the channel which could generate SSRCs.
1411         const std::vector<StreamParams>& streams = channel->local_streams();
1412         transceiver->internal()->sender_internal()->set_stream_ids(
1413             streams[0].stream_ids());
1414         transceiver->internal()->sender_internal()->SetSsrc(
1415             streams[0].first_ssrc());
1416       }
1417     }
1418   } else {
1419     // Plan B semantics.
1420 
1421     // Update state and SSRC of local MediaStreams and DataChannels based on the
1422     // local session description.
1423     const cricket::ContentInfo* audio_content =
1424         GetFirstAudioContent(local_description()->description());
1425     if (audio_content) {
1426       if (audio_content->rejected) {
1427         RemoveSenders(cricket::MEDIA_TYPE_AUDIO);
1428       } else {
1429         const cricket::AudioContentDescription* audio_desc =
1430             audio_content->media_description()->as_audio();
1431         UpdateLocalSenders(audio_desc->streams(), audio_desc->type());
1432       }
1433     }
1434 
1435     const cricket::ContentInfo* video_content =
1436         GetFirstVideoContent(local_description()->description());
1437     if (video_content) {
1438       if (video_content->rejected) {
1439         RemoveSenders(cricket::MEDIA_TYPE_VIDEO);
1440       } else {
1441         const cricket::VideoContentDescription* video_desc =
1442             video_content->media_description()->as_video();
1443         UpdateLocalSenders(video_desc->streams(), video_desc->type());
1444       }
1445     }
1446   }
1447 
1448   const cricket::ContentInfo* data_content =
1449       GetFirstDataContent(local_description()->description());
1450   if (data_content) {
1451     const cricket::RtpDataContentDescription* rtp_data_desc =
1452         data_content->media_description()->as_rtp_data();
1453     // rtp_data_desc will be null if this is an SCTP description.
1454     if (rtp_data_desc) {
1455       data_channel_controller()->UpdateLocalRtpDataChannels(
1456           rtp_data_desc->streams());
1457     }
1458   }
1459 
1460   if (type == SdpType::kAnswer &&
1461       local_ice_credentials_to_replace_->SatisfiesIceRestart(
1462           *current_local_description_)) {
1463     local_ice_credentials_to_replace_->ClearIceCredentials();
1464   }
1465 
1466   return RTCError::OK();
1467 }
1468 
SetRemoteDescription(SetSessionDescriptionObserver * observer,SessionDescriptionInterface * desc_ptr)1469 void SdpOfferAnswerHandler::SetRemoteDescription(
1470     SetSessionDescriptionObserver* observer,
1471     SessionDescriptionInterface* desc_ptr) {
1472   RTC_DCHECK_RUN_ON(signaling_thread());
1473   // Chain this operation. If asynchronous operations are pending on the chain,
1474   // this operation will be queued to be invoked, otherwise the contents of the
1475   // lambda will execute immediately.
1476   operations_chain_->ChainOperation(
1477       [this_weak_ptr = weak_ptr_factory_.GetWeakPtr(),
1478        observer_refptr =
1479            rtc::scoped_refptr<SetSessionDescriptionObserver>(observer),
1480        desc = std::unique_ptr<SessionDescriptionInterface>(desc_ptr)](
1481           std::function<void()> operations_chain_callback) mutable {
1482         // Abort early if |this_weak_ptr| is no longer valid.
1483         if (!this_weak_ptr) {
1484           // For consistency with SetSessionDescriptionObserverAdapter whose
1485           // posted messages doesn't get processed when the PC is destroyed, we
1486           // do not inform |observer_refptr| that the operation failed.
1487           operations_chain_callback();
1488           return;
1489         }
1490         // SetSessionDescriptionObserverAdapter takes care of making sure the
1491         // |observer_refptr| is invoked in a posted message.
1492         this_weak_ptr->DoSetRemoteDescription(
1493             std::move(desc),
1494             rtc::scoped_refptr<SetRemoteDescriptionObserverInterface>(
1495                 new rtc::RefCountedObject<SetSessionDescriptionObserverAdapter>(
1496                     this_weak_ptr, observer_refptr)));
1497         // For backwards-compatability reasons, we declare the operation as
1498         // completed here (rather than in a post), so that the operation chain
1499         // is not blocked by this operation when the observer is invoked. This
1500         // allows the observer to trigger subsequent offer/answer operations
1501         // synchronously if the operation chain is now empty.
1502         operations_chain_callback();
1503       });
1504 }
1505 
SetRemoteDescription(std::unique_ptr<SessionDescriptionInterface> desc,rtc::scoped_refptr<SetRemoteDescriptionObserverInterface> observer)1506 void SdpOfferAnswerHandler::SetRemoteDescription(
1507     std::unique_ptr<SessionDescriptionInterface> desc,
1508     rtc::scoped_refptr<SetRemoteDescriptionObserverInterface> observer) {
1509   RTC_DCHECK_RUN_ON(signaling_thread());
1510   // Chain this operation. If asynchronous operations are pending on the chain,
1511   // this operation will be queued to be invoked, otherwise the contents of the
1512   // lambda will execute immediately.
1513   operations_chain_->ChainOperation(
1514       [this_weak_ptr = weak_ptr_factory_.GetWeakPtr(), observer,
1515        desc = std::move(desc)](
1516           std::function<void()> operations_chain_callback) mutable {
1517         // Abort early if |this_weak_ptr| is no longer valid.
1518         if (!this_weak_ptr) {
1519           observer->OnSetRemoteDescriptionComplete(RTCError(
1520               RTCErrorType::INTERNAL_ERROR,
1521               "SetRemoteDescription failed because the session was shut down"));
1522           operations_chain_callback();
1523           return;
1524         }
1525         this_weak_ptr->DoSetRemoteDescription(std::move(desc),
1526                                               std::move(observer));
1527         // DoSetRemoteDescription() is implemented as a synchronous operation.
1528         // The |observer| will already have been informed that it completed, and
1529         // we can mark this operation as complete without any loose ends.
1530         operations_chain_callback();
1531       });
1532 }
1533 
ApplyRemoteDescription(std::unique_ptr<SessionDescriptionInterface> desc)1534 RTCError SdpOfferAnswerHandler::ApplyRemoteDescription(
1535     std::unique_ptr<SessionDescriptionInterface> desc) {
1536   RTC_DCHECK_RUN_ON(signaling_thread());
1537   RTC_DCHECK(desc);
1538 
1539   // Update stats here so that we have the most recent stats for tracks and
1540   // streams that might be removed by updating the session description.
1541   pc_->stats()->UpdateStats(PeerConnectionInterface::kStatsOutputLevelStandard);
1542 
1543   // Take a reference to the old remote description since it's used below to
1544   // compare against the new remote description. When setting the new remote
1545   // description, grab ownership of the replaced session description in case it
1546   // is the same as |old_remote_description|, to keep it alive for the duration
1547   // of the method.
1548   const SessionDescriptionInterface* old_remote_description =
1549       remote_description();
1550   std::unique_ptr<SessionDescriptionInterface> replaced_remote_description;
1551   SdpType type = desc->GetType();
1552   if (type == SdpType::kAnswer) {
1553     replaced_remote_description = pending_remote_description_
1554                                       ? std::move(pending_remote_description_)
1555                                       : std::move(current_remote_description_);
1556     current_remote_description_ = std::move(desc);
1557     pending_remote_description_ = nullptr;
1558     current_local_description_ = std::move(pending_local_description_);
1559   } else {
1560     replaced_remote_description = std::move(pending_remote_description_);
1561     pending_remote_description_ = std::move(desc);
1562   }
1563   // The session description to apply now must be accessed by
1564   // |remote_description()|.
1565   RTC_DCHECK(remote_description());
1566 
1567   // Report statistics about any use of simulcast.
1568   ReportSimulcastApiVersion(kSimulcastVersionApplyRemoteDescription,
1569                             *remote_description()->description());
1570 
1571   RTCError error = PushdownTransportDescription(cricket::CS_REMOTE, type);
1572   if (!error.ok()) {
1573     return error;
1574   }
1575   // Transport and Media channels will be created only when offer is set.
1576   if (IsUnifiedPlan()) {
1577     RTCError error = UpdateTransceiversAndDataChannels(
1578         cricket::CS_REMOTE, *remote_description(), local_description(),
1579         old_remote_description);
1580     if (!error.ok()) {
1581       return error;
1582     }
1583   } else {
1584     // Media channels will be created only when offer is set. These may use new
1585     // transports just created by PushdownTransportDescription.
1586     if (type == SdpType::kOffer) {
1587       // TODO(mallinath) - Handle CreateChannel failure, as new local
1588       // description is applied. Restore back to old description.
1589       RTCError error = CreateChannels(*remote_description()->description());
1590       if (!error.ok()) {
1591         return error;
1592       }
1593     }
1594     // Remove unused channels if MediaContentDescription is rejected.
1595     RemoveUnusedChannels(remote_description()->description());
1596   }
1597 
1598   // NOTE: Candidates allocation will be initiated only when
1599   // SetLocalDescription is called.
1600   error = UpdateSessionState(type, cricket::CS_REMOTE,
1601                              remote_description()->description());
1602   if (!error.ok()) {
1603     return error;
1604   }
1605 
1606   if (local_description() &&
1607       !UseCandidatesInSessionDescription(remote_description())) {
1608     LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, kInvalidCandidates);
1609   }
1610 
1611   if (old_remote_description) {
1612     for (const cricket::ContentInfo& content :
1613          old_remote_description->description()->contents()) {
1614       // Check if this new SessionDescription contains new ICE ufrag and
1615       // password that indicates the remote peer requests an ICE restart.
1616       // TODO(deadbeef): When we start storing both the current and pending
1617       // remote description, this should reset pending_ice_restarts and compare
1618       // against the current description.
1619       if (CheckForRemoteIceRestart(old_remote_description, remote_description(),
1620                                    content.name)) {
1621         if (type == SdpType::kOffer) {
1622           pending_ice_restarts_.insert(content.name);
1623         }
1624       } else {
1625         // We retain all received candidates only if ICE is not restarted.
1626         // When ICE is restarted, all previous candidates belong to an old
1627         // generation and should not be kept.
1628         // TODO(deadbeef): This goes against the W3C spec which says the remote
1629         // description should only contain candidates from the last set remote
1630         // description plus any candidates added since then. We should remove
1631         // this once we're sure it won't break anything.
1632         WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription(
1633             old_remote_description, content.name, mutable_remote_description());
1634       }
1635     }
1636   }
1637 
1638   if (session_error() != SessionError::kNone) {
1639     LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, GetSessionErrorMsg());
1640   }
1641 
1642   // Set the the ICE connection state to connecting since the connection may
1643   // become writable with peer reflexive candidates before any remote candidate
1644   // is signaled.
1645   // TODO(pthatcher): This is a short-term solution for crbug/446908. A real fix
1646   // is to have a new signal the indicates a change in checking state from the
1647   // transport and expose a new checking() member from transport that can be
1648   // read to determine the current checking state. The existing SignalConnecting
1649   // actually means "gathering candidates", so cannot be be used here.
1650   if (remote_description()->GetType() != SdpType::kOffer &&
1651       remote_description()->number_of_mediasections() > 0u &&
1652       pc_->ice_connection_state() ==
1653           PeerConnectionInterface::kIceConnectionNew) {
1654     pc_->SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
1655   }
1656 
1657   // If setting the description decided our SSL role, allocate any necessary
1658   // SCTP sids.
1659   rtc::SSLRole role;
1660   if (IsSctpLike(pc_->data_channel_type()) && pc_->GetSctpSslRole(&role)) {
1661     data_channel_controller()->AllocateSctpSids(role);
1662   }
1663 
1664   if (IsUnifiedPlan()) {
1665     std::vector<rtc::scoped_refptr<RtpTransceiverInterface>>
1666         now_receiving_transceivers;
1667     std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> remove_list;
1668     std::vector<rtc::scoped_refptr<MediaStreamInterface>> added_streams;
1669     std::vector<rtc::scoped_refptr<MediaStreamInterface>> removed_streams;
1670     for (const auto& transceiver : transceivers()->List()) {
1671       const ContentInfo* content =
1672           FindMediaSectionForTransceiver(transceiver, remote_description());
1673       if (!content) {
1674         continue;
1675       }
1676       const MediaContentDescription* media_desc = content->media_description();
1677       RtpTransceiverDirection local_direction =
1678           RtpTransceiverDirectionReversed(media_desc->direction());
1679       // Roughly the same as steps 2.2.8.6 of section 4.4.1.6 "Set the
1680       // RTCSessionDescription: Set the associated remote streams given
1681       // transceiver.[[Receiver]], msids, addList, and removeList".
1682       // https://w3c.github.io/webrtc-pc/#set-the-rtcsessiondescription
1683       if (RtpTransceiverDirectionHasRecv(local_direction)) {
1684         std::vector<std::string> stream_ids;
1685         if (!media_desc->streams().empty()) {
1686           // The remote description has signaled the stream IDs.
1687           stream_ids = media_desc->streams()[0].stream_ids();
1688         }
1689         transceivers()
1690             ->StableState(transceiver)
1691             ->SetRemoteStreamIdsIfUnset(transceiver->receiver()->stream_ids());
1692 
1693         RTC_LOG(LS_INFO) << "Processing the MSIDs for MID=" << content->name
1694                          << " (" << GetStreamIdsString(stream_ids) << ").";
1695         SetAssociatedRemoteStreams(transceiver->internal()->receiver_internal(),
1696                                    stream_ids, &added_streams,
1697                                    &removed_streams);
1698         // From the WebRTC specification, steps 2.2.8.5/6 of section 4.4.1.6
1699         // "Set the RTCSessionDescription: If direction is sendrecv or recvonly,
1700         // and transceiver's current direction is neither sendrecv nor recvonly,
1701         // process the addition of a remote track for the media description.
1702         if (!transceiver->fired_direction() ||
1703             !RtpTransceiverDirectionHasRecv(*transceiver->fired_direction())) {
1704           RTC_LOG(LS_INFO)
1705               << "Processing the addition of a remote track for MID="
1706               << content->name << ".";
1707           now_receiving_transceivers.push_back(transceiver);
1708         }
1709       }
1710       // 2.2.8.1.9: If direction is "sendonly" or "inactive", and transceiver's
1711       // [[FiredDirection]] slot is either "sendrecv" or "recvonly", process the
1712       // removal of a remote track for the media description, given transceiver,
1713       // removeList, and muteTracks.
1714       if (!RtpTransceiverDirectionHasRecv(local_direction) &&
1715           (transceiver->fired_direction() &&
1716            RtpTransceiverDirectionHasRecv(*transceiver->fired_direction()))) {
1717         ProcessRemovalOfRemoteTrack(transceiver, &remove_list,
1718                                     &removed_streams);
1719       }
1720       // 2.2.8.1.10: Set transceiver's [[FiredDirection]] slot to direction.
1721       transceiver->internal()->set_fired_direction(local_direction);
1722       // 2.2.8.1.11: If description is of type "answer" or "pranswer", then run
1723       // the following steps:
1724       if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) {
1725         // 2.2.8.1.11.1: Set transceiver's [[CurrentDirection]] slot to
1726         // direction.
1727         transceiver->internal()->set_current_direction(local_direction);
1728         // 2.2.8.1.11.[3-6]: Set the transport internal slots.
1729         if (transceiver->mid()) {
1730           auto dtls_transport =
1731               transport_controller()->LookupDtlsTransportByMid(
1732                   *transceiver->mid());
1733           transceiver->internal()->sender_internal()->set_transport(
1734               dtls_transport);
1735           transceiver->internal()->receiver_internal()->set_transport(
1736               dtls_transport);
1737         }
1738       }
1739       // 2.2.8.1.12: If the media description is rejected, and transceiver is
1740       // not already stopped, stop the RTCRtpTransceiver transceiver.
1741       if (content->rejected && !transceiver->stopped()) {
1742         RTC_LOG(LS_INFO) << "Stopping transceiver for MID=" << content->name
1743                          << " since the media section was rejected.";
1744         transceiver->internal()->StopTransceiverProcedure();
1745       }
1746       if (!content->rejected &&
1747           RtpTransceiverDirectionHasRecv(local_direction)) {
1748         if (!media_desc->streams().empty() &&
1749             media_desc->streams()[0].has_ssrcs()) {
1750           uint32_t ssrc = media_desc->streams()[0].first_ssrc();
1751           transceiver->internal()->receiver_internal()->SetupMediaChannel(ssrc);
1752         } else {
1753           transceiver->internal()
1754               ->receiver_internal()
1755               ->SetupUnsignaledMediaChannel();
1756         }
1757       }
1758     }
1759     // Once all processing has finished, fire off callbacks.
1760     auto observer = pc_->Observer();
1761     for (const auto& transceiver : now_receiving_transceivers) {
1762       pc_->stats()->AddTrack(transceiver->receiver()->track());
1763       observer->OnTrack(transceiver);
1764       observer->OnAddTrack(transceiver->receiver(),
1765                            transceiver->receiver()->streams());
1766     }
1767     for (const auto& stream : added_streams) {
1768       observer->OnAddStream(stream);
1769     }
1770     for (const auto& transceiver : remove_list) {
1771       observer->OnRemoveTrack(transceiver->receiver());
1772     }
1773     for (const auto& stream : removed_streams) {
1774       observer->OnRemoveStream(stream);
1775     }
1776   }
1777 
1778   const cricket::ContentInfo* audio_content =
1779       GetFirstAudioContent(remote_description()->description());
1780   const cricket::ContentInfo* video_content =
1781       GetFirstVideoContent(remote_description()->description());
1782   const cricket::AudioContentDescription* audio_desc =
1783       GetFirstAudioContentDescription(remote_description()->description());
1784   const cricket::VideoContentDescription* video_desc =
1785       GetFirstVideoContentDescription(remote_description()->description());
1786   const cricket::RtpDataContentDescription* rtp_data_desc =
1787       GetFirstRtpDataContentDescription(remote_description()->description());
1788 
1789   // Check if the descriptions include streams, just in case the peer supports
1790   // MSID, but doesn't indicate so with "a=msid-semantic".
1791   if (remote_description()->description()->msid_supported() ||
1792       (audio_desc && !audio_desc->streams().empty()) ||
1793       (video_desc && !video_desc->streams().empty())) {
1794     remote_peer_supports_msid_ = true;
1795   }
1796 
1797   // We wait to signal new streams until we finish processing the description,
1798   // since only at that point will new streams have all their tracks.
1799   rtc::scoped_refptr<StreamCollection> new_streams(StreamCollection::Create());
1800 
1801   if (!IsUnifiedPlan()) {
1802     // TODO(steveanton): When removing RTP senders/receivers in response to a
1803     // rejected media section, there is some cleanup logic that expects the
1804     // voice/ video channel to still be set. But in this method the voice/video
1805     // channel would have been destroyed by the SetRemoteDescription caller
1806     // above so the cleanup that relies on them fails to run. The RemoveSenders
1807     // calls should be moved to right before the DestroyChannel calls to fix
1808     // this.
1809 
1810     // Find all audio rtp streams and create corresponding remote AudioTracks
1811     // and MediaStreams.
1812     if (audio_content) {
1813       if (audio_content->rejected) {
1814         RemoveSenders(cricket::MEDIA_TYPE_AUDIO);
1815       } else {
1816         bool default_audio_track_needed =
1817             !remote_peer_supports_msid_ &&
1818             RtpTransceiverDirectionHasSend(audio_desc->direction());
1819         UpdateRemoteSendersList(GetActiveStreams(audio_desc),
1820                                 default_audio_track_needed, audio_desc->type(),
1821                                 new_streams);
1822       }
1823     }
1824 
1825     // Find all video rtp streams and create corresponding remote VideoTracks
1826     // and MediaStreams.
1827     if (video_content) {
1828       if (video_content->rejected) {
1829         RemoveSenders(cricket::MEDIA_TYPE_VIDEO);
1830       } else {
1831         bool default_video_track_needed =
1832             !remote_peer_supports_msid_ &&
1833             RtpTransceiverDirectionHasSend(video_desc->direction());
1834         UpdateRemoteSendersList(GetActiveStreams(video_desc),
1835                                 default_video_track_needed, video_desc->type(),
1836                                 new_streams);
1837       }
1838     }
1839 
1840     // If this is an RTP data transport, update the DataChannels with the
1841     // information from the remote peer.
1842     if (rtp_data_desc) {
1843       data_channel_controller()->UpdateRemoteRtpDataChannels(
1844           GetActiveStreams(rtp_data_desc));
1845     }
1846 
1847     // Iterate new_streams and notify the observer about new MediaStreams.
1848     auto observer = pc_->Observer();
1849     for (size_t i = 0; i < new_streams->count(); ++i) {
1850       MediaStreamInterface* new_stream = new_streams->at(i);
1851       pc_->stats()->AddStream(new_stream);
1852       observer->OnAddStream(
1853           rtc::scoped_refptr<MediaStreamInterface>(new_stream));
1854     }
1855 
1856     UpdateEndedRemoteMediaStreams();
1857   }
1858 
1859   if (type == SdpType::kAnswer &&
1860       local_ice_credentials_to_replace_->SatisfiesIceRestart(
1861           *current_local_description_)) {
1862     local_ice_credentials_to_replace_->ClearIceCredentials();
1863   }
1864 
1865   return RTCError::OK();
1866 }
1867 
DoSetLocalDescription(std::unique_ptr<SessionDescriptionInterface> desc,rtc::scoped_refptr<SetLocalDescriptionObserverInterface> observer)1868 void SdpOfferAnswerHandler::DoSetLocalDescription(
1869     std::unique_ptr<SessionDescriptionInterface> desc,
1870     rtc::scoped_refptr<SetLocalDescriptionObserverInterface> observer) {
1871   RTC_DCHECK_RUN_ON(signaling_thread());
1872   TRACE_EVENT0("webrtc", "SdpOfferAnswerHandler::DoSetLocalDescription");
1873 
1874   if (!observer) {
1875     RTC_LOG(LS_ERROR) << "SetLocalDescription - observer is NULL.";
1876     return;
1877   }
1878 
1879   if (!desc) {
1880     observer->OnSetLocalDescriptionComplete(
1881         RTCError(RTCErrorType::INTERNAL_ERROR, "SessionDescription is NULL."));
1882     return;
1883   }
1884 
1885   // If a session error has occurred the PeerConnection is in a possibly
1886   // inconsistent state so fail right away.
1887   if (session_error() != SessionError::kNone) {
1888     std::string error_message = GetSessionErrorMsg();
1889     RTC_LOG(LS_ERROR) << "SetLocalDescription: " << error_message;
1890     observer->OnSetLocalDescriptionComplete(
1891         RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message)));
1892     return;
1893   }
1894 
1895   // For SLD we support only explicit rollback.
1896   if (desc->GetType() == SdpType::kRollback) {
1897     if (IsUnifiedPlan()) {
1898       observer->OnSetLocalDescriptionComplete(Rollback(desc->GetType()));
1899     } else {
1900       observer->OnSetLocalDescriptionComplete(
1901           RTCError(RTCErrorType::UNSUPPORTED_OPERATION,
1902                    "Rollback not supported in Plan B"));
1903     }
1904     return;
1905   }
1906 
1907   RTCError error = ValidateSessionDescription(desc.get(), cricket::CS_LOCAL);
1908   if (!error.ok()) {
1909     std::string error_message = GetSetDescriptionErrorMessage(
1910         cricket::CS_LOCAL, desc->GetType(), error);
1911     RTC_LOG(LS_ERROR) << error_message;
1912     observer->OnSetLocalDescriptionComplete(
1913         RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message)));
1914     return;
1915   }
1916 
1917   // Grab the description type before moving ownership to ApplyLocalDescription,
1918   // which may destroy it before returning.
1919   const SdpType type = desc->GetType();
1920 
1921   error = ApplyLocalDescription(std::move(desc));
1922   // |desc| may be destroyed at this point.
1923 
1924   if (!error.ok()) {
1925     // If ApplyLocalDescription fails, the PeerConnection could be in an
1926     // inconsistent state, so act conservatively here and set the session error
1927     // so that future calls to SetLocalDescription/SetRemoteDescription fail.
1928     SetSessionError(SessionError::kContent, error.message());
1929     std::string error_message =
1930         GetSetDescriptionErrorMessage(cricket::CS_LOCAL, type, error);
1931     RTC_LOG(LS_ERROR) << error_message;
1932     observer->OnSetLocalDescriptionComplete(
1933         RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message)));
1934     return;
1935   }
1936   RTC_DCHECK(local_description());
1937 
1938   if (local_description()->GetType() == SdpType::kAnswer) {
1939     RemoveStoppedTransceivers();
1940 
1941     // TODO(deadbeef): We already had to hop to the network thread for
1942     // MaybeStartGathering...
1943     pc_->network_thread()->Invoke<void>(
1944         RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::DiscardCandidatePool,
1945                                  port_allocator()));
1946     // Make UMA notes about what was agreed to.
1947     ReportNegotiatedSdpSemantics(*local_description());
1948   }
1949 
1950   observer->OnSetLocalDescriptionComplete(RTCError::OK());
1951   pc_->NoteUsageEvent(UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED);
1952 
1953   // Check if negotiation is needed. We must do this after informing the
1954   // observer that SetLocalDescription() has completed to ensure negotiation is
1955   // not needed prior to the promise resolving.
1956   if (IsUnifiedPlan()) {
1957     bool was_negotiation_needed = is_negotiation_needed_;
1958     UpdateNegotiationNeeded();
1959     if (signaling_state() == PeerConnectionInterface::kStable &&
1960         was_negotiation_needed && is_negotiation_needed_) {
1961       // Legacy version.
1962       pc_->Observer()->OnRenegotiationNeeded();
1963       // Spec-compliant version; the event may get invalidated before firing.
1964       GenerateNegotiationNeededEvent();
1965     }
1966   }
1967 
1968   // MaybeStartGathering needs to be called after informing the observer so that
1969   // we don't signal any candidates before signaling that SetLocalDescription
1970   // completed.
1971   transport_controller()->MaybeStartGathering();
1972 }
1973 
DoCreateOffer(const PeerConnectionInterface::RTCOfferAnswerOptions & options,rtc::scoped_refptr<CreateSessionDescriptionObserver> observer)1974 void SdpOfferAnswerHandler::DoCreateOffer(
1975     const PeerConnectionInterface::RTCOfferAnswerOptions& options,
1976     rtc::scoped_refptr<CreateSessionDescriptionObserver> observer) {
1977   RTC_DCHECK_RUN_ON(signaling_thread());
1978   TRACE_EVENT0("webrtc", "SdpOfferAnswerHandler::DoCreateOffer");
1979 
1980   if (!observer) {
1981     RTC_LOG(LS_ERROR) << "CreateOffer - observer is NULL.";
1982     return;
1983   }
1984 
1985   if (pc_->IsClosed()) {
1986     std::string error = "CreateOffer called when PeerConnection is closed.";
1987     RTC_LOG(LS_ERROR) << error;
1988     pc_->message_handler()->PostCreateSessionDescriptionFailure(
1989         observer, RTCError(RTCErrorType::INVALID_STATE, std::move(error)));
1990     return;
1991   }
1992 
1993   // If a session error has occurred the PeerConnection is in a possibly
1994   // inconsistent state so fail right away.
1995   if (session_error() != SessionError::kNone) {
1996     std::string error_message = GetSessionErrorMsg();
1997     RTC_LOG(LS_ERROR) << "CreateOffer: " << error_message;
1998     pc_->message_handler()->PostCreateSessionDescriptionFailure(
1999         observer,
2000         RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message)));
2001     return;
2002   }
2003 
2004   if (!ValidateOfferAnswerOptions(options)) {
2005     std::string error = "CreateOffer called with invalid options.";
2006     RTC_LOG(LS_ERROR) << error;
2007     pc_->message_handler()->PostCreateSessionDescriptionFailure(
2008         observer, RTCError(RTCErrorType::INVALID_PARAMETER, std::move(error)));
2009     return;
2010   }
2011 
2012   // Legacy handling for offer_to_receive_audio and offer_to_receive_video.
2013   // Specified in WebRTC section 4.4.3.2 "Legacy configuration extensions".
2014   if (IsUnifiedPlan()) {
2015     RTCError error = HandleLegacyOfferOptions(options);
2016     if (!error.ok()) {
2017       pc_->message_handler()->PostCreateSessionDescriptionFailure(
2018           observer, std::move(error));
2019       return;
2020     }
2021   }
2022 
2023   cricket::MediaSessionOptions session_options;
2024   GetOptionsForOffer(options, &session_options);
2025   webrtc_session_desc_factory_->CreateOffer(observer, options, session_options);
2026 }
2027 
CreateAnswer(CreateSessionDescriptionObserver * observer,const PeerConnectionInterface::RTCOfferAnswerOptions & options)2028 void SdpOfferAnswerHandler::CreateAnswer(
2029     CreateSessionDescriptionObserver* observer,
2030     const PeerConnectionInterface::RTCOfferAnswerOptions& options) {
2031   RTC_DCHECK_RUN_ON(signaling_thread());
2032   // Chain this operation. If asynchronous operations are pending on the chain,
2033   // this operation will be queued to be invoked, otherwise the contents of the
2034   // lambda will execute immediately.
2035   operations_chain_->ChainOperation(
2036       [this_weak_ptr = weak_ptr_factory_.GetWeakPtr(),
2037        observer_refptr =
2038            rtc::scoped_refptr<CreateSessionDescriptionObserver>(observer),
2039        options](std::function<void()> operations_chain_callback) {
2040         // Abort early if |this_weak_ptr| is no longer valid.
2041         if (!this_weak_ptr) {
2042           observer_refptr->OnFailure(RTCError(
2043               RTCErrorType::INTERNAL_ERROR,
2044               "CreateAnswer failed because the session was shut down"));
2045           operations_chain_callback();
2046           return;
2047         }
2048         // The operation completes asynchronously when the wrapper is invoked.
2049         rtc::scoped_refptr<CreateSessionDescriptionObserverOperationWrapper>
2050             observer_wrapper(new rtc::RefCountedObject<
2051                              CreateSessionDescriptionObserverOperationWrapper>(
2052                 std::move(observer_refptr),
2053                 std::move(operations_chain_callback)));
2054         this_weak_ptr->DoCreateAnswer(options, observer_wrapper);
2055       });
2056 }
2057 
DoCreateAnswer(const PeerConnectionInterface::RTCOfferAnswerOptions & options,rtc::scoped_refptr<CreateSessionDescriptionObserver> observer)2058 void SdpOfferAnswerHandler::DoCreateAnswer(
2059     const PeerConnectionInterface::RTCOfferAnswerOptions& options,
2060     rtc::scoped_refptr<CreateSessionDescriptionObserver> observer) {
2061   RTC_DCHECK_RUN_ON(signaling_thread());
2062   TRACE_EVENT0("webrtc", "SdpOfferAnswerHandler::DoCreateAnswer");
2063   if (!observer) {
2064     RTC_LOG(LS_ERROR) << "CreateAnswer - observer is NULL.";
2065     return;
2066   }
2067 
2068   // If a session error has occurred the PeerConnection is in a possibly
2069   // inconsistent state so fail right away.
2070   if (session_error() != SessionError::kNone) {
2071     std::string error_message = GetSessionErrorMsg();
2072     RTC_LOG(LS_ERROR) << "CreateAnswer: " << error_message;
2073     pc_->message_handler()->PostCreateSessionDescriptionFailure(
2074         observer,
2075         RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message)));
2076     return;
2077   }
2078 
2079   if (!(signaling_state_ == PeerConnectionInterface::kHaveRemoteOffer ||
2080         signaling_state_ == PeerConnectionInterface::kHaveLocalPrAnswer)) {
2081     std::string error =
2082         "PeerConnection cannot create an answer in a state other than "
2083         "have-remote-offer or have-local-pranswer.";
2084     RTC_LOG(LS_ERROR) << error;
2085     pc_->message_handler()->PostCreateSessionDescriptionFailure(
2086         observer, RTCError(RTCErrorType::INVALID_STATE, std::move(error)));
2087     return;
2088   }
2089 
2090   // The remote description should be set if we're in the right state.
2091   RTC_DCHECK(remote_description());
2092 
2093   if (IsUnifiedPlan()) {
2094     if (options.offer_to_receive_audio !=
2095         PeerConnectionInterface::RTCOfferAnswerOptions::kUndefined) {
2096       RTC_LOG(LS_WARNING) << "CreateAnswer: offer_to_receive_audio is not "
2097                              "supported with Unified Plan semantics. Use the "
2098                              "RtpTransceiver API instead.";
2099     }
2100     if (options.offer_to_receive_video !=
2101         PeerConnectionInterface::RTCOfferAnswerOptions::kUndefined) {
2102       RTC_LOG(LS_WARNING) << "CreateAnswer: offer_to_receive_video is not "
2103                              "supported with Unified Plan semantics. Use the "
2104                              "RtpTransceiver API instead.";
2105     }
2106   }
2107 
2108   cricket::MediaSessionOptions session_options;
2109   GetOptionsForAnswer(options, &session_options);
2110   webrtc_session_desc_factory_->CreateAnswer(observer, session_options);
2111 }
2112 
DoSetRemoteDescription(std::unique_ptr<SessionDescriptionInterface> desc,rtc::scoped_refptr<SetRemoteDescriptionObserverInterface> observer)2113 void SdpOfferAnswerHandler::DoSetRemoteDescription(
2114     std::unique_ptr<SessionDescriptionInterface> desc,
2115     rtc::scoped_refptr<SetRemoteDescriptionObserverInterface> observer) {
2116   RTC_DCHECK_RUN_ON(signaling_thread());
2117   TRACE_EVENT0("webrtc", "SdpOfferAnswerHandler::DoSetRemoteDescription");
2118 
2119   if (!observer) {
2120     RTC_LOG(LS_ERROR) << "SetRemoteDescription - observer is NULL.";
2121     return;
2122   }
2123 
2124   if (!desc) {
2125     observer->OnSetRemoteDescriptionComplete(RTCError(
2126         RTCErrorType::INVALID_PARAMETER, "SessionDescription is NULL."));
2127     return;
2128   }
2129 
2130   // If a session error has occurred the PeerConnection is in a possibly
2131   // inconsistent state so fail right away.
2132   if (session_error() != SessionError::kNone) {
2133     std::string error_message = GetSessionErrorMsg();
2134     RTC_LOG(LS_ERROR) << "SetRemoteDescription: " << error_message;
2135     observer->OnSetRemoteDescriptionComplete(
2136         RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message)));
2137     return;
2138   }
2139   if (IsUnifiedPlan()) {
2140     if (pc_->configuration()->enable_implicit_rollback) {
2141       if (desc->GetType() == SdpType::kOffer &&
2142           signaling_state() == PeerConnectionInterface::kHaveLocalOffer) {
2143         Rollback(desc->GetType());
2144       }
2145     }
2146     // Explicit rollback.
2147     if (desc->GetType() == SdpType::kRollback) {
2148       observer->OnSetRemoteDescriptionComplete(Rollback(desc->GetType()));
2149       return;
2150     }
2151   } else if (desc->GetType() == SdpType::kRollback) {
2152     observer->OnSetRemoteDescriptionComplete(
2153         RTCError(RTCErrorType::UNSUPPORTED_OPERATION,
2154                  "Rollback not supported in Plan B"));
2155     return;
2156   }
2157   if (desc->GetType() == SdpType::kOffer ||
2158       desc->GetType() == SdpType::kAnswer) {
2159     // Report to UMA the format of the received offer or answer.
2160     pc_->ReportSdpFormatReceived(*desc);
2161   }
2162 
2163   // Handle remote descriptions missing a=mid lines for interop with legacy end
2164   // points.
2165   FillInMissingRemoteMids(desc->description());
2166 
2167   RTCError error = ValidateSessionDescription(desc.get(), cricket::CS_REMOTE);
2168   if (!error.ok()) {
2169     std::string error_message = GetSetDescriptionErrorMessage(
2170         cricket::CS_REMOTE, desc->GetType(), error);
2171     RTC_LOG(LS_ERROR) << error_message;
2172     observer->OnSetRemoteDescriptionComplete(
2173         RTCError(error.type(), std::move(error_message)));
2174     return;
2175   }
2176 
2177   // Grab the description type before moving ownership to
2178   // ApplyRemoteDescription, which may destroy it before returning.
2179   const SdpType type = desc->GetType();
2180 
2181   error = ApplyRemoteDescription(std::move(desc));
2182   // |desc| may be destroyed at this point.
2183 
2184   if (!error.ok()) {
2185     // If ApplyRemoteDescription fails, the PeerConnection could be in an
2186     // inconsistent state, so act conservatively here and set the session error
2187     // so that future calls to SetLocalDescription/SetRemoteDescription fail.
2188     SetSessionError(SessionError::kContent, error.message());
2189     std::string error_message =
2190         GetSetDescriptionErrorMessage(cricket::CS_REMOTE, type, error);
2191     RTC_LOG(LS_ERROR) << error_message;
2192     observer->OnSetRemoteDescriptionComplete(
2193         RTCError(error.type(), std::move(error_message)));
2194     return;
2195   }
2196   RTC_DCHECK(remote_description());
2197 
2198   if (type == SdpType::kAnswer) {
2199     RemoveStoppedTransceivers();
2200     // TODO(deadbeef): We already had to hop to the network thread for
2201     // MaybeStartGathering...
2202     pc_->network_thread()->Invoke<void>(
2203         RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::DiscardCandidatePool,
2204                                  port_allocator()));
2205     // Make UMA notes about what was agreed to.
2206     ReportNegotiatedSdpSemantics(*remote_description());
2207   }
2208 
2209   observer->OnSetRemoteDescriptionComplete(RTCError::OK());
2210   pc_->NoteUsageEvent(UsageEvent::SET_REMOTE_DESCRIPTION_SUCCEEDED);
2211 
2212   // Check if negotiation is needed. We must do this after informing the
2213   // observer that SetRemoteDescription() has completed to ensure negotiation is
2214   // not needed prior to the promise resolving.
2215   if (IsUnifiedPlan()) {
2216     bool was_negotiation_needed = is_negotiation_needed_;
2217     UpdateNegotiationNeeded();
2218     if (signaling_state() == PeerConnectionInterface::kStable &&
2219         was_negotiation_needed && is_negotiation_needed_) {
2220       // Legacy version.
2221       pc_->Observer()->OnRenegotiationNeeded();
2222       // Spec-compliant version; the event may get invalidated before firing.
2223       GenerateNegotiationNeededEvent();
2224     }
2225   }
2226 }
2227 
SetAssociatedRemoteStreams(rtc::scoped_refptr<RtpReceiverInternal> receiver,const std::vector<std::string> & stream_ids,std::vector<rtc::scoped_refptr<MediaStreamInterface>> * added_streams,std::vector<rtc::scoped_refptr<MediaStreamInterface>> * removed_streams)2228 void SdpOfferAnswerHandler::SetAssociatedRemoteStreams(
2229     rtc::scoped_refptr<RtpReceiverInternal> receiver,
2230     const std::vector<std::string>& stream_ids,
2231     std::vector<rtc::scoped_refptr<MediaStreamInterface>>* added_streams,
2232     std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams) {
2233   RTC_DCHECK_RUN_ON(signaling_thread());
2234   std::vector<rtc::scoped_refptr<MediaStreamInterface>> media_streams;
2235   for (const std::string& stream_id : stream_ids) {
2236     rtc::scoped_refptr<MediaStreamInterface> stream =
2237         remote_streams_->find(stream_id);
2238     if (!stream) {
2239       stream = MediaStreamProxy::Create(rtc::Thread::Current(),
2240                                         MediaStream::Create(stream_id));
2241       remote_streams_->AddStream(stream);
2242       added_streams->push_back(stream);
2243     }
2244     media_streams.push_back(stream);
2245   }
2246   // Special case: "a=msid" missing, use random stream ID.
2247   if (media_streams.empty() &&
2248       !(remote_description()->description()->msid_signaling() &
2249         cricket::kMsidSignalingMediaSection)) {
2250     if (!missing_msid_default_stream_) {
2251       missing_msid_default_stream_ = MediaStreamProxy::Create(
2252           rtc::Thread::Current(), MediaStream::Create(rtc::CreateRandomUuid()));
2253       added_streams->push_back(missing_msid_default_stream_);
2254     }
2255     media_streams.push_back(missing_msid_default_stream_);
2256   }
2257   std::vector<rtc::scoped_refptr<MediaStreamInterface>> previous_streams =
2258       receiver->streams();
2259   // SetStreams() will add/remove the receiver's track to/from the streams. This
2260   // differs from the spec - the spec uses an "addList" and "removeList" to
2261   // update the stream-track relationships in a later step. We do this earlier,
2262   // changing the order of things, but the end-result is the same.
2263   // TODO(hbos): When we remove remote_streams(), use set_stream_ids()
2264   // instead. https://crbug.com/webrtc/9480
2265   receiver->SetStreams(media_streams);
2266   RemoveRemoteStreamsIfEmpty(previous_streams, removed_streams);
2267 }
2268 
AddIceCandidate(const IceCandidateInterface * ice_candidate)2269 bool SdpOfferAnswerHandler::AddIceCandidate(
2270     const IceCandidateInterface* ice_candidate) {
2271   RTC_DCHECK_RUN_ON(signaling_thread());
2272   TRACE_EVENT0("webrtc", "SdpOfferAnswerHandler::AddIceCandidate");
2273   if (pc_->IsClosed()) {
2274     RTC_LOG(LS_ERROR) << "AddIceCandidate: PeerConnection is closed.";
2275     NoteAddIceCandidateResult(kAddIceCandidateFailClosed);
2276     return false;
2277   }
2278 
2279   if (!remote_description()) {
2280     RTC_LOG(LS_ERROR) << "AddIceCandidate: ICE candidates can't be added "
2281                          "without any remote session description.";
2282     NoteAddIceCandidateResult(kAddIceCandidateFailNoRemoteDescription);
2283     return false;
2284   }
2285 
2286   if (!ice_candidate) {
2287     RTC_LOG(LS_ERROR) << "AddIceCandidate: Candidate is null.";
2288     NoteAddIceCandidateResult(kAddIceCandidateFailNullCandidate);
2289     return false;
2290   }
2291 
2292   bool valid = false;
2293   bool ready = ReadyToUseRemoteCandidate(ice_candidate, nullptr, &valid);
2294   if (!valid) {
2295     NoteAddIceCandidateResult(kAddIceCandidateFailNotValid);
2296     return false;
2297   }
2298 
2299   // Add this candidate to the remote session description.
2300   if (!mutable_remote_description()->AddCandidate(ice_candidate)) {
2301     RTC_LOG(LS_ERROR) << "AddIceCandidate: Candidate cannot be used.";
2302     NoteAddIceCandidateResult(kAddIceCandidateFailInAddition);
2303     return false;
2304   }
2305 
2306   if (ready) {
2307     bool result = UseCandidate(ice_candidate);
2308     if (result) {
2309       pc_->NoteUsageEvent(UsageEvent::ADD_ICE_CANDIDATE_SUCCEEDED);
2310       NoteAddIceCandidateResult(kAddIceCandidateSuccess);
2311     } else {
2312       NoteAddIceCandidateResult(kAddIceCandidateFailNotUsable);
2313     }
2314     return result;
2315   } else {
2316     RTC_LOG(LS_INFO) << "AddIceCandidate: Not ready to use candidate.";
2317     NoteAddIceCandidateResult(kAddIceCandidateFailNotReady);
2318     return true;
2319   }
2320 }
2321 
AddIceCandidate(std::unique_ptr<IceCandidateInterface> candidate,std::function<void (RTCError)> callback)2322 void SdpOfferAnswerHandler::AddIceCandidate(
2323     std::unique_ptr<IceCandidateInterface> candidate,
2324     std::function<void(RTCError)> callback) {
2325   RTC_DCHECK_RUN_ON(signaling_thread());
2326   // Chain this operation. If asynchronous operations are pending on the chain,
2327   // this operation will be queued to be invoked, otherwise the contents of the
2328   // lambda will execute immediately.
2329   operations_chain_->ChainOperation(
2330       [this_weak_ptr = weak_ptr_factory_.GetWeakPtr(),
2331        candidate = std::move(candidate), callback = std::move(callback)](
2332           std::function<void()> operations_chain_callback) {
2333         if (!this_weak_ptr) {
2334           operations_chain_callback();
2335           callback(RTCError(
2336               RTCErrorType::INVALID_STATE,
2337               "AddIceCandidate failed because the session was shut down"));
2338           return;
2339         }
2340         if (!this_weak_ptr->AddIceCandidate(candidate.get())) {
2341           operations_chain_callback();
2342           // Fail with an error type and message consistent with Chromium.
2343           // TODO(hbos): Fail with error types according to spec.
2344           callback(RTCError(RTCErrorType::UNSUPPORTED_OPERATION,
2345                             "Error processing ICE candidate"));
2346           return;
2347         }
2348         operations_chain_callback();
2349         callback(RTCError::OK());
2350       });
2351 }
2352 
RemoveIceCandidates(const std::vector<cricket::Candidate> & candidates)2353 bool SdpOfferAnswerHandler::RemoveIceCandidates(
2354     const std::vector<cricket::Candidate>& candidates) {
2355   TRACE_EVENT0("webrtc", "SdpOfferAnswerHandler::RemoveIceCandidates");
2356   RTC_DCHECK_RUN_ON(signaling_thread());
2357   if (pc_->IsClosed()) {
2358     RTC_LOG(LS_ERROR) << "RemoveIceCandidates: PeerConnection is closed.";
2359     return false;
2360   }
2361 
2362   if (!remote_description()) {
2363     RTC_LOG(LS_ERROR) << "RemoveIceCandidates: ICE candidates can't be removed "
2364                          "without any remote session description.";
2365     return false;
2366   }
2367 
2368   if (candidates.empty()) {
2369     RTC_LOG(LS_ERROR) << "RemoveIceCandidates: candidates are empty.";
2370     return false;
2371   }
2372 
2373   size_t number_removed =
2374       mutable_remote_description()->RemoveCandidates(candidates);
2375   if (number_removed != candidates.size()) {
2376     RTC_LOG(LS_ERROR)
2377         << "RemoveIceCandidates: Failed to remove candidates. Requested "
2378         << candidates.size() << " but only " << number_removed
2379         << " are removed.";
2380   }
2381 
2382   // Remove the candidates from the transport controller.
2383   RTCError error = transport_controller()->RemoveRemoteCandidates(candidates);
2384   if (!error.ok()) {
2385     RTC_LOG(LS_ERROR)
2386         << "RemoveIceCandidates: Error when removing remote candidates: "
2387         << error.message();
2388   }
2389   return true;
2390 }
2391 
AddLocalIceCandidate(const JsepIceCandidate * candidate)2392 void SdpOfferAnswerHandler::AddLocalIceCandidate(
2393     const JsepIceCandidate* candidate) {
2394   RTC_DCHECK_RUN_ON(signaling_thread());
2395   if (local_description()) {
2396     mutable_local_description()->AddCandidate(candidate);
2397   }
2398 }
2399 
RemoveLocalIceCandidates(const std::vector<cricket::Candidate> & candidates)2400 void SdpOfferAnswerHandler::RemoveLocalIceCandidates(
2401     const std::vector<cricket::Candidate>& candidates) {
2402   RTC_DCHECK_RUN_ON(signaling_thread());
2403   if (local_description()) {
2404     mutable_local_description()->RemoveCandidates(candidates);
2405   }
2406 }
2407 
local_description() const2408 const SessionDescriptionInterface* SdpOfferAnswerHandler::local_description()
2409     const {
2410   RTC_DCHECK_RUN_ON(signaling_thread());
2411   return pending_local_description_ ? pending_local_description_.get()
2412                                     : current_local_description_.get();
2413 }
2414 
remote_description() const2415 const SessionDescriptionInterface* SdpOfferAnswerHandler::remote_description()
2416     const {
2417   RTC_DCHECK_RUN_ON(signaling_thread());
2418   return pending_remote_description_ ? pending_remote_description_.get()
2419                                      : current_remote_description_.get();
2420 }
2421 
2422 const SessionDescriptionInterface*
current_local_description() const2423 SdpOfferAnswerHandler::current_local_description() const {
2424   RTC_DCHECK_RUN_ON(signaling_thread());
2425   return current_local_description_.get();
2426 }
2427 
2428 const SessionDescriptionInterface*
current_remote_description() const2429 SdpOfferAnswerHandler::current_remote_description() const {
2430   RTC_DCHECK_RUN_ON(signaling_thread());
2431   return current_remote_description_.get();
2432 }
2433 
2434 const SessionDescriptionInterface*
pending_local_description() const2435 SdpOfferAnswerHandler::pending_local_description() const {
2436   RTC_DCHECK_RUN_ON(signaling_thread());
2437   return pending_local_description_.get();
2438 }
2439 
2440 const SessionDescriptionInterface*
pending_remote_description() const2441 SdpOfferAnswerHandler::pending_remote_description() const {
2442   RTC_DCHECK_RUN_ON(signaling_thread());
2443   return pending_remote_description_.get();
2444 }
2445 
signaling_state() const2446 PeerConnectionInterface::SignalingState SdpOfferAnswerHandler::signaling_state()
2447     const {
2448   RTC_DCHECK_RUN_ON(signaling_thread());
2449   return signaling_state_;
2450 }
2451 
ChangeSignalingState(PeerConnectionInterface::SignalingState signaling_state)2452 void SdpOfferAnswerHandler::ChangeSignalingState(
2453     PeerConnectionInterface::SignalingState signaling_state) {
2454   RTC_DCHECK_RUN_ON(signaling_thread());
2455   if (signaling_state_ == signaling_state) {
2456     return;
2457   }
2458   RTC_LOG(LS_INFO) << "Session: " << pc_->session_id() << " Old state: "
2459                    << GetSignalingStateString(signaling_state_)
2460                    << " New state: "
2461                    << GetSignalingStateString(signaling_state);
2462   signaling_state_ = signaling_state;
2463   pc_->Observer()->OnSignalingChange(signaling_state_);
2464 }
2465 
UpdateSessionState(SdpType type,cricket::ContentSource source,const cricket::SessionDescription * description)2466 RTCError SdpOfferAnswerHandler::UpdateSessionState(
2467     SdpType type,
2468     cricket::ContentSource source,
2469     const cricket::SessionDescription* description) {
2470   RTC_DCHECK_RUN_ON(signaling_thread());
2471 
2472   // If there's already a pending error then no state transition should happen.
2473   // But all call-sites should be verifying this before calling us!
2474   RTC_DCHECK(session_error() == SessionError::kNone);
2475 
2476   // If this is answer-ish we're ready to let media flow.
2477   if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) {
2478     EnableSending();
2479   }
2480 
2481   // Update the signaling state according to the specified state machine (see
2482   // https://w3c.github.io/webrtc-pc/#rtcsignalingstate-enum).
2483   if (type == SdpType::kOffer) {
2484     ChangeSignalingState(source == cricket::CS_LOCAL
2485                              ? PeerConnectionInterface::kHaveLocalOffer
2486                              : PeerConnectionInterface::kHaveRemoteOffer);
2487   } else if (type == SdpType::kPrAnswer) {
2488     ChangeSignalingState(source == cricket::CS_LOCAL
2489                              ? PeerConnectionInterface::kHaveLocalPrAnswer
2490                              : PeerConnectionInterface::kHaveRemotePrAnswer);
2491   } else {
2492     RTC_DCHECK(type == SdpType::kAnswer);
2493     ChangeSignalingState(PeerConnectionInterface::kStable);
2494     transceivers()->DiscardStableStates();
2495     have_pending_rtp_data_channel_ = false;
2496   }
2497 
2498   // Update internal objects according to the session description's media
2499   // descriptions.
2500   RTCError error = PushdownMediaDescription(type, source);
2501   if (!error.ok()) {
2502     return error;
2503   }
2504 
2505   return RTCError::OK();
2506 }
2507 
ShouldFireNegotiationNeededEvent(uint32_t event_id)2508 bool SdpOfferAnswerHandler::ShouldFireNegotiationNeededEvent(
2509     uint32_t event_id) {
2510   RTC_DCHECK_RUN_ON(signaling_thread());
2511   // Plan B? Always fire to conform with useless legacy behavior.
2512   if (!IsUnifiedPlan()) {
2513     return true;
2514   }
2515   // The event ID has been invalidated. Either negotiation is no longer needed
2516   // or a newer negotiation needed event has been generated.
2517   if (event_id != negotiation_needed_event_id_) {
2518     return false;
2519   }
2520   // The chain is no longer empty, update negotiation needed when it becomes
2521   // empty. This should generate a newer negotiation needed event, making this
2522   // one obsolete.
2523   if (!operations_chain_->IsEmpty()) {
2524     // Since we just suppressed an event that would have been fired, if
2525     // negotiation is still needed by the time the chain becomes empty again, we
2526     // must make sure to generate another event if negotiation is needed then.
2527     // This happens when |is_negotiation_needed_| goes from false to true, so we
2528     // set it to false until UpdateNegotiationNeeded() is called.
2529     is_negotiation_needed_ = false;
2530     update_negotiation_needed_on_empty_chain_ = true;
2531     return false;
2532   }
2533   // We must not fire if the signaling state is no longer "stable". If
2534   // negotiation is still needed when we return to "stable", a new negotiation
2535   // needed event will be generated, so this one can safely be suppressed.
2536   if (signaling_state_ != PeerConnectionInterface::kStable) {
2537     return false;
2538   }
2539   // All checks have passed - please fire "negotiationneeded" now!
2540   return true;
2541 }
2542 
2543 rtc::scoped_refptr<StreamCollectionInterface>
local_streams()2544 SdpOfferAnswerHandler::local_streams() {
2545   RTC_DCHECK_RUN_ON(signaling_thread());
2546   RTC_CHECK(!IsUnifiedPlan()) << "local_streams is not available with Unified "
2547                                  "Plan SdpSemantics. Please use GetSenders "
2548                                  "instead.";
2549   return local_streams_;
2550 }
2551 
2552 rtc::scoped_refptr<StreamCollectionInterface>
remote_streams()2553 SdpOfferAnswerHandler::remote_streams() {
2554   RTC_DCHECK_RUN_ON(signaling_thread());
2555   RTC_CHECK(!IsUnifiedPlan()) << "remote_streams is not available with Unified "
2556                                  "Plan SdpSemantics. Please use GetReceivers "
2557                                  "instead.";
2558   return remote_streams_;
2559 }
2560 
AddStream(MediaStreamInterface * local_stream)2561 bool SdpOfferAnswerHandler::AddStream(MediaStreamInterface* local_stream) {
2562   RTC_DCHECK_RUN_ON(signaling_thread());
2563   RTC_CHECK(!IsUnifiedPlan()) << "AddStream is not available with Unified Plan "
2564                                  "SdpSemantics. Please use AddTrack instead.";
2565   if (pc_->IsClosed()) {
2566     return false;
2567   }
2568   if (!CanAddLocalMediaStream(local_streams_, local_stream)) {
2569     return false;
2570   }
2571 
2572   local_streams_->AddStream(local_stream);
2573   MediaStreamObserver* observer = new MediaStreamObserver(local_stream);
2574   observer->SignalAudioTrackAdded.connect(
2575       this, &SdpOfferAnswerHandler::OnAudioTrackAdded);
2576   observer->SignalAudioTrackRemoved.connect(
2577       this, &SdpOfferAnswerHandler::OnAudioTrackRemoved);
2578   observer->SignalVideoTrackAdded.connect(
2579       this, &SdpOfferAnswerHandler::OnVideoTrackAdded);
2580   observer->SignalVideoTrackRemoved.connect(
2581       this, &SdpOfferAnswerHandler::OnVideoTrackRemoved);
2582   stream_observers_.push_back(std::unique_ptr<MediaStreamObserver>(observer));
2583 
2584   for (const auto& track : local_stream->GetAudioTracks()) {
2585     rtp_manager()->AddAudioTrack(track.get(), local_stream);
2586   }
2587   for (const auto& track : local_stream->GetVideoTracks()) {
2588     rtp_manager()->AddVideoTrack(track.get(), local_stream);
2589   }
2590 
2591   pc_->stats()->AddStream(local_stream);
2592   UpdateNegotiationNeeded();
2593   return true;
2594 }
2595 
RemoveStream(MediaStreamInterface * local_stream)2596 void SdpOfferAnswerHandler::RemoveStream(MediaStreamInterface* local_stream) {
2597   RTC_DCHECK_RUN_ON(signaling_thread());
2598   RTC_CHECK(!IsUnifiedPlan()) << "RemoveStream is not available with Unified "
2599                                  "Plan SdpSemantics. Please use RemoveTrack "
2600                                  "instead.";
2601   TRACE_EVENT0("webrtc", "PeerConnection::RemoveStream");
2602   if (!pc_->IsClosed()) {
2603     for (const auto& track : local_stream->GetAudioTracks()) {
2604       rtp_manager()->RemoveAudioTrack(track.get(), local_stream);
2605     }
2606     for (const auto& track : local_stream->GetVideoTracks()) {
2607       rtp_manager()->RemoveVideoTrack(track.get(), local_stream);
2608     }
2609   }
2610   local_streams_->RemoveStream(local_stream);
2611   stream_observers_.erase(
2612       std::remove_if(
2613           stream_observers_.begin(), stream_observers_.end(),
2614           [local_stream](const std::unique_ptr<MediaStreamObserver>& observer) {
2615             return observer->stream()->id().compare(local_stream->id()) == 0;
2616           }),
2617       stream_observers_.end());
2618 
2619   if (pc_->IsClosed()) {
2620     return;
2621   }
2622   UpdateNegotiationNeeded();
2623 }
2624 
OnAudioTrackAdded(AudioTrackInterface * track,MediaStreamInterface * stream)2625 void SdpOfferAnswerHandler::OnAudioTrackAdded(AudioTrackInterface* track,
2626                                               MediaStreamInterface* stream) {
2627   if (pc_->IsClosed()) {
2628     return;
2629   }
2630   rtp_manager()->AddAudioTrack(track, stream);
2631   UpdateNegotiationNeeded();
2632 }
2633 
OnAudioTrackRemoved(AudioTrackInterface * track,MediaStreamInterface * stream)2634 void SdpOfferAnswerHandler::OnAudioTrackRemoved(AudioTrackInterface* track,
2635                                                 MediaStreamInterface* stream) {
2636   if (pc_->IsClosed()) {
2637     return;
2638   }
2639   rtp_manager()->RemoveAudioTrack(track, stream);
2640   UpdateNegotiationNeeded();
2641 }
2642 
OnVideoTrackAdded(VideoTrackInterface * track,MediaStreamInterface * stream)2643 void SdpOfferAnswerHandler::OnVideoTrackAdded(VideoTrackInterface* track,
2644                                               MediaStreamInterface* stream) {
2645   if (pc_->IsClosed()) {
2646     return;
2647   }
2648   rtp_manager()->AddVideoTrack(track, stream);
2649   UpdateNegotiationNeeded();
2650 }
2651 
OnVideoTrackRemoved(VideoTrackInterface * track,MediaStreamInterface * stream)2652 void SdpOfferAnswerHandler::OnVideoTrackRemoved(VideoTrackInterface* track,
2653                                                 MediaStreamInterface* stream) {
2654   if (pc_->IsClosed()) {
2655     return;
2656   }
2657   rtp_manager()->RemoveVideoTrack(track, stream);
2658   UpdateNegotiationNeeded();
2659 }
2660 
Rollback(SdpType desc_type)2661 RTCError SdpOfferAnswerHandler::Rollback(SdpType desc_type) {
2662   auto state = signaling_state();
2663   if (state != PeerConnectionInterface::kHaveLocalOffer &&
2664       state != PeerConnectionInterface::kHaveRemoteOffer) {
2665     return RTCError(RTCErrorType::INVALID_STATE,
2666                     "Called in wrong signalingState: " +
2667                         GetSignalingStateString(signaling_state()));
2668   }
2669   RTC_DCHECK_RUN_ON(signaling_thread());
2670   RTC_DCHECK(IsUnifiedPlan());
2671   std::vector<rtc::scoped_refptr<MediaStreamInterface>> all_added_streams;
2672   std::vector<rtc::scoped_refptr<MediaStreamInterface>> all_removed_streams;
2673   std::vector<rtc::scoped_refptr<RtpReceiverInterface>> removed_receivers;
2674 
2675   for (auto&& transceivers_stable_state_pair : transceivers()->StableStates()) {
2676     auto transceiver = transceivers_stable_state_pair.first;
2677     auto state = transceivers_stable_state_pair.second;
2678 
2679     if (state.remote_stream_ids()) {
2680       std::vector<rtc::scoped_refptr<MediaStreamInterface>> added_streams;
2681       std::vector<rtc::scoped_refptr<MediaStreamInterface>> removed_streams;
2682       SetAssociatedRemoteStreams(transceiver->internal()->receiver_internal(),
2683                                  state.remote_stream_ids().value(),
2684                                  &added_streams, &removed_streams);
2685       all_added_streams.insert(all_added_streams.end(), added_streams.begin(),
2686                                added_streams.end());
2687       all_removed_streams.insert(all_removed_streams.end(),
2688                                  removed_streams.begin(),
2689                                  removed_streams.end());
2690       if (!state.has_m_section() && !state.newly_created()) {
2691         continue;
2692       }
2693     }
2694 
2695     RTC_DCHECK(transceiver->internal()->mid().has_value());
2696     DestroyTransceiverChannel(transceiver);
2697 
2698     if (signaling_state() == PeerConnectionInterface::kHaveRemoteOffer &&
2699         transceiver->receiver()) {
2700       removed_receivers.push_back(transceiver->receiver());
2701     }
2702     if (state.newly_created()) {
2703       if (transceiver->internal()->reused_for_addtrack()) {
2704         transceiver->internal()->set_created_by_addtrack(true);
2705       } else {
2706         transceivers()->Remove(transceiver);
2707       }
2708     }
2709     transceiver->internal()->sender_internal()->set_transport(nullptr);
2710     transceiver->internal()->receiver_internal()->set_transport(nullptr);
2711     transceiver->internal()->set_mid(state.mid());
2712     transceiver->internal()->set_mline_index(state.mline_index());
2713   }
2714   transport_controller()->RollbackTransports();
2715   if (have_pending_rtp_data_channel_) {
2716     DestroyDataChannelTransport();
2717     have_pending_rtp_data_channel_ = false;
2718   }
2719   transceivers()->DiscardStableStates();
2720   pending_local_description_.reset();
2721   pending_remote_description_.reset();
2722   ChangeSignalingState(PeerConnectionInterface::kStable);
2723 
2724   // Once all processing has finished, fire off callbacks.
2725   for (const auto& receiver : removed_receivers) {
2726     pc_->Observer()->OnRemoveTrack(receiver);
2727   }
2728   for (const auto& stream : all_added_streams) {
2729     pc_->Observer()->OnAddStream(stream);
2730   }
2731   for (const auto& stream : all_removed_streams) {
2732     pc_->Observer()->OnRemoveStream(stream);
2733   }
2734 
2735   // The assumption is that in case of implicit rollback UpdateNegotiationNeeded
2736   // gets called in SetRemoteDescription.
2737   if (desc_type == SdpType::kRollback) {
2738     UpdateNegotiationNeeded();
2739     if (is_negotiation_needed_) {
2740       // Legacy version.
2741       pc_->Observer()->OnRenegotiationNeeded();
2742       // Spec-compliant version; the event may get invalidated before firing.
2743       GenerateNegotiationNeededEvent();
2744     }
2745   }
2746   return RTCError::OK();
2747 }
2748 
IsUnifiedPlan() const2749 bool SdpOfferAnswerHandler::IsUnifiedPlan() const {
2750   return pc_->IsUnifiedPlan();
2751 }
2752 
OnOperationsChainEmpty()2753 void SdpOfferAnswerHandler::OnOperationsChainEmpty() {
2754   RTC_DCHECK_RUN_ON(signaling_thread());
2755   if (pc_->IsClosed() || !update_negotiation_needed_on_empty_chain_)
2756     return;
2757   update_negotiation_needed_on_empty_chain_ = false;
2758   // Firing when chain is empty is only supported in Unified Plan to avoid Plan
2759   // B regressions. (In Plan B, onnegotiationneeded is already broken anyway, so
2760   // firing it even more might just be confusing.)
2761   if (IsUnifiedPlan()) {
2762     UpdateNegotiationNeeded();
2763   }
2764 }
2765 
is_caller()2766 absl::optional<bool> SdpOfferAnswerHandler::is_caller() {
2767   RTC_DCHECK_RUN_ON(signaling_thread());
2768   return is_caller_;
2769 }
2770 
HasNewIceCredentials()2771 bool SdpOfferAnswerHandler::HasNewIceCredentials() {
2772   RTC_DCHECK_RUN_ON(signaling_thread());
2773   return local_ice_credentials_to_replace_->HasIceCredentials();
2774 }
2775 
IceRestartPending(const std::string & content_name) const2776 bool SdpOfferAnswerHandler::IceRestartPending(
2777     const std::string& content_name) const {
2778   RTC_DCHECK_RUN_ON(signaling_thread());
2779   return pending_ice_restarts_.find(content_name) !=
2780          pending_ice_restarts_.end();
2781 }
2782 
NeedsIceRestart(const std::string & content_name) const2783 bool SdpOfferAnswerHandler::NeedsIceRestart(
2784     const std::string& content_name) const {
2785   return transport_controller()->NeedsIceRestart(content_name);
2786 }
2787 
GetDtlsRole(const std::string & mid) const2788 absl::optional<rtc::SSLRole> SdpOfferAnswerHandler::GetDtlsRole(
2789     const std::string& mid) const {
2790   return transport_controller()->GetDtlsRole(mid);
2791 }
2792 
UpdateNegotiationNeeded()2793 void SdpOfferAnswerHandler::UpdateNegotiationNeeded() {
2794   RTC_DCHECK_RUN_ON(signaling_thread());
2795   if (!IsUnifiedPlan()) {
2796     pc_->Observer()->OnRenegotiationNeeded();
2797     GenerateNegotiationNeededEvent();
2798     return;
2799   }
2800 
2801   // In the spec, a task is queued here to run the following steps - this is
2802   // meant to ensure we do not fire onnegotiationneeded prematurely if multiple
2803   // changes are being made at once. In order to support Chromium's
2804   // implementation where the JavaScript representation of the PeerConnection
2805   // lives on a separate thread though, the queuing of a task is instead
2806   // performed by the PeerConnectionObserver posting from the signaling thread
2807   // to the JavaScript main thread that negotiation is needed. And because the
2808   // Operations Chain lives on the WebRTC signaling thread,
2809   // ShouldFireNegotiationNeededEvent() must be called before firing the event
2810   // to ensure the Operations Chain is still empty and the event has not been
2811   // invalidated.
2812 
2813   // If connection's [[IsClosed]] slot is true, abort these steps.
2814   if (pc_->IsClosed())
2815     return;
2816 
2817   // If connection's signaling state is not "stable", abort these steps.
2818   if (signaling_state() != PeerConnectionInterface::kStable)
2819     return;
2820 
2821   // NOTE
2822   // The negotiation-needed flag will be updated once the state transitions to
2823   // "stable", as part of the steps for setting an RTCSessionDescription.
2824 
2825   // If the result of checking if negotiation is needed is false, clear the
2826   // negotiation-needed flag by setting connection's [[NegotiationNeeded]] slot
2827   // to false, and abort these steps.
2828   bool is_negotiation_needed = CheckIfNegotiationIsNeeded();
2829   if (!is_negotiation_needed) {
2830     is_negotiation_needed_ = false;
2831     // Invalidate any negotiation needed event that may previosuly have been
2832     // generated.
2833     ++negotiation_needed_event_id_;
2834     return;
2835   }
2836 
2837   // If connection's [[NegotiationNeeded]] slot is already true, abort these
2838   // steps.
2839   if (is_negotiation_needed_)
2840     return;
2841 
2842   // Set connection's [[NegotiationNeeded]] slot to true.
2843   is_negotiation_needed_ = true;
2844 
2845   // Queue a task that runs the following steps:
2846   // If connection's [[IsClosed]] slot is true, abort these steps.
2847   // If connection's [[NegotiationNeeded]] slot is false, abort these steps.
2848   // Fire an event named negotiationneeded at connection.
2849   pc_->Observer()->OnRenegotiationNeeded();
2850   // Fire the spec-compliant version; when ShouldFireNegotiationNeededEvent() is
2851   // used in the task queued by the observer, this event will only fire when the
2852   // chain is empty.
2853   GenerateNegotiationNeededEvent();
2854 }
2855 
CheckIfNegotiationIsNeeded()2856 bool SdpOfferAnswerHandler::CheckIfNegotiationIsNeeded() {
2857   RTC_DCHECK_RUN_ON(signaling_thread());
2858   // 1. If any implementation-specific negotiation is required, as described at
2859   // the start of this section, return true.
2860 
2861   // 2. If connection.[[LocalIceCredentialsToReplace]] is not empty, return
2862   // true.
2863   if (local_ice_credentials_to_replace_->HasIceCredentials()) {
2864     return true;
2865   }
2866 
2867   // 3. Let description be connection.[[CurrentLocalDescription]].
2868   const SessionDescriptionInterface* description = current_local_description();
2869   if (!description)
2870     return true;
2871 
2872   // 4. If connection has created any RTCDataChannels, and no m= section in
2873   // description has been negotiated yet for data, return true.
2874   if (data_channel_controller()->HasSctpDataChannels()) {
2875     if (!cricket::GetFirstDataContent(description->description()->contents()))
2876       return true;
2877   }
2878 
2879   // 5. For each transceiver in connection's set of transceivers, perform the
2880   // following checks:
2881   for (const auto& transceiver : transceivers()->List()) {
2882     const ContentInfo* current_local_msection =
2883         FindTransceiverMSection(transceiver.get(), description);
2884 
2885     const ContentInfo* current_remote_msection = FindTransceiverMSection(
2886         transceiver.get(), current_remote_description());
2887 
2888     // 5.4 If transceiver is stopped and is associated with an m= section,
2889     // but the associated m= section is not yet rejected in
2890     // connection.[[CurrentLocalDescription]] or
2891     // connection.[[CurrentRemoteDescription]], return true.
2892     if (transceiver->stopped()) {
2893       RTC_DCHECK(transceiver->stopping());
2894       if (current_local_msection && !current_local_msection->rejected &&
2895           ((current_remote_msection && !current_remote_msection->rejected) ||
2896            !current_remote_msection)) {
2897         return true;
2898       }
2899       continue;
2900     }
2901 
2902     // 5.1 If transceiver.[[Stopping]] is true and transceiver.[[Stopped]] is
2903     // false, return true.
2904     if (transceiver->stopping() && !transceiver->stopped())
2905       return true;
2906 
2907     // 5.2 If transceiver isn't stopped and isn't yet associated with an m=
2908     // section in description, return true.
2909     if (!current_local_msection)
2910       return true;
2911 
2912     const MediaContentDescription* current_local_media_description =
2913         current_local_msection->media_description();
2914     // 5.3 If transceiver isn't stopped and is associated with an m= section
2915     // in description then perform the following checks:
2916 
2917     // 5.3.1 If transceiver.[[Direction]] is "sendrecv" or "sendonly", and the
2918     // associated m= section in description either doesn't contain a single
2919     // "a=msid" line, or the number of MSIDs from the "a=msid" lines in this
2920     // m= section, or the MSID values themselves, differ from what is in
2921     // transceiver.sender.[[AssociatedMediaStreamIds]], return true.
2922     if (RtpTransceiverDirectionHasSend(transceiver->direction())) {
2923       if (current_local_media_description->streams().size() == 0)
2924         return true;
2925 
2926       std::vector<std::string> msection_msids;
2927       for (const auto& stream : current_local_media_description->streams()) {
2928         for (const std::string& msid : stream.stream_ids())
2929           msection_msids.push_back(msid);
2930       }
2931 
2932       std::vector<std::string> transceiver_msids =
2933           transceiver->sender()->stream_ids();
2934       if (msection_msids.size() != transceiver_msids.size())
2935         return true;
2936 
2937       absl::c_sort(transceiver_msids);
2938       absl::c_sort(msection_msids);
2939       if (transceiver_msids != msection_msids)
2940         return true;
2941     }
2942 
2943     // 5.3.2 If description is of type "offer", and the direction of the
2944     // associated m= section in neither connection.[[CurrentLocalDescription]]
2945     // nor connection.[[CurrentRemoteDescription]] matches
2946     // transceiver.[[Direction]], return true.
2947     if (description->GetType() == SdpType::kOffer) {
2948       if (!current_remote_description())
2949         return true;
2950 
2951       if (!current_remote_msection)
2952         return true;
2953 
2954       RtpTransceiverDirection current_local_direction =
2955           current_local_media_description->direction();
2956       RtpTransceiverDirection current_remote_direction =
2957           current_remote_msection->media_description()->direction();
2958       if (transceiver->direction() != current_local_direction &&
2959           transceiver->direction() !=
2960               RtpTransceiverDirectionReversed(current_remote_direction)) {
2961         return true;
2962       }
2963     }
2964 
2965     // 5.3.3 If description is of type "answer", and the direction of the
2966     // associated m= section in the description does not match
2967     // transceiver.[[Direction]] intersected with the offered direction (as
2968     // described in [JSEP] (section 5.3.1.)), return true.
2969     if (description->GetType() == SdpType::kAnswer) {
2970       if (!remote_description())
2971         return true;
2972 
2973       const ContentInfo* offered_remote_msection =
2974           FindTransceiverMSection(transceiver.get(), remote_description());
2975 
2976       RtpTransceiverDirection offered_direction =
2977           offered_remote_msection
2978               ? offered_remote_msection->media_description()->direction()
2979               : RtpTransceiverDirection::kInactive;
2980 
2981       if (current_local_media_description->direction() !=
2982           (RtpTransceiverDirectionIntersection(
2983               transceiver->direction(),
2984               RtpTransceiverDirectionReversed(offered_direction)))) {
2985         return true;
2986       }
2987     }
2988   }
2989 
2990   // If all the preceding checks were performed and true was not returned,
2991   // nothing remains to be negotiated; return false.
2992   return false;
2993 }
2994 
GenerateNegotiationNeededEvent()2995 void SdpOfferAnswerHandler::GenerateNegotiationNeededEvent() {
2996   RTC_DCHECK_RUN_ON(signaling_thread());
2997   ++negotiation_needed_event_id_;
2998   pc_->Observer()->OnNegotiationNeededEvent(negotiation_needed_event_id_);
2999 }
3000 
ValidateSessionDescription(const SessionDescriptionInterface * sdesc,cricket::ContentSource source)3001 RTCError SdpOfferAnswerHandler::ValidateSessionDescription(
3002     const SessionDescriptionInterface* sdesc,
3003     cricket::ContentSource source) {
3004   if (session_error() != SessionError::kNone) {
3005     LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, GetSessionErrorMsg());
3006   }
3007 
3008   if (!sdesc || !sdesc->description()) {
3009     LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, kInvalidSdp);
3010   }
3011 
3012   SdpType type = sdesc->GetType();
3013   if ((source == cricket::CS_LOCAL && !ExpectSetLocalDescription(type)) ||
3014       (source == cricket::CS_REMOTE && !ExpectSetRemoteDescription(type))) {
3015     LOG_AND_RETURN_ERROR(
3016         RTCErrorType::INVALID_STATE,
3017         "Called in wrong state: " + GetSignalingStateString(signaling_state()));
3018   }
3019 
3020   RTCError error = ValidateMids(*sdesc->description());
3021   if (!error.ok()) {
3022     return error;
3023   }
3024 
3025   // Verify crypto settings.
3026   std::string crypto_error;
3027   if (webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED ||
3028       pc_->dtls_enabled()) {
3029     RTCError crypto_error =
3030         VerifyCrypto(sdesc->description(), pc_->dtls_enabled());
3031     if (!crypto_error.ok()) {
3032       return crypto_error;
3033     }
3034   }
3035 
3036   // Verify ice-ufrag and ice-pwd.
3037   if (!VerifyIceUfragPwdPresent(sdesc->description())) {
3038     LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
3039                          kSdpWithoutIceUfragPwd);
3040   }
3041 
3042   if (!pc_->ValidateBundleSettings(sdesc->description())) {
3043     LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
3044                          kBundleWithoutRtcpMux);
3045   }
3046 
3047   // TODO(skvlad): When the local rtcp-mux policy is Require, reject any
3048   // m-lines that do not rtcp-mux enabled.
3049 
3050   // Verify m-lines in Answer when compared against Offer.
3051   if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) {
3052     // With an answer we want to compare the new answer session description with
3053     // the offer's session description from the current negotiation.
3054     const cricket::SessionDescription* offer_desc =
3055         (source == cricket::CS_LOCAL) ? remote_description()->description()
3056                                       : local_description()->description();
3057     if (!MediaSectionsHaveSameCount(*offer_desc, *sdesc->description()) ||
3058         !MediaSectionsInSameOrder(*offer_desc, nullptr, *sdesc->description(),
3059                                   type)) {
3060       LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
3061                            kMlineMismatchInAnswer);
3062     }
3063   } else {
3064     // The re-offers should respect the order of m= sections in current
3065     // description. See RFC3264 Section 8 paragraph 4 for more details.
3066     // With a re-offer, either the current local or current remote descriptions
3067     // could be the most up to date, so we would like to check against both of
3068     // them if they exist. It could be the case that one of them has a 0 port
3069     // for a media section, but the other does not. This is important to check
3070     // against in the case that we are recycling an m= section.
3071     const cricket::SessionDescription* current_desc = nullptr;
3072     const cricket::SessionDescription* secondary_current_desc = nullptr;
3073     if (local_description()) {
3074       current_desc = local_description()->description();
3075       if (remote_description()) {
3076         secondary_current_desc = remote_description()->description();
3077       }
3078     } else if (remote_description()) {
3079       current_desc = remote_description()->description();
3080     }
3081     if (current_desc &&
3082         !MediaSectionsInSameOrder(*current_desc, secondary_current_desc,
3083                                   *sdesc->description(), type)) {
3084       LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
3085                            kMlineMismatchInSubsequentOffer);
3086     }
3087   }
3088 
3089   if (IsUnifiedPlan()) {
3090     // Ensure that each audio and video media section has at most one
3091     // "StreamParams". This will return an error if receiving a session
3092     // description from a "Plan B" endpoint which adds multiple tracks of the
3093     // same type. With Unified Plan, there can only be at most one track per
3094     // media section.
3095     for (const ContentInfo& content : sdesc->description()->contents()) {
3096       const MediaContentDescription& desc = *content.media_description();
3097       if ((desc.type() == cricket::MEDIA_TYPE_AUDIO ||
3098            desc.type() == cricket::MEDIA_TYPE_VIDEO) &&
3099           desc.streams().size() > 1u) {
3100         LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
3101                              "Media section has more than one track specified "
3102                              "with a=ssrc lines which is not supported with "
3103                              "Unified Plan.");
3104       }
3105     }
3106   }
3107 
3108   return RTCError::OK();
3109 }
3110 
UpdateTransceiversAndDataChannels(cricket::ContentSource source,const SessionDescriptionInterface & new_session,const SessionDescriptionInterface * old_local_description,const SessionDescriptionInterface * old_remote_description)3111 RTCError SdpOfferAnswerHandler::UpdateTransceiversAndDataChannels(
3112     cricket::ContentSource source,
3113     const SessionDescriptionInterface& new_session,
3114     const SessionDescriptionInterface* old_local_description,
3115     const SessionDescriptionInterface* old_remote_description) {
3116   RTC_DCHECK_RUN_ON(signaling_thread());
3117   RTC_DCHECK(IsUnifiedPlan());
3118 
3119   const cricket::ContentGroup* bundle_group = nullptr;
3120   if (new_session.GetType() == SdpType::kOffer) {
3121     auto bundle_group_or_error =
3122         GetEarlyBundleGroup(*new_session.description());
3123     if (!bundle_group_or_error.ok()) {
3124       return bundle_group_or_error.MoveError();
3125     }
3126     bundle_group = bundle_group_or_error.MoveValue();
3127   }
3128 
3129   const ContentInfos& new_contents = new_session.description()->contents();
3130   for (size_t i = 0; i < new_contents.size(); ++i) {
3131     const cricket::ContentInfo& new_content = new_contents[i];
3132     cricket::MediaType media_type = new_content.media_description()->type();
3133     mid_generator_.AddKnownId(new_content.name);
3134     if (media_type == cricket::MEDIA_TYPE_AUDIO ||
3135         media_type == cricket::MEDIA_TYPE_VIDEO) {
3136       const cricket::ContentInfo* old_local_content = nullptr;
3137       if (old_local_description &&
3138           i < old_local_description->description()->contents().size()) {
3139         old_local_content =
3140             &old_local_description->description()->contents()[i];
3141       }
3142       const cricket::ContentInfo* old_remote_content = nullptr;
3143       if (old_remote_description &&
3144           i < old_remote_description->description()->contents().size()) {
3145         old_remote_content =
3146             &old_remote_description->description()->contents()[i];
3147       }
3148       auto transceiver_or_error =
3149           AssociateTransceiver(source, new_session.GetType(), i, new_content,
3150                                old_local_content, old_remote_content);
3151       if (!transceiver_or_error.ok()) {
3152         // In the case where a transceiver is rejected locally, we don't
3153         // expect to find a transceiver, but might find it in the case
3154         // where state is still "stopping", not "stopped".
3155         if (new_content.rejected) {
3156           continue;
3157         }
3158         return transceiver_or_error.MoveError();
3159       }
3160       auto transceiver = transceiver_or_error.MoveValue();
3161       RTCError error =
3162           UpdateTransceiverChannel(transceiver, new_content, bundle_group);
3163       if (!error.ok()) {
3164         return error;
3165       }
3166     } else if (media_type == cricket::MEDIA_TYPE_DATA) {
3167       if (pc_->GetDataMid() && new_content.name != *(pc_->GetDataMid())) {
3168         // Ignore all but the first data section.
3169         RTC_LOG(LS_INFO) << "Ignoring data media section with MID="
3170                          << new_content.name;
3171         continue;
3172       }
3173       RTCError error = UpdateDataChannel(source, new_content, bundle_group);
3174       if (!error.ok()) {
3175         return error;
3176       }
3177     } else if (media_type == cricket::MEDIA_TYPE_UNSUPPORTED) {
3178       RTC_LOG(LS_INFO) << "Ignoring unsupported media type";
3179     } else {
3180       LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR,
3181                            "Unknown section type.");
3182     }
3183   }
3184 
3185   return RTCError::OK();
3186 }
3187 
3188 RTCErrorOr<rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>>
AssociateTransceiver(cricket::ContentSource source,SdpType type,size_t mline_index,const ContentInfo & content,const ContentInfo * old_local_content,const ContentInfo * old_remote_content)3189 SdpOfferAnswerHandler::AssociateTransceiver(
3190     cricket::ContentSource source,
3191     SdpType type,
3192     size_t mline_index,
3193     const ContentInfo& content,
3194     const ContentInfo* old_local_content,
3195     const ContentInfo* old_remote_content) {
3196   RTC_DCHECK(IsUnifiedPlan());
3197 #if RTC_DCHECK_IS_ON
3198   // If this is an offer then the m= section might be recycled. If the m=
3199   // section is being recycled (defined as: rejected in the current local or
3200   // remote description and not rejected in new description), the transceiver
3201   // should have been removed by RemoveStoppedtransceivers()->
3202   if (IsMediaSectionBeingRecycled(type, content, old_local_content,
3203                                   old_remote_content)) {
3204     const std::string& old_mid =
3205         (old_local_content && old_local_content->rejected)
3206             ? old_local_content->name
3207             : old_remote_content->name;
3208     auto old_transceiver = transceivers()->FindByMid(old_mid);
3209     // The transceiver should be disassociated in RemoveStoppedTransceivers()
3210     RTC_DCHECK(!old_transceiver);
3211   }
3212 #endif
3213 
3214   const MediaContentDescription* media_desc = content.media_description();
3215   auto transceiver = transceivers()->FindByMid(content.name);
3216   if (source == cricket::CS_LOCAL) {
3217     // Find the RtpTransceiver that corresponds to this m= section, using the
3218     // mapping between transceivers and m= section indices established when
3219     // creating the offer.
3220     if (!transceiver) {
3221       transceiver = transceivers()->FindByMLineIndex(mline_index);
3222     }
3223     if (!transceiver) {
3224       // This may happen normally when media sections are rejected.
3225       LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
3226                            "Transceiver not found based on m-line index");
3227     }
3228   } else {
3229     RTC_DCHECK_EQ(source, cricket::CS_REMOTE);
3230     // If the m= section is sendrecv or recvonly, and there are RtpTransceivers
3231     // of the same type...
3232     // When simulcast is requested, a transceiver cannot be associated because
3233     // AddTrack cannot be called to initialize it.
3234     if (!transceiver &&
3235         RtpTransceiverDirectionHasRecv(media_desc->direction()) &&
3236         !media_desc->HasSimulcast()) {
3237       transceiver = FindAvailableTransceiverToReceive(media_desc->type());
3238     }
3239     // If no RtpTransceiver was found in the previous step, create one with a
3240     // recvonly direction.
3241     if (!transceiver) {
3242       RTC_LOG(LS_INFO) << "Adding "
3243                        << cricket::MediaTypeToString(media_desc->type())
3244                        << " transceiver for MID=" << content.name
3245                        << " at i=" << mline_index
3246                        << " in response to the remote description.";
3247       std::string sender_id = rtc::CreateRandomUuid();
3248       std::vector<RtpEncodingParameters> send_encodings =
3249           GetSendEncodingsFromRemoteDescription(*media_desc);
3250       auto sender = rtp_manager()->CreateSender(media_desc->type(), sender_id,
3251                                                 nullptr, {}, send_encodings);
3252       std::string receiver_id;
3253       if (!media_desc->streams().empty()) {
3254         receiver_id = media_desc->streams()[0].id;
3255       } else {
3256         receiver_id = rtc::CreateRandomUuid();
3257       }
3258       auto receiver =
3259           rtp_manager()->CreateReceiver(media_desc->type(), receiver_id);
3260       transceiver = rtp_manager()->CreateAndAddTransceiver(sender, receiver);
3261       transceiver->internal()->set_direction(
3262           RtpTransceiverDirection::kRecvOnly);
3263       if (type == SdpType::kOffer) {
3264         transceivers()->StableState(transceiver)->set_newly_created();
3265       }
3266     }
3267 
3268     RTC_DCHECK(transceiver);
3269 
3270     // Check if the offer indicated simulcast but the answer rejected it.
3271     // This can happen when simulcast is not supported on the remote party.
3272     if (SimulcastIsRejected(old_local_content, *media_desc)) {
3273       RTC_HISTOGRAM_BOOLEAN(kSimulcastDisabled, true);
3274       RTCError error =
3275           DisableSimulcastInSender(transceiver->internal()->sender_internal());
3276       if (!error.ok()) {
3277         RTC_LOG(LS_ERROR) << "Failed to remove rejected simulcast.";
3278         return std::move(error);
3279       }
3280     }
3281   }
3282 
3283   if (transceiver->media_type() != media_desc->type()) {
3284     LOG_AND_RETURN_ERROR(
3285         RTCErrorType::INVALID_PARAMETER,
3286         "Transceiver type does not match media description type.");
3287   }
3288 
3289   if (media_desc->HasSimulcast()) {
3290     std::vector<SimulcastLayer> layers =
3291         source == cricket::CS_LOCAL
3292             ? media_desc->simulcast_description().send_layers().GetAllLayers()
3293             : media_desc->simulcast_description()
3294                   .receive_layers()
3295                   .GetAllLayers();
3296     RTCError error = UpdateSimulcastLayerStatusInSender(
3297         layers, transceiver->internal()->sender_internal());
3298     if (!error.ok()) {
3299       RTC_LOG(LS_ERROR) << "Failed updating status for simulcast layers.";
3300       return std::move(error);
3301     }
3302   }
3303   if (type == SdpType::kOffer) {
3304     bool state_changes = transceiver->internal()->mid() != content.name ||
3305                          transceiver->internal()->mline_index() != mline_index;
3306     if (state_changes) {
3307       transceivers()
3308           ->StableState(transceiver)
3309           ->SetMSectionIfUnset(transceiver->internal()->mid(),
3310                                transceiver->internal()->mline_index());
3311     }
3312   }
3313   // Associate the found or created RtpTransceiver with the m= section by
3314   // setting the value of the RtpTransceiver's mid property to the MID of the m=
3315   // section, and establish a mapping between the transceiver and the index of
3316   // the m= section.
3317   transceiver->internal()->set_mid(content.name);
3318   transceiver->internal()->set_mline_index(mline_index);
3319   return std::move(transceiver);
3320 }
3321 
3322 RTCErrorOr<const cricket::ContentGroup*>
GetEarlyBundleGroup(const SessionDescription & desc) const3323 SdpOfferAnswerHandler::GetEarlyBundleGroup(
3324     const SessionDescription& desc) const {
3325   const cricket::ContentGroup* bundle_group = nullptr;
3326   if (pc_->configuration()->bundle_policy ==
3327       PeerConnectionInterface::kBundlePolicyMaxBundle) {
3328     bundle_group = desc.GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
3329     if (!bundle_group) {
3330       LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
3331                            "max-bundle configured but session description "
3332                            "has no BUNDLE group");
3333     }
3334   }
3335   return bundle_group;
3336 }
3337 
UpdateTransceiverChannel(rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> transceiver,const cricket::ContentInfo & content,const cricket::ContentGroup * bundle_group)3338 RTCError SdpOfferAnswerHandler::UpdateTransceiverChannel(
3339     rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
3340         transceiver,
3341     const cricket::ContentInfo& content,
3342     const cricket::ContentGroup* bundle_group) {
3343   RTC_DCHECK(IsUnifiedPlan());
3344   RTC_DCHECK(transceiver);
3345   cricket::ChannelInterface* channel = transceiver->internal()->channel();
3346   if (content.rejected) {
3347     if (channel) {
3348       transceiver->internal()->SetChannel(nullptr);
3349       DestroyChannelInterface(channel);
3350     }
3351   } else {
3352     if (!channel) {
3353       if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) {
3354         channel = CreateVoiceChannel(content.name);
3355       } else {
3356         RTC_DCHECK_EQ(cricket::MEDIA_TYPE_VIDEO, transceiver->media_type());
3357         channel = CreateVideoChannel(content.name);
3358       }
3359       if (!channel) {
3360         LOG_AND_RETURN_ERROR(
3361             RTCErrorType::INTERNAL_ERROR,
3362             "Failed to create channel for mid=" + content.name);
3363       }
3364       transceiver->internal()->SetChannel(channel);
3365     }
3366   }
3367   return RTCError::OK();
3368 }
3369 
UpdateDataChannel(cricket::ContentSource source,const cricket::ContentInfo & content,const cricket::ContentGroup * bundle_group)3370 RTCError SdpOfferAnswerHandler::UpdateDataChannel(
3371     cricket::ContentSource source,
3372     const cricket::ContentInfo& content,
3373     const cricket::ContentGroup* bundle_group) {
3374   if (pc_->data_channel_type() == cricket::DCT_NONE) {
3375     // If data channels are disabled, ignore this media section. CreateAnswer
3376     // will take care of rejecting it.
3377     return RTCError::OK();
3378   }
3379   if (content.rejected) {
3380     RTC_LOG(LS_INFO) << "Rejected data channel, mid=" << content.mid();
3381     DestroyDataChannelTransport();
3382   } else {
3383     if (!data_channel_controller()->rtp_data_channel() &&
3384         !data_channel_controller()->data_channel_transport()) {
3385       RTC_LOG(LS_INFO) << "Creating data channel, mid=" << content.mid();
3386       if (!CreateDataChannel(content.name)) {
3387         LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR,
3388                              "Failed to create data channel.");
3389       }
3390     }
3391     if (source == cricket::CS_REMOTE) {
3392       const MediaContentDescription* data_desc = content.media_description();
3393       if (data_desc && cricket::IsRtpProtocol(data_desc->protocol())) {
3394         data_channel_controller()->UpdateRemoteRtpDataChannels(
3395             GetActiveStreams(data_desc));
3396       }
3397     }
3398   }
3399   return RTCError::OK();
3400 }
3401 
ExpectSetLocalDescription(SdpType type)3402 bool SdpOfferAnswerHandler::ExpectSetLocalDescription(SdpType type) {
3403   PeerConnectionInterface::SignalingState state = signaling_state();
3404   if (type == SdpType::kOffer) {
3405     return (state == PeerConnectionInterface::kStable) ||
3406            (state == PeerConnectionInterface::kHaveLocalOffer);
3407   } else {
3408     RTC_DCHECK(type == SdpType::kPrAnswer || type == SdpType::kAnswer);
3409     return (state == PeerConnectionInterface::kHaveRemoteOffer) ||
3410            (state == PeerConnectionInterface::kHaveLocalPrAnswer);
3411   }
3412 }
3413 
ExpectSetRemoteDescription(SdpType type)3414 bool SdpOfferAnswerHandler::ExpectSetRemoteDescription(SdpType type) {
3415   PeerConnectionInterface::SignalingState state = signaling_state();
3416   if (type == SdpType::kOffer) {
3417     return (state == PeerConnectionInterface::kStable) ||
3418            (state == PeerConnectionInterface::kHaveRemoteOffer);
3419   } else {
3420     RTC_DCHECK(type == SdpType::kPrAnswer || type == SdpType::kAnswer);
3421     return (state == PeerConnectionInterface::kHaveLocalOffer) ||
3422            (state == PeerConnectionInterface::kHaveRemotePrAnswer);
3423   }
3424 }
3425 
FillInMissingRemoteMids(cricket::SessionDescription * new_remote_description)3426 void SdpOfferAnswerHandler::FillInMissingRemoteMids(
3427     cricket::SessionDescription* new_remote_description) {
3428   RTC_DCHECK_RUN_ON(signaling_thread());
3429   RTC_DCHECK(new_remote_description);
3430   const cricket::ContentInfos no_infos;
3431   const cricket::ContentInfos& local_contents =
3432       (local_description() ? local_description()->description()->contents()
3433                            : no_infos);
3434   const cricket::ContentInfos& remote_contents =
3435       (remote_description() ? remote_description()->description()->contents()
3436                             : no_infos);
3437   for (size_t i = 0; i < new_remote_description->contents().size(); ++i) {
3438     cricket::ContentInfo& content = new_remote_description->contents()[i];
3439     if (!content.name.empty()) {
3440       continue;
3441     }
3442     std::string new_mid;
3443     absl::string_view source_explanation;
3444     if (IsUnifiedPlan()) {
3445       if (i < local_contents.size()) {
3446         new_mid = local_contents[i].name;
3447         source_explanation = "from the matching local media section";
3448       } else if (i < remote_contents.size()) {
3449         new_mid = remote_contents[i].name;
3450         source_explanation = "from the matching previous remote media section";
3451       } else {
3452         new_mid = mid_generator_.GenerateString();
3453         source_explanation = "generated just now";
3454       }
3455     } else {
3456       new_mid = std::string(
3457           GetDefaultMidForPlanB(content.media_description()->type()));
3458       source_explanation = "to match pre-existing behavior";
3459     }
3460     RTC_DCHECK(!new_mid.empty());
3461     content.name = new_mid;
3462     new_remote_description->transport_infos()[i].content_name = new_mid;
3463     RTC_LOG(LS_INFO) << "SetRemoteDescription: Remote media section at i=" << i
3464                      << " is missing an a=mid line. Filling in the value '"
3465                      << new_mid << "' " << source_explanation << ".";
3466   }
3467 }
3468 
3469 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
FindAvailableTransceiverToReceive(cricket::MediaType media_type) const3470 SdpOfferAnswerHandler::FindAvailableTransceiverToReceive(
3471     cricket::MediaType media_type) const {
3472   RTC_DCHECK_RUN_ON(signaling_thread());
3473   RTC_DCHECK(IsUnifiedPlan());
3474   // From JSEP section 5.10 (Applying a Remote Description):
3475   // If the m= section is sendrecv or recvonly, and there are RtpTransceivers of
3476   // the same type that were added to the PeerConnection by addTrack and are not
3477   // associated with any m= section and are not stopped, find the first such
3478   // RtpTransceiver.
3479   for (auto transceiver : transceivers()->List()) {
3480     if (transceiver->media_type() == media_type &&
3481         transceiver->internal()->created_by_addtrack() && !transceiver->mid() &&
3482         !transceiver->stopped()) {
3483       return transceiver;
3484     }
3485   }
3486   return nullptr;
3487 }
3488 
3489 const cricket::ContentInfo*
FindMediaSectionForTransceiver(rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> transceiver,const SessionDescriptionInterface * sdesc) const3490 SdpOfferAnswerHandler::FindMediaSectionForTransceiver(
3491     rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
3492         transceiver,
3493     const SessionDescriptionInterface* sdesc) const {
3494   RTC_DCHECK_RUN_ON(signaling_thread());
3495   RTC_DCHECK(transceiver);
3496   RTC_DCHECK(sdesc);
3497   if (IsUnifiedPlan()) {
3498     if (!transceiver->internal()->mid()) {
3499       // This transceiver is not associated with a media section yet.
3500       return nullptr;
3501     }
3502     return sdesc->description()->GetContentByName(
3503         *transceiver->internal()->mid());
3504   } else {
3505     // Plan B only allows at most one audio and one video section, so use the
3506     // first media section of that type.
3507     return cricket::GetFirstMediaContent(sdesc->description()->contents(),
3508                                          transceiver->media_type());
3509   }
3510 }
3511 
GetOptionsForOffer(const PeerConnectionInterface::RTCOfferAnswerOptions & offer_answer_options,cricket::MediaSessionOptions * session_options)3512 void SdpOfferAnswerHandler::GetOptionsForOffer(
3513     const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options,
3514     cricket::MediaSessionOptions* session_options) {
3515   RTC_DCHECK_RUN_ON(signaling_thread());
3516   ExtractSharedMediaSessionOptions(offer_answer_options, session_options);
3517 
3518   if (IsUnifiedPlan()) {
3519     GetOptionsForUnifiedPlanOffer(offer_answer_options, session_options);
3520   } else {
3521     GetOptionsForPlanBOffer(offer_answer_options, session_options);
3522   }
3523 
3524   // Intentionally unset the data channel type for RTP data channel with the
3525   // second condition. Otherwise the RTP data channels would be successfully
3526   // negotiated by default and the unit tests in WebRtcDataBrowserTest will fail
3527   // when building with chromium. We want to leave RTP data channels broken, so
3528   // people won't try to use them.
3529   if (data_channel_controller()->HasRtpDataChannels() ||
3530       pc_->data_channel_type() != cricket::DCT_RTP) {
3531     session_options->data_channel_type = pc_->data_channel_type();
3532   }
3533 
3534   // Apply ICE restart flag and renomination flag.
3535   bool ice_restart = offer_answer_options.ice_restart || HasNewIceCredentials();
3536   for (auto& options : session_options->media_description_options) {
3537     options.transport_options.ice_restart = ice_restart;
3538     options.transport_options.enable_ice_renomination =
3539         pc_->configuration()->enable_ice_renomination;
3540   }
3541 
3542   session_options->rtcp_cname = rtcp_cname_;
3543   session_options->crypto_options = pc_->GetCryptoOptions();
3544   session_options->pooled_ice_credentials =
3545       pc_->network_thread()->Invoke<std::vector<cricket::IceParameters>>(
3546           RTC_FROM_HERE,
3547           rtc::Bind(&cricket::PortAllocator::GetPooledIceCredentials,
3548                     port_allocator()));
3549   session_options->offer_extmap_allow_mixed =
3550       pc_->configuration()->offer_extmap_allow_mixed;
3551 
3552   // Allow fallback for using obsolete SCTP syntax.
3553   // Note that the default in |session_options| is true, while
3554   // the default in |options| is false.
3555   session_options->use_obsolete_sctp_sdp =
3556       offer_answer_options.use_obsolete_sctp_sdp;
3557 }
3558 
GetOptionsForPlanBOffer(const PeerConnectionInterface::RTCOfferAnswerOptions & offer_answer_options,cricket::MediaSessionOptions * session_options)3559 void SdpOfferAnswerHandler::GetOptionsForPlanBOffer(
3560     const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options,
3561     cricket::MediaSessionOptions* session_options) {
3562   // Figure out transceiver directional preferences.
3563   bool send_audio =
3564       !rtp_manager()->GetAudioTransceiver()->internal()->senders().empty();
3565   bool send_video =
3566       !rtp_manager()->GetVideoTransceiver()->internal()->senders().empty();
3567 
3568   // By default, generate sendrecv/recvonly m= sections.
3569   bool recv_audio = true;
3570   bool recv_video = true;
3571 
3572   // By default, only offer a new m= section if we have media to send with it.
3573   bool offer_new_audio_description = send_audio;
3574   bool offer_new_video_description = send_video;
3575   bool offer_new_data_description =
3576       data_channel_controller()->HasDataChannels();
3577 
3578   // The "offer_to_receive_X" options allow those defaults to be overridden.
3579   if (offer_answer_options.offer_to_receive_audio !=
3580       PeerConnectionInterface::RTCOfferAnswerOptions::kUndefined) {
3581     recv_audio = (offer_answer_options.offer_to_receive_audio > 0);
3582     offer_new_audio_description =
3583         offer_new_audio_description ||
3584         (offer_answer_options.offer_to_receive_audio > 0);
3585   }
3586   if (offer_answer_options.offer_to_receive_video !=
3587       RTCOfferAnswerOptions::kUndefined) {
3588     recv_video = (offer_answer_options.offer_to_receive_video > 0);
3589     offer_new_video_description =
3590         offer_new_video_description ||
3591         (offer_answer_options.offer_to_receive_video > 0);
3592   }
3593 
3594   absl::optional<size_t> audio_index;
3595   absl::optional<size_t> video_index;
3596   absl::optional<size_t> data_index;
3597   // If a current description exists, generate m= sections in the same order,
3598   // using the first audio/video/data section that appears and rejecting
3599   // extraneous ones.
3600   if (local_description()) {
3601     GenerateMediaDescriptionOptions(
3602         local_description(),
3603         RtpTransceiverDirectionFromSendRecv(send_audio, recv_audio),
3604         RtpTransceiverDirectionFromSendRecv(send_video, recv_video),
3605         &audio_index, &video_index, &data_index, session_options);
3606   }
3607 
3608   // Add audio/video/data m= sections to the end if needed.
3609   if (!audio_index && offer_new_audio_description) {
3610     cricket::MediaDescriptionOptions options(
3611         cricket::MEDIA_TYPE_AUDIO, cricket::CN_AUDIO,
3612         RtpTransceiverDirectionFromSendRecv(send_audio, recv_audio), false);
3613     options.header_extensions =
3614         channel_manager()->GetSupportedAudioRtpHeaderExtensions();
3615     session_options->media_description_options.push_back(options);
3616     audio_index = session_options->media_description_options.size() - 1;
3617   }
3618   if (!video_index && offer_new_video_description) {
3619     cricket::MediaDescriptionOptions options(
3620         cricket::MEDIA_TYPE_VIDEO, cricket::CN_VIDEO,
3621         RtpTransceiverDirectionFromSendRecv(send_video, recv_video), false);
3622     options.header_extensions =
3623         channel_manager()->GetSupportedVideoRtpHeaderExtensions();
3624     session_options->media_description_options.push_back(options);
3625     video_index = session_options->media_description_options.size() - 1;
3626   }
3627   if (!data_index && offer_new_data_description) {
3628     session_options->media_description_options.push_back(
3629         GetMediaDescriptionOptionsForActiveData(cricket::CN_DATA));
3630     data_index = session_options->media_description_options.size() - 1;
3631   }
3632 
3633   cricket::MediaDescriptionOptions* audio_media_description_options =
3634       !audio_index ? nullptr
3635                    : &session_options->media_description_options[*audio_index];
3636   cricket::MediaDescriptionOptions* video_media_description_options =
3637       !video_index ? nullptr
3638                    : &session_options->media_description_options[*video_index];
3639 
3640   AddPlanBRtpSenderOptions(rtp_manager()->GetSendersInternal(),
3641                            audio_media_description_options,
3642                            video_media_description_options,
3643                            offer_answer_options.num_simulcast_layers);
3644 }
3645 
GetOptionsForUnifiedPlanOffer(const RTCOfferAnswerOptions & offer_answer_options,cricket::MediaSessionOptions * session_options)3646 void SdpOfferAnswerHandler::GetOptionsForUnifiedPlanOffer(
3647     const RTCOfferAnswerOptions& offer_answer_options,
3648     cricket::MediaSessionOptions* session_options) {
3649   // Rules for generating an offer are dictated by JSEP sections 5.2.1 (Initial
3650   // Offers) and 5.2.2 (Subsequent Offers).
3651   RTC_DCHECK_EQ(session_options->media_description_options.size(), 0);
3652   const ContentInfos no_infos;
3653   const ContentInfos& local_contents =
3654       (local_description() ? local_description()->description()->contents()
3655                            : no_infos);
3656   const ContentInfos& remote_contents =
3657       (remote_description() ? remote_description()->description()->contents()
3658                             : no_infos);
3659   // The mline indices that can be recycled. New transceivers should reuse these
3660   // slots first.
3661   std::queue<size_t> recycleable_mline_indices;
3662   // First, go through each media section that exists in either the local or
3663   // remote description and generate a media section in this offer for the
3664   // associated transceiver. If a media section can be recycled, generate a
3665   // default, rejected media section here that can be later overwritten.
3666   for (size_t i = 0;
3667        i < std::max(local_contents.size(), remote_contents.size()); ++i) {
3668     // Either |local_content| or |remote_content| is non-null.
3669     const ContentInfo* local_content =
3670         (i < local_contents.size() ? &local_contents[i] : nullptr);
3671     const ContentInfo* current_local_content =
3672         GetContentByIndex(current_local_description(), i);
3673     const ContentInfo* remote_content =
3674         (i < remote_contents.size() ? &remote_contents[i] : nullptr);
3675     const ContentInfo* current_remote_content =
3676         GetContentByIndex(current_remote_description(), i);
3677     bool had_been_rejected =
3678         (current_local_content && current_local_content->rejected) ||
3679         (current_remote_content && current_remote_content->rejected);
3680     const std::string& mid =
3681         (local_content ? local_content->name : remote_content->name);
3682     cricket::MediaType media_type =
3683         (local_content ? local_content->media_description()->type()
3684                        : remote_content->media_description()->type());
3685     if (media_type == cricket::MEDIA_TYPE_AUDIO ||
3686         media_type == cricket::MEDIA_TYPE_VIDEO) {
3687       // A media section is considered eligible for recycling if it is marked as
3688       // rejected in either the current local or current remote description.
3689       auto transceiver = transceivers()->FindByMid(mid);
3690       if (!transceiver) {
3691         // No associated transceiver. The media section has been stopped.
3692         recycleable_mline_indices.push(i);
3693         session_options->media_description_options.push_back(
3694             cricket::MediaDescriptionOptions(media_type, mid,
3695                                              RtpTransceiverDirection::kInactive,
3696                                              /*stopped=*/true));
3697       } else {
3698         // NOTE: a stopping transceiver should be treated as a stopped one in
3699         // createOffer as specified in
3700         // https://w3c.github.io/webrtc-pc/#dom-rtcpeerconnection-createoffer.
3701         if (had_been_rejected && transceiver->stopping()) {
3702           session_options->media_description_options.push_back(
3703               cricket::MediaDescriptionOptions(
3704                   transceiver->media_type(), mid,
3705                   RtpTransceiverDirection::kInactive,
3706                   /*stopped=*/true));
3707           recycleable_mline_indices.push(i);
3708         } else {
3709           session_options->media_description_options.push_back(
3710               GetMediaDescriptionOptionsForTransceiver(
3711                   transceiver, mid,
3712                   /*is_create_offer=*/true));
3713           // CreateOffer shouldn't really cause any state changes in
3714           // PeerConnection, but we need a way to match new transceivers to new
3715           // media sections in SetLocalDescription and JSEP specifies this is
3716           // done by recording the index of the media section generated for the
3717           // transceiver in the offer.
3718           transceiver->internal()->set_mline_index(i);
3719         }
3720       }
3721     } else if (media_type == cricket::MEDIA_TYPE_UNSUPPORTED) {
3722       RTC_DCHECK(local_content->rejected);
3723       session_options->media_description_options.push_back(
3724           cricket::MediaDescriptionOptions(media_type, mid,
3725                                            RtpTransceiverDirection::kInactive,
3726                                            /*stopped=*/true));
3727     } else {
3728       RTC_CHECK_EQ(cricket::MEDIA_TYPE_DATA, media_type);
3729       if (had_been_rejected) {
3730         session_options->media_description_options.push_back(
3731             GetMediaDescriptionOptionsForRejectedData(mid));
3732       } else {
3733         RTC_CHECK(pc_->GetDataMid());
3734         if (mid == *(pc_->GetDataMid())) {
3735           session_options->media_description_options.push_back(
3736               GetMediaDescriptionOptionsForActiveData(mid));
3737         } else {
3738           session_options->media_description_options.push_back(
3739               GetMediaDescriptionOptionsForRejectedData(mid));
3740         }
3741       }
3742     }
3743   }
3744 
3745   // Next, look for transceivers that are newly added (that is, are not stopped
3746   // and not associated). Reuse media sections marked as recyclable first,
3747   // otherwise append to the end of the offer. New media sections should be
3748   // added in the order they were added to the PeerConnection.
3749   for (const auto& transceiver : transceivers()->List()) {
3750     if (transceiver->mid() || transceiver->stopping()) {
3751       continue;
3752     }
3753     size_t mline_index;
3754     if (!recycleable_mline_indices.empty()) {
3755       mline_index = recycleable_mline_indices.front();
3756       recycleable_mline_indices.pop();
3757       session_options->media_description_options[mline_index] =
3758           GetMediaDescriptionOptionsForTransceiver(
3759               transceiver, mid_generator_.GenerateString(),
3760               /*is_create_offer=*/true);
3761     } else {
3762       mline_index = session_options->media_description_options.size();
3763       session_options->media_description_options.push_back(
3764           GetMediaDescriptionOptionsForTransceiver(
3765               transceiver, mid_generator_.GenerateString(),
3766               /*is_create_offer=*/true));
3767     }
3768     // See comment above for why CreateOffer changes the transceiver's state.
3769     transceiver->internal()->set_mline_index(mline_index);
3770   }
3771   // Lastly, add a m-section if we have local data channels and an m section
3772   // does not already exist.
3773   if (!pc_->GetDataMid() && data_channel_controller()->HasDataChannels()) {
3774     session_options->media_description_options.push_back(
3775         GetMediaDescriptionOptionsForActiveData(
3776             mid_generator_.GenerateString()));
3777   }
3778 }
3779 
GetOptionsForAnswer(const RTCOfferAnswerOptions & offer_answer_options,cricket::MediaSessionOptions * session_options)3780 void SdpOfferAnswerHandler::GetOptionsForAnswer(
3781     const RTCOfferAnswerOptions& offer_answer_options,
3782     cricket::MediaSessionOptions* session_options) {
3783   RTC_DCHECK_RUN_ON(signaling_thread());
3784   ExtractSharedMediaSessionOptions(offer_answer_options, session_options);
3785 
3786   if (IsUnifiedPlan()) {
3787     GetOptionsForUnifiedPlanAnswer(offer_answer_options, session_options);
3788   } else {
3789     GetOptionsForPlanBAnswer(offer_answer_options, session_options);
3790   }
3791 
3792   // Intentionally unset the data channel type for RTP data channel. Otherwise
3793   // the RTP data channels would be successfully negotiated by default and the
3794   // unit tests in WebRtcDataBrowserTest will fail when building with chromium.
3795   // We want to leave RTP data channels broken, so people won't try to use them.
3796   if (data_channel_controller()->HasRtpDataChannels() ||
3797       pc_->data_channel_type() != cricket::DCT_RTP) {
3798     session_options->data_channel_type = pc_->data_channel_type();
3799   }
3800 
3801   // Apply ICE renomination flag.
3802   for (auto& options : session_options->media_description_options) {
3803     options.transport_options.enable_ice_renomination =
3804         pc_->configuration()->enable_ice_renomination;
3805   }
3806 
3807   session_options->rtcp_cname = rtcp_cname_;
3808   session_options->crypto_options = pc_->GetCryptoOptions();
3809   session_options->pooled_ice_credentials =
3810       pc_->network_thread()->Invoke<std::vector<cricket::IceParameters>>(
3811           RTC_FROM_HERE,
3812           rtc::Bind(&cricket::PortAllocator::GetPooledIceCredentials,
3813                     port_allocator()));
3814 }
3815 
GetOptionsForPlanBAnswer(const PeerConnectionInterface::RTCOfferAnswerOptions & offer_answer_options,cricket::MediaSessionOptions * session_options)3816 void SdpOfferAnswerHandler::GetOptionsForPlanBAnswer(
3817     const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options,
3818     cricket::MediaSessionOptions* session_options) {
3819   // Figure out transceiver directional preferences.
3820   bool send_audio =
3821       !rtp_manager()->GetAudioTransceiver()->internal()->senders().empty();
3822   bool send_video =
3823       !rtp_manager()->GetVideoTransceiver()->internal()->senders().empty();
3824 
3825   // By default, generate sendrecv/recvonly m= sections. The direction is also
3826   // restricted by the direction in the offer.
3827   bool recv_audio = true;
3828   bool recv_video = true;
3829 
3830   // The "offer_to_receive_X" options allow those defaults to be overridden.
3831   if (offer_answer_options.offer_to_receive_audio !=
3832       RTCOfferAnswerOptions::kUndefined) {
3833     recv_audio = (offer_answer_options.offer_to_receive_audio > 0);
3834   }
3835   if (offer_answer_options.offer_to_receive_video !=
3836       RTCOfferAnswerOptions::kUndefined) {
3837     recv_video = (offer_answer_options.offer_to_receive_video > 0);
3838   }
3839 
3840   absl::optional<size_t> audio_index;
3841   absl::optional<size_t> video_index;
3842   absl::optional<size_t> data_index;
3843 
3844   // Generate m= sections that match those in the offer.
3845   // Note that mediasession.cc will handle intersection our preferred
3846   // direction with the offered direction.
3847   GenerateMediaDescriptionOptions(
3848       remote_description(),
3849       RtpTransceiverDirectionFromSendRecv(send_audio, recv_audio),
3850       RtpTransceiverDirectionFromSendRecv(send_video, recv_video), &audio_index,
3851       &video_index, &data_index, session_options);
3852 
3853   cricket::MediaDescriptionOptions* audio_media_description_options =
3854       !audio_index ? nullptr
3855                    : &session_options->media_description_options[*audio_index];
3856   cricket::MediaDescriptionOptions* video_media_description_options =
3857       !video_index ? nullptr
3858                    : &session_options->media_description_options[*video_index];
3859 
3860   AddPlanBRtpSenderOptions(rtp_manager()->GetSendersInternal(),
3861                            audio_media_description_options,
3862                            video_media_description_options,
3863                            offer_answer_options.num_simulcast_layers);
3864 }
3865 
GetOptionsForUnifiedPlanAnswer(const PeerConnectionInterface::RTCOfferAnswerOptions & offer_answer_options,cricket::MediaSessionOptions * session_options)3866 void SdpOfferAnswerHandler::GetOptionsForUnifiedPlanAnswer(
3867     const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options,
3868     cricket::MediaSessionOptions* session_options) {
3869   // Rules for generating an answer are dictated by JSEP sections 5.3.1 (Initial
3870   // Answers) and 5.3.2 (Subsequent Answers).
3871   RTC_DCHECK(remote_description());
3872   RTC_DCHECK(remote_description()->GetType() == SdpType::kOffer);
3873   for (const ContentInfo& content :
3874        remote_description()->description()->contents()) {
3875     cricket::MediaType media_type = content.media_description()->type();
3876     if (media_type == cricket::MEDIA_TYPE_AUDIO ||
3877         media_type == cricket::MEDIA_TYPE_VIDEO) {
3878       auto transceiver = transceivers()->FindByMid(content.name);
3879       if (transceiver) {
3880         session_options->media_description_options.push_back(
3881             GetMediaDescriptionOptionsForTransceiver(
3882                 transceiver, content.name,
3883                 /*is_create_offer=*/false));
3884       } else {
3885         // This should only happen with rejected transceivers.
3886         RTC_DCHECK(content.rejected);
3887         session_options->media_description_options.push_back(
3888             cricket::MediaDescriptionOptions(media_type, content.name,
3889                                              RtpTransceiverDirection::kInactive,
3890                                              /*stopped=*/true));
3891       }
3892     } else if (media_type == cricket::MEDIA_TYPE_UNSUPPORTED) {
3893       RTC_DCHECK(content.rejected);
3894       session_options->media_description_options.push_back(
3895           cricket::MediaDescriptionOptions(media_type, content.name,
3896                                            RtpTransceiverDirection::kInactive,
3897                                            /*stopped=*/true));
3898     } else {
3899       RTC_CHECK_EQ(cricket::MEDIA_TYPE_DATA, media_type);
3900       // Reject all data sections if data channels are disabled.
3901       // Reject a data section if it has already been rejected.
3902       // Reject all data sections except for the first one.
3903       if (pc_->data_channel_type() == cricket::DCT_NONE || content.rejected ||
3904           content.name != *(pc_->GetDataMid())) {
3905         session_options->media_description_options.push_back(
3906             GetMediaDescriptionOptionsForRejectedData(content.name));
3907       } else {
3908         session_options->media_description_options.push_back(
3909             GetMediaDescriptionOptionsForActiveData(content.name));
3910       }
3911     }
3912   }
3913 }
3914 
SessionErrorToString(SessionError error) const3915 const char* SdpOfferAnswerHandler::SessionErrorToString(
3916     SessionError error) const {
3917   switch (error) {
3918     case SessionError::kNone:
3919       return "ERROR_NONE";
3920     case SessionError::kContent:
3921       return "ERROR_CONTENT";
3922     case SessionError::kTransport:
3923       return "ERROR_TRANSPORT";
3924   }
3925   RTC_NOTREACHED();
3926   return "";
3927 }
3928 
GetSessionErrorMsg()3929 std::string SdpOfferAnswerHandler::GetSessionErrorMsg() {
3930   RTC_DCHECK_RUN_ON(signaling_thread());
3931   rtc::StringBuilder desc;
3932   desc << kSessionError << SessionErrorToString(session_error()) << ". ";
3933   desc << kSessionErrorDesc << session_error_desc() << ".";
3934   return desc.Release();
3935 }
3936 
SetSessionError(SessionError error,const std::string & error_desc)3937 void SdpOfferAnswerHandler::SetSessionError(SessionError error,
3938                                             const std::string& error_desc) {
3939   RTC_DCHECK_RUN_ON(signaling_thread());
3940   if (error != session_error_) {
3941     session_error_ = error;
3942     session_error_desc_ = error_desc;
3943   }
3944 }
3945 
HandleLegacyOfferOptions(const PeerConnectionInterface::RTCOfferAnswerOptions & options)3946 RTCError SdpOfferAnswerHandler::HandleLegacyOfferOptions(
3947     const PeerConnectionInterface::RTCOfferAnswerOptions& options) {
3948   RTC_DCHECK_RUN_ON(signaling_thread());
3949   RTC_DCHECK(IsUnifiedPlan());
3950 
3951   if (options.offer_to_receive_audio == 0) {
3952     RemoveRecvDirectionFromReceivingTransceiversOfType(
3953         cricket::MEDIA_TYPE_AUDIO);
3954   } else if (options.offer_to_receive_audio == 1) {
3955     AddUpToOneReceivingTransceiverOfType(cricket::MEDIA_TYPE_AUDIO);
3956   } else if (options.offer_to_receive_audio > 1) {
3957     LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_PARAMETER,
3958                          "offer_to_receive_audio > 1 is not supported.");
3959   }
3960 
3961   if (options.offer_to_receive_video == 0) {
3962     RemoveRecvDirectionFromReceivingTransceiversOfType(
3963         cricket::MEDIA_TYPE_VIDEO);
3964   } else if (options.offer_to_receive_video == 1) {
3965     AddUpToOneReceivingTransceiverOfType(cricket::MEDIA_TYPE_VIDEO);
3966   } else if (options.offer_to_receive_video > 1) {
3967     LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_PARAMETER,
3968                          "offer_to_receive_video > 1 is not supported.");
3969   }
3970 
3971   return RTCError::OK();
3972 }
3973 
RemoveRecvDirectionFromReceivingTransceiversOfType(cricket::MediaType media_type)3974 void SdpOfferAnswerHandler::RemoveRecvDirectionFromReceivingTransceiversOfType(
3975     cricket::MediaType media_type) {
3976   for (const auto& transceiver : GetReceivingTransceiversOfType(media_type)) {
3977     RtpTransceiverDirection new_direction =
3978         RtpTransceiverDirectionWithRecvSet(transceiver->direction(), false);
3979     if (new_direction != transceiver->direction()) {
3980       RTC_LOG(LS_INFO) << "Changing " << cricket::MediaTypeToString(media_type)
3981                        << " transceiver (MID="
3982                        << transceiver->mid().value_or("<not set>") << ") from "
3983                        << RtpTransceiverDirectionToString(
3984                               transceiver->direction())
3985                        << " to "
3986                        << RtpTransceiverDirectionToString(new_direction)
3987                        << " since CreateOffer specified offer_to_receive=0";
3988       transceiver->internal()->set_direction(new_direction);
3989     }
3990   }
3991 }
3992 
AddUpToOneReceivingTransceiverOfType(cricket::MediaType media_type)3993 void SdpOfferAnswerHandler::AddUpToOneReceivingTransceiverOfType(
3994     cricket::MediaType media_type) {
3995   RTC_DCHECK_RUN_ON(signaling_thread());
3996   if (GetReceivingTransceiversOfType(media_type).empty()) {
3997     RTC_LOG(LS_INFO)
3998         << "Adding one recvonly " << cricket::MediaTypeToString(media_type)
3999         << " transceiver since CreateOffer specified offer_to_receive=1";
4000     RtpTransceiverInit init;
4001     init.direction = RtpTransceiverDirection::kRecvOnly;
4002     pc_->AddTransceiver(media_type, nullptr, init,
4003                         /*update_negotiation_needed=*/false);
4004   }
4005 }
4006 
4007 std::vector<rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>>
GetReceivingTransceiversOfType(cricket::MediaType media_type)4008 SdpOfferAnswerHandler::GetReceivingTransceiversOfType(
4009     cricket::MediaType media_type) {
4010   std::vector<
4011       rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>>
4012       receiving_transceivers;
4013   for (const auto& transceiver : transceivers()->List()) {
4014     if (!transceiver->stopped() && transceiver->media_type() == media_type &&
4015         RtpTransceiverDirectionHasRecv(transceiver->direction())) {
4016       receiving_transceivers.push_back(transceiver);
4017     }
4018   }
4019   return receiving_transceivers;
4020 }
4021 
ProcessRemovalOfRemoteTrack(rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> transceiver,std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> * remove_list,std::vector<rtc::scoped_refptr<MediaStreamInterface>> * removed_streams)4022 void SdpOfferAnswerHandler::ProcessRemovalOfRemoteTrack(
4023     rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
4024         transceiver,
4025     std::vector<rtc::scoped_refptr<RtpTransceiverInterface>>* remove_list,
4026     std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams) {
4027   RTC_DCHECK(transceiver->mid());
4028   RTC_LOG(LS_INFO) << "Processing the removal of a track for MID="
4029                    << *transceiver->mid();
4030   std::vector<rtc::scoped_refptr<MediaStreamInterface>> previous_streams =
4031       transceiver->internal()->receiver_internal()->streams();
4032   // This will remove the remote track from the streams.
4033   transceiver->internal()->receiver_internal()->set_stream_ids({});
4034   remove_list->push_back(transceiver);
4035   RemoveRemoteStreamsIfEmpty(previous_streams, removed_streams);
4036 }
4037 
RemoveRemoteStreamsIfEmpty(const std::vector<rtc::scoped_refptr<MediaStreamInterface>> & remote_streams,std::vector<rtc::scoped_refptr<MediaStreamInterface>> * removed_streams)4038 void SdpOfferAnswerHandler::RemoveRemoteStreamsIfEmpty(
4039     const std::vector<rtc::scoped_refptr<MediaStreamInterface>>& remote_streams,
4040     std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams) {
4041   RTC_DCHECK_RUN_ON(signaling_thread());
4042   // TODO(https://crbug.com/webrtc/9480): When we use stream IDs instead of
4043   // streams, see if the stream was removed by checking if this was the last
4044   // receiver with that stream ID.
4045   for (const auto& remote_stream : remote_streams) {
4046     if (remote_stream->GetAudioTracks().empty() &&
4047         remote_stream->GetVideoTracks().empty()) {
4048       remote_streams_->RemoveStream(remote_stream);
4049       removed_streams->push_back(remote_stream);
4050     }
4051   }
4052 }
4053 
RemoveSenders(cricket::MediaType media_type)4054 void SdpOfferAnswerHandler::RemoveSenders(cricket::MediaType media_type) {
4055   RTC_DCHECK_RUN_ON(signaling_thread());
4056   UpdateLocalSenders(std::vector<cricket::StreamParams>(), media_type);
4057   UpdateRemoteSendersList(std::vector<cricket::StreamParams>(), false,
4058                           media_type, nullptr);
4059 }
4060 
UpdateLocalSenders(const std::vector<cricket::StreamParams> & streams,cricket::MediaType media_type)4061 void SdpOfferAnswerHandler::UpdateLocalSenders(
4062     const std::vector<cricket::StreamParams>& streams,
4063     cricket::MediaType media_type) {
4064   RTC_DCHECK_RUN_ON(signaling_thread());
4065   std::vector<RtpSenderInfo>* current_senders =
4066       rtp_manager()->GetLocalSenderInfos(media_type);
4067 
4068   // Find removed tracks. I.e., tracks where the track id, stream id or ssrc
4069   // don't match the new StreamParam.
4070   for (auto sender_it = current_senders->begin();
4071        sender_it != current_senders->end();
4072        /* incremented manually */) {
4073     const RtpSenderInfo& info = *sender_it;
4074     const cricket::StreamParams* params =
4075         cricket::GetStreamBySsrc(streams, info.first_ssrc);
4076     if (!params || params->id != info.sender_id ||
4077         params->first_stream_id() != info.stream_id) {
4078       rtp_manager()->OnLocalSenderRemoved(info, media_type);
4079       sender_it = current_senders->erase(sender_it);
4080     } else {
4081       ++sender_it;
4082     }
4083   }
4084 
4085   // Find new and active senders.
4086   for (const cricket::StreamParams& params : streams) {
4087     // The sync_label is the MediaStream label and the |stream.id| is the
4088     // sender id.
4089     const std::string& stream_id = params.first_stream_id();
4090     const std::string& sender_id = params.id;
4091     uint32_t ssrc = params.first_ssrc();
4092     const RtpSenderInfo* sender_info =
4093         rtp_manager()->FindSenderInfo(*current_senders, stream_id, sender_id);
4094     if (!sender_info) {
4095       current_senders->push_back(RtpSenderInfo(stream_id, sender_id, ssrc));
4096       rtp_manager()->OnLocalSenderAdded(current_senders->back(), media_type);
4097     }
4098   }
4099 }
4100 
UpdateRemoteSendersList(const cricket::StreamParamsVec & streams,bool default_sender_needed,cricket::MediaType media_type,StreamCollection * new_streams)4101 void SdpOfferAnswerHandler::UpdateRemoteSendersList(
4102     const cricket::StreamParamsVec& streams,
4103     bool default_sender_needed,
4104     cricket::MediaType media_type,
4105     StreamCollection* new_streams) {
4106   RTC_DCHECK_RUN_ON(signaling_thread());
4107   RTC_DCHECK(!IsUnifiedPlan());
4108 
4109   std::vector<RtpSenderInfo>* current_senders =
4110       rtp_manager()->GetRemoteSenderInfos(media_type);
4111 
4112   // Find removed senders. I.e., senders where the sender id or ssrc don't match
4113   // the new StreamParam.
4114   for (auto sender_it = current_senders->begin();
4115        sender_it != current_senders->end();
4116        /* incremented manually */) {
4117     const RtpSenderInfo& info = *sender_it;
4118     const cricket::StreamParams* params =
4119         cricket::GetStreamBySsrc(streams, info.first_ssrc);
4120     std::string params_stream_id;
4121     if (params) {
4122       params_stream_id =
4123           (!params->first_stream_id().empty() ? params->first_stream_id()
4124                                               : kDefaultStreamId);
4125     }
4126     bool sender_exists = params && params->id == info.sender_id &&
4127                          params_stream_id == info.stream_id;
4128     // If this is a default track, and we still need it, don't remove it.
4129     if ((info.stream_id == kDefaultStreamId && default_sender_needed) ||
4130         sender_exists) {
4131       ++sender_it;
4132     } else {
4133       rtp_manager()->OnRemoteSenderRemoved(
4134           info, remote_streams_->find(info.stream_id), media_type);
4135       sender_it = current_senders->erase(sender_it);
4136     }
4137   }
4138 
4139   // Find new and active senders.
4140   for (const cricket::StreamParams& params : streams) {
4141     if (!params.has_ssrcs()) {
4142       // The remote endpoint has streams, but didn't signal ssrcs. For an active
4143       // sender, this means it is coming from a Unified Plan endpoint,so we just
4144       // create a default.
4145       default_sender_needed = true;
4146       break;
4147     }
4148 
4149     // |params.id| is the sender id and the stream id uses the first of
4150     // |params.stream_ids|. The remote description could come from a Unified
4151     // Plan endpoint, with multiple or no stream_ids() signaled. Since this is
4152     // not supported in Plan B, we just take the first here and create the
4153     // default stream ID if none is specified.
4154     const std::string& stream_id =
4155         (!params.first_stream_id().empty() ? params.first_stream_id()
4156                                            : kDefaultStreamId);
4157     const std::string& sender_id = params.id;
4158     uint32_t ssrc = params.first_ssrc();
4159 
4160     rtc::scoped_refptr<MediaStreamInterface> stream =
4161         remote_streams_->find(stream_id);
4162     if (!stream) {
4163       // This is a new MediaStream. Create a new remote MediaStream.
4164       stream = MediaStreamProxy::Create(rtc::Thread::Current(),
4165                                         MediaStream::Create(stream_id));
4166       remote_streams_->AddStream(stream);
4167       new_streams->AddStream(stream);
4168     }
4169 
4170     const RtpSenderInfo* sender_info =
4171         rtp_manager()->FindSenderInfo(*current_senders, stream_id, sender_id);
4172     if (!sender_info) {
4173       current_senders->push_back(RtpSenderInfo(stream_id, sender_id, ssrc));
4174       rtp_manager()->OnRemoteSenderAdded(current_senders->back(), stream,
4175                                          media_type);
4176     }
4177   }
4178 
4179   // Add default sender if necessary.
4180   if (default_sender_needed) {
4181     rtc::scoped_refptr<MediaStreamInterface> default_stream =
4182         remote_streams_->find(kDefaultStreamId);
4183     if (!default_stream) {
4184       // Create the new default MediaStream.
4185       default_stream = MediaStreamProxy::Create(
4186           rtc::Thread::Current(), MediaStream::Create(kDefaultStreamId));
4187       remote_streams_->AddStream(default_stream);
4188       new_streams->AddStream(default_stream);
4189     }
4190     std::string default_sender_id = (media_type == cricket::MEDIA_TYPE_AUDIO)
4191                                         ? kDefaultAudioSenderId
4192                                         : kDefaultVideoSenderId;
4193     const RtpSenderInfo* default_sender_info = rtp_manager()->FindSenderInfo(
4194         *current_senders, kDefaultStreamId, default_sender_id);
4195     if (!default_sender_info) {
4196       current_senders->push_back(
4197           RtpSenderInfo(kDefaultStreamId, default_sender_id, /*ssrc=*/0));
4198       rtp_manager()->OnRemoteSenderAdded(current_senders->back(),
4199                                          default_stream, media_type);
4200     }
4201   }
4202 }
4203 
EnableSending()4204 void SdpOfferAnswerHandler::EnableSending() {
4205   RTC_DCHECK_RUN_ON(signaling_thread());
4206   for (const auto& transceiver : transceivers()->List()) {
4207     cricket::ChannelInterface* channel = transceiver->internal()->channel();
4208     if (channel && !channel->enabled()) {
4209       channel->Enable(true);
4210     }
4211   }
4212 
4213   if (data_channel_controller()->rtp_data_channel() &&
4214       !data_channel_controller()->rtp_data_channel()->enabled()) {
4215     data_channel_controller()->rtp_data_channel()->Enable(true);
4216   }
4217 }
4218 
PushdownMediaDescription(SdpType type,cricket::ContentSource source)4219 RTCError SdpOfferAnswerHandler::PushdownMediaDescription(
4220     SdpType type,
4221     cricket::ContentSource source) {
4222   const SessionDescriptionInterface* sdesc =
4223       (source == cricket::CS_LOCAL ? local_description()
4224                                    : remote_description());
4225   RTC_DCHECK_RUN_ON(signaling_thread());
4226   RTC_DCHECK(sdesc);
4227 
4228   if (!UpdatePayloadTypeDemuxingState(source)) {
4229     // Note that this is never expected to fail, since RtpDemuxer doesn't return
4230     // an error when changing payload type demux criteria, which is all this
4231     // does.
4232     LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR,
4233                          "Failed to update payload type demuxing state.");
4234   }
4235 
4236   // Push down the new SDP media section for each audio/video transceiver.
4237   for (const auto& transceiver : transceivers()->List()) {
4238     const ContentInfo* content_info =
4239         FindMediaSectionForTransceiver(transceiver, sdesc);
4240     cricket::ChannelInterface* channel = transceiver->internal()->channel();
4241     if (!channel || !content_info || content_info->rejected) {
4242       continue;
4243     }
4244     const MediaContentDescription* content_desc =
4245         content_info->media_description();
4246     if (!content_desc) {
4247       continue;
4248     }
4249     std::string error;
4250     bool success = (source == cricket::CS_LOCAL)
4251                        ? channel->SetLocalContent(content_desc, type, &error)
4252                        : channel->SetRemoteContent(content_desc, type, &error);
4253     if (!success) {
4254       LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, error);
4255     }
4256   }
4257 
4258   // If using the RtpDataChannel, push down the new SDP section for it too.
4259   if (data_channel_controller()->rtp_data_channel()) {
4260     const ContentInfo* data_content =
4261         cricket::GetFirstDataContent(sdesc->description());
4262     if (data_content && !data_content->rejected) {
4263       const MediaContentDescription* data_desc =
4264           data_content->media_description();
4265       if (data_desc) {
4266         std::string error;
4267         bool success = (source == cricket::CS_LOCAL)
4268                            ? data_channel_controller()
4269                                  ->rtp_data_channel()
4270                                  ->SetLocalContent(data_desc, type, &error)
4271                            : data_channel_controller()
4272                                  ->rtp_data_channel()
4273                                  ->SetRemoteContent(data_desc, type, &error);
4274         if (!success) {
4275           LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, error);
4276         }
4277       }
4278     }
4279   }
4280 
4281   // Need complete offer/answer with an SCTP m= section before starting SCTP,
4282   // according to https://tools.ietf.org/html/draft-ietf-mmusic-sctp-sdp-19
4283   if (pc_->sctp_mid() && local_description() && remote_description()) {
4284     rtc::scoped_refptr<SctpTransport> sctp_transport =
4285         transport_controller()->GetSctpTransport(*(pc_->sctp_mid()));
4286     auto local_sctp_description = cricket::GetFirstSctpDataContentDescription(
4287         local_description()->description());
4288     auto remote_sctp_description = cricket::GetFirstSctpDataContentDescription(
4289         remote_description()->description());
4290     if (sctp_transport && local_sctp_description && remote_sctp_description) {
4291       int max_message_size;
4292       // A remote max message size of zero means "any size supported".
4293       // We configure the connection with our own max message size.
4294       if (remote_sctp_description->max_message_size() == 0) {
4295         max_message_size = local_sctp_description->max_message_size();
4296       } else {
4297         max_message_size =
4298             std::min(local_sctp_description->max_message_size(),
4299                      remote_sctp_description->max_message_size());
4300       }
4301       sctp_transport->Start(local_sctp_description->port(),
4302                             remote_sctp_description->port(), max_message_size);
4303     }
4304   }
4305 
4306   return RTCError::OK();
4307 }
4308 
PushdownTransportDescription(cricket::ContentSource source,SdpType type)4309 RTCError SdpOfferAnswerHandler::PushdownTransportDescription(
4310     cricket::ContentSource source,
4311     SdpType type) {
4312   RTC_DCHECK_RUN_ON(signaling_thread());
4313 
4314   if (source == cricket::CS_LOCAL) {
4315     const SessionDescriptionInterface* sdesc = local_description();
4316     RTC_DCHECK(sdesc);
4317     return transport_controller()->SetLocalDescription(type,
4318                                                        sdesc->description());
4319   } else {
4320     const SessionDescriptionInterface* sdesc = remote_description();
4321     RTC_DCHECK(sdesc);
4322     return transport_controller()->SetRemoteDescription(type,
4323                                                         sdesc->description());
4324   }
4325 }
4326 
RemoveStoppedTransceivers()4327 void SdpOfferAnswerHandler::RemoveStoppedTransceivers() {
4328   RTC_DCHECK_RUN_ON(signaling_thread());
4329   // 3.2.10.1: For each transceiver in the connection's set of transceivers
4330   //           run the following steps:
4331   if (!IsUnifiedPlan())
4332     return;
4333   // Traverse a copy of the transceiver list.
4334   auto transceiver_list = transceivers()->List();
4335   for (auto transceiver : transceiver_list) {
4336     // 3.2.10.1.1: If transceiver is stopped, associated with an m= section
4337     //             and the associated m= section is rejected in
4338     //             connection.[[CurrentLocalDescription]] or
4339     //             connection.[[CurrentRemoteDescription]], remove the
4340     //             transceiver from the connection's set of transceivers.
4341     if (!transceiver->stopped()) {
4342       continue;
4343     }
4344     const ContentInfo* local_content =
4345         FindMediaSectionForTransceiver(transceiver, local_description());
4346     const ContentInfo* remote_content =
4347         FindMediaSectionForTransceiver(transceiver, remote_description());
4348     if ((local_content && local_content->rejected) ||
4349         (remote_content && remote_content->rejected)) {
4350       RTC_LOG(LS_INFO) << "Dissociating transceiver"
4351                        << " since the media section is being recycled.";
4352       transceiver->internal()->set_mid(absl::nullopt);
4353       transceiver->internal()->set_mline_index(absl::nullopt);
4354       transceivers()->Remove(transceiver);
4355       continue;
4356     }
4357     if (!local_content && !remote_content) {
4358       // TODO(bugs.webrtc.org/11973): Consider if this should be removed already
4359       // See https://github.com/w3c/webrtc-pc/issues/2576
4360       RTC_LOG(LS_INFO)
4361           << "Dropping stopped transceiver that was never associated";
4362       transceivers()->Remove(transceiver);
4363       continue;
4364     }
4365   }
4366 }
4367 
RemoveUnusedChannels(const SessionDescription * desc)4368 void SdpOfferAnswerHandler::RemoveUnusedChannels(
4369     const SessionDescription* desc) {
4370   RTC_DCHECK_RUN_ON(signaling_thread());
4371   // Destroy video channel first since it may have a pointer to the
4372   // voice channel.
4373   const cricket::ContentInfo* video_info = cricket::GetFirstVideoContent(desc);
4374   if (!video_info || video_info->rejected) {
4375     DestroyTransceiverChannel(rtp_manager()->GetVideoTransceiver());
4376   }
4377 
4378   const cricket::ContentInfo* audio_info = cricket::GetFirstAudioContent(desc);
4379   if (!audio_info || audio_info->rejected) {
4380     DestroyTransceiverChannel(rtp_manager()->GetAudioTransceiver());
4381   }
4382 
4383   const cricket::ContentInfo* data_info = cricket::GetFirstDataContent(desc);
4384   if (!data_info || data_info->rejected) {
4385     DestroyDataChannelTransport();
4386   }
4387 }
4388 
ReportNegotiatedSdpSemantics(const SessionDescriptionInterface & answer)4389 void SdpOfferAnswerHandler::ReportNegotiatedSdpSemantics(
4390     const SessionDescriptionInterface& answer) {
4391   SdpSemanticNegotiated semantics_negotiated;
4392   switch (answer.description()->msid_signaling()) {
4393     case 0:
4394       semantics_negotiated = kSdpSemanticNegotiatedNone;
4395       break;
4396     case cricket::kMsidSignalingMediaSection:
4397       semantics_negotiated = kSdpSemanticNegotiatedUnifiedPlan;
4398       break;
4399     case cricket::kMsidSignalingSsrcAttribute:
4400       semantics_negotiated = kSdpSemanticNegotiatedPlanB;
4401       break;
4402     case cricket::kMsidSignalingMediaSection |
4403         cricket::kMsidSignalingSsrcAttribute:
4404       semantics_negotiated = kSdpSemanticNegotiatedMixed;
4405       break;
4406     default:
4407       RTC_NOTREACHED();
4408   }
4409   RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.SdpSemanticNegotiated",
4410                             semantics_negotiated, kSdpSemanticNegotiatedMax);
4411 }
4412 
UpdateEndedRemoteMediaStreams()4413 void SdpOfferAnswerHandler::UpdateEndedRemoteMediaStreams() {
4414   RTC_DCHECK_RUN_ON(signaling_thread());
4415   std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams_to_remove;
4416   for (size_t i = 0; i < remote_streams_->count(); ++i) {
4417     MediaStreamInterface* stream = remote_streams_->at(i);
4418     if (stream->GetAudioTracks().empty() && stream->GetVideoTracks().empty()) {
4419       streams_to_remove.push_back(stream);
4420     }
4421   }
4422 
4423   for (auto& stream : streams_to_remove) {
4424     remote_streams_->RemoveStream(stream);
4425     pc_->Observer()->OnRemoveStream(std::move(stream));
4426   }
4427 }
4428 
UseCandidatesInSessionDescription(const SessionDescriptionInterface * remote_desc)4429 bool SdpOfferAnswerHandler::UseCandidatesInSessionDescription(
4430     const SessionDescriptionInterface* remote_desc) {
4431   RTC_DCHECK_RUN_ON(signaling_thread());
4432   if (!remote_desc) {
4433     return true;
4434   }
4435   bool ret = true;
4436 
4437   for (size_t m = 0; m < remote_desc->number_of_mediasections(); ++m) {
4438     const IceCandidateCollection* candidates = remote_desc->candidates(m);
4439     for (size_t n = 0; n < candidates->count(); ++n) {
4440       const IceCandidateInterface* candidate = candidates->at(n);
4441       bool valid = false;
4442       if (!ReadyToUseRemoteCandidate(candidate, remote_desc, &valid)) {
4443         if (valid) {
4444           RTC_LOG(LS_INFO)
4445               << "UseCandidatesInSessionDescription: Not ready to use "
4446                  "candidate.";
4447         }
4448         continue;
4449       }
4450       ret = UseCandidate(candidate);
4451       if (!ret) {
4452         break;
4453       }
4454     }
4455   }
4456   return ret;
4457 }
4458 
UseCandidate(const IceCandidateInterface * candidate)4459 bool SdpOfferAnswerHandler::UseCandidate(
4460     const IceCandidateInterface* candidate) {
4461   RTC_DCHECK_RUN_ON(signaling_thread());
4462   RTCErrorOr<const cricket::ContentInfo*> result =
4463       FindContentInfo(remote_description(), candidate);
4464   if (!result.ok()) {
4465     RTC_LOG(LS_ERROR) << "UseCandidate: Invalid candidate. "
4466                       << result.error().message();
4467     return false;
4468   }
4469   std::vector<cricket::Candidate> candidates;
4470   candidates.push_back(candidate->candidate());
4471   // Invoking BaseSession method to handle remote candidates.
4472   RTCError error = transport_controller()->AddRemoteCandidates(
4473       result.value()->name, candidates);
4474   if (error.ok()) {
4475     ReportRemoteIceCandidateAdded(candidate->candidate());
4476     // Candidates successfully submitted for checking.
4477     if (pc_->ice_connection_state() ==
4478             PeerConnectionInterface::kIceConnectionNew ||
4479         pc_->ice_connection_state() ==
4480             PeerConnectionInterface::kIceConnectionDisconnected) {
4481       // If state is New, then the session has just gotten its first remote ICE
4482       // candidates, so go to Checking.
4483       // If state is Disconnected, the session is re-using old candidates or
4484       // receiving additional ones, so go to Checking.
4485       // If state is Connected, stay Connected.
4486       // TODO(bemasc): If state is Connected, and the new candidates are for a
4487       // newly added transport, then the state actually _should_ move to
4488       // checking.  Add a way to distinguish that case.
4489       pc_->SetIceConnectionState(
4490           PeerConnectionInterface::kIceConnectionChecking);
4491     }
4492     // TODO(bemasc): If state is Completed, go back to Connected.
4493   } else {
4494     RTC_LOG(LS_WARNING) << error.message();
4495   }
4496   return true;
4497 }
4498 
4499 // We need to check the local/remote description for the Transport instead of
4500 // the session, because a new Transport added during renegotiation may have
4501 // them unset while the session has them set from the previous negotiation.
4502 // Not doing so may trigger the auto generation of transport description and
4503 // mess up DTLS identity information, ICE credential, etc.
ReadyToUseRemoteCandidate(const IceCandidateInterface * candidate,const SessionDescriptionInterface * remote_desc,bool * valid)4504 bool SdpOfferAnswerHandler::ReadyToUseRemoteCandidate(
4505     const IceCandidateInterface* candidate,
4506     const SessionDescriptionInterface* remote_desc,
4507     bool* valid) {
4508   RTC_DCHECK_RUN_ON(signaling_thread());
4509   *valid = true;
4510 
4511   const SessionDescriptionInterface* current_remote_desc =
4512       remote_desc ? remote_desc : remote_description();
4513 
4514   if (!current_remote_desc) {
4515     return false;
4516   }
4517 
4518   RTCErrorOr<const cricket::ContentInfo*> result =
4519       FindContentInfo(current_remote_desc, candidate);
4520   if (!result.ok()) {
4521     RTC_LOG(LS_ERROR) << "ReadyToUseRemoteCandidate: Invalid candidate. "
4522                       << result.error().message();
4523 
4524     *valid = false;
4525     return false;
4526   }
4527 
4528   std::string transport_name = GetTransportName(result.value()->name);
4529   return !transport_name.empty();
4530 }
4531 
ReportRemoteIceCandidateAdded(const cricket::Candidate & candidate)4532 void SdpOfferAnswerHandler::ReportRemoteIceCandidateAdded(
4533     const cricket::Candidate& candidate) {
4534   pc_->NoteUsageEvent(UsageEvent::REMOTE_CANDIDATE_ADDED);
4535   if (candidate.address().IsPrivateIP()) {
4536     pc_->NoteUsageEvent(UsageEvent::REMOTE_PRIVATE_CANDIDATE_ADDED);
4537   }
4538   if (candidate.address().IsUnresolvedIP()) {
4539     pc_->NoteUsageEvent(UsageEvent::REMOTE_MDNS_CANDIDATE_ADDED);
4540   }
4541   if (candidate.address().family() == AF_INET6) {
4542     pc_->NoteUsageEvent(UsageEvent::REMOTE_IPV6_CANDIDATE_ADDED);
4543   }
4544 }
4545 
FindContentInfo(const SessionDescriptionInterface * description,const IceCandidateInterface * candidate)4546 RTCErrorOr<const cricket::ContentInfo*> SdpOfferAnswerHandler::FindContentInfo(
4547     const SessionDescriptionInterface* description,
4548     const IceCandidateInterface* candidate) {
4549   if (candidate->sdp_mline_index() >= 0) {
4550     size_t mediacontent_index =
4551         static_cast<size_t>(candidate->sdp_mline_index());
4552     size_t content_size = description->description()->contents().size();
4553     if (mediacontent_index < content_size) {
4554       return &description->description()->contents()[mediacontent_index];
4555     } else {
4556       return RTCError(RTCErrorType::INVALID_RANGE,
4557                       "Media line index (" +
4558                           rtc::ToString(candidate->sdp_mline_index()) +
4559                           ") out of range (number of mlines: " +
4560                           rtc::ToString(content_size) + ").");
4561     }
4562   } else if (!candidate->sdp_mid().empty()) {
4563     auto& contents = description->description()->contents();
4564     auto it = absl::c_find_if(
4565         contents, [candidate](const cricket::ContentInfo& content_info) {
4566           return content_info.mid() == candidate->sdp_mid();
4567         });
4568     if (it == contents.end()) {
4569       return RTCError(
4570           RTCErrorType::INVALID_PARAMETER,
4571           "Mid " + candidate->sdp_mid() +
4572               " specified but no media section with that mid found.");
4573     } else {
4574       return &*it;
4575     }
4576   }
4577 
4578   return RTCError(RTCErrorType::INVALID_PARAMETER,
4579                   "Neither sdp_mline_index nor sdp_mid specified.");
4580 }
4581 
CreateChannels(const SessionDescription & desc)4582 RTCError SdpOfferAnswerHandler::CreateChannels(const SessionDescription& desc) {
4583   // Creating the media channels. Transports should already have been created
4584   // at this point.
4585   RTC_DCHECK_RUN_ON(signaling_thread());
4586   const cricket::ContentInfo* voice = cricket::GetFirstAudioContent(&desc);
4587   if (voice && !voice->rejected &&
4588       !rtp_manager()->GetAudioTransceiver()->internal()->channel()) {
4589     cricket::VoiceChannel* voice_channel = CreateVoiceChannel(voice->name);
4590     if (!voice_channel) {
4591       LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR,
4592                            "Failed to create voice channel.");
4593     }
4594     rtp_manager()->GetAudioTransceiver()->internal()->SetChannel(voice_channel);
4595   }
4596 
4597   const cricket::ContentInfo* video = cricket::GetFirstVideoContent(&desc);
4598   if (video && !video->rejected &&
4599       !rtp_manager()->GetVideoTransceiver()->internal()->channel()) {
4600     cricket::VideoChannel* video_channel = CreateVideoChannel(video->name);
4601     if (!video_channel) {
4602       LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR,
4603                            "Failed to create video channel.");
4604     }
4605     rtp_manager()->GetVideoTransceiver()->internal()->SetChannel(video_channel);
4606   }
4607 
4608   const cricket::ContentInfo* data = cricket::GetFirstDataContent(&desc);
4609   if (pc_->data_channel_type() != cricket::DCT_NONE && data &&
4610       !data->rejected && !data_channel_controller()->rtp_data_channel() &&
4611       !data_channel_controller()->data_channel_transport()) {
4612     if (!CreateDataChannel(data->name)) {
4613       LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR,
4614                            "Failed to create data channel.");
4615     }
4616   }
4617 
4618   return RTCError::OK();
4619 }
4620 
4621 // TODO(steveanton): Perhaps this should be managed by the RtpTransceiver.
CreateVoiceChannel(const std::string & mid)4622 cricket::VoiceChannel* SdpOfferAnswerHandler::CreateVoiceChannel(
4623     const std::string& mid) {
4624   RTC_DCHECK_RUN_ON(signaling_thread());
4625   RtpTransportInternal* rtp_transport = pc_->GetRtpTransport(mid);
4626 
4627   // TODO(bugs.webrtc.org/11992): CreateVoiceChannel internally switches to the
4628   // worker thread. We shouldn't be using the |call_ptr_| hack here but simply
4629   // be on the worker thread and use |call_| (update upstream code).
4630   cricket::VoiceChannel* voice_channel;
4631   {
4632     RTC_DCHECK_RUN_ON(pc_->signaling_thread());
4633     voice_channel = channel_manager()->CreateVoiceChannel(
4634         pc_->call_ptr(), pc_->configuration()->media_config, rtp_transport,
4635         signaling_thread(), mid, pc_->SrtpRequired(), pc_->GetCryptoOptions(),
4636         &ssrc_generator_, audio_options());
4637   }
4638   if (!voice_channel) {
4639     return nullptr;
4640   }
4641   voice_channel->SignalSentPacket().connect(pc_,
4642                                             &PeerConnection::OnSentPacket_w);
4643   voice_channel->SetRtpTransport(rtp_transport);
4644 
4645   return voice_channel;
4646 }
4647 
4648 // TODO(steveanton): Perhaps this should be managed by the RtpTransceiver.
CreateVideoChannel(const std::string & mid)4649 cricket::VideoChannel* SdpOfferAnswerHandler::CreateVideoChannel(
4650     const std::string& mid) {
4651   RTC_DCHECK_RUN_ON(signaling_thread());
4652   RtpTransportInternal* rtp_transport = pc_->GetRtpTransport(mid);
4653 
4654   // TODO(bugs.webrtc.org/11992): CreateVideoChannel internally switches to the
4655   // worker thread. We shouldn't be using the |call_ptr_| hack here but simply
4656   // be on the worker thread and use |call_| (update upstream code).
4657   cricket::VideoChannel* video_channel;
4658   {
4659     RTC_DCHECK_RUN_ON(pc_->signaling_thread());
4660     video_channel = channel_manager()->CreateVideoChannel(
4661         pc_->call_ptr(), pc_->configuration()->media_config, rtp_transport,
4662         signaling_thread(), mid, pc_->SrtpRequired(), pc_->GetCryptoOptions(),
4663         &ssrc_generator_, video_options(),
4664         video_bitrate_allocator_factory_.get());
4665   }
4666   if (!video_channel) {
4667     return nullptr;
4668   }
4669   video_channel->SignalSentPacket().connect(pc_,
4670                                             &PeerConnection::OnSentPacket_w);
4671   video_channel->SetRtpTransport(rtp_transport);
4672 
4673   return video_channel;
4674 }
4675 
CreateDataChannel(const std::string & mid)4676 bool SdpOfferAnswerHandler::CreateDataChannel(const std::string& mid) {
4677   RTC_DCHECK_RUN_ON(signaling_thread());
4678   switch (pc_->data_channel_type()) {
4679     case cricket::DCT_SCTP:
4680       if (pc_->network_thread()->Invoke<bool>(
4681               RTC_FROM_HERE,
4682               rtc::Bind(&PeerConnection::SetupDataChannelTransport_n, pc_,
4683                         mid))) {
4684         pc_->SetSctpDataMid(mid);
4685       } else {
4686         return false;
4687       }
4688       return true;
4689     case cricket::DCT_RTP:
4690     default:
4691       RtpTransportInternal* rtp_transport = pc_->GetRtpTransport(mid);
4692       // TODO(bugs.webrtc.org/9987): set_rtp_data_channel() should be called on
4693       // the network thread like set_data_channel_transport is.
4694       {
4695         RTC_DCHECK_RUN_ON(pc_->signaling_thread());
4696         data_channel_controller()->set_rtp_data_channel(
4697             channel_manager()->CreateRtpDataChannel(
4698                 pc_->configuration()->media_config, rtp_transport,
4699                 signaling_thread(), mid, pc_->SrtpRequired(),
4700                 pc_->GetCryptoOptions(), &ssrc_generator_));
4701       }
4702       if (!data_channel_controller()->rtp_data_channel()) {
4703         return false;
4704       }
4705       data_channel_controller()->rtp_data_channel()->SignalSentPacket().connect(
4706           pc_, &PeerConnection::OnSentPacket_w);
4707       data_channel_controller()->rtp_data_channel()->SetRtpTransport(
4708           rtp_transport);
4709       SetHavePendingRtpDataChannel();
4710       return true;
4711   }
4712   return false;
4713 }
4714 
DestroyTransceiverChannel(rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> transceiver)4715 void SdpOfferAnswerHandler::DestroyTransceiverChannel(
4716     rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
4717         transceiver) {
4718   RTC_DCHECK(transceiver);
4719 
4720   cricket::ChannelInterface* channel = transceiver->internal()->channel();
4721   if (channel) {
4722     transceiver->internal()->SetChannel(nullptr);
4723     DestroyChannelInterface(channel);
4724   }
4725 }
4726 
DestroyDataChannelTransport()4727 void SdpOfferAnswerHandler::DestroyDataChannelTransport() {
4728   RTC_DCHECK_RUN_ON(signaling_thread());
4729   if (data_channel_controller()->rtp_data_channel()) {
4730     data_channel_controller()->OnTransportChannelClosed();
4731     DestroyChannelInterface(data_channel_controller()->rtp_data_channel());
4732     data_channel_controller()->set_rtp_data_channel(nullptr);
4733   }
4734 
4735   // Note: Cannot use rtc::Bind to create a functor to invoke because it will
4736   // grab a reference to this PeerConnection. If this is called from the
4737   // PeerConnection destructor, the RefCountedObject vtable will have already
4738   // been destroyed (since it is a subclass of PeerConnection) and using
4739   // rtc::Bind will cause "Pure virtual function called" error to appear.
4740 
4741   if (pc_->sctp_mid()) {
4742     RTC_DCHECK_RUN_ON(pc_->signaling_thread());
4743     data_channel_controller()->OnTransportChannelClosed();
4744     pc_->network_thread()->Invoke<void>(RTC_FROM_HERE, [this] {
4745       RTC_DCHECK_RUN_ON(pc_->network_thread());
4746       pc_->TeardownDataChannelTransport_n();
4747     });
4748     pc_->ResetSctpDataMid();
4749   }
4750 }
4751 
DestroyChannelInterface(cricket::ChannelInterface * channel)4752 void SdpOfferAnswerHandler::DestroyChannelInterface(
4753     cricket::ChannelInterface* channel) {
4754   // TODO(bugs.webrtc.org/11992): All the below methods should be called on the
4755   // worker thread. (they switch internally anyway). Change
4756   // DestroyChannelInterface to either be called on the worker thread, or do
4757   // this asynchronously on the worker.
4758   RTC_DCHECK(channel);
4759   switch (channel->media_type()) {
4760     case cricket::MEDIA_TYPE_AUDIO:
4761       channel_manager()->DestroyVoiceChannel(
4762           static_cast<cricket::VoiceChannel*>(channel));
4763       break;
4764     case cricket::MEDIA_TYPE_VIDEO:
4765       channel_manager()->DestroyVideoChannel(
4766           static_cast<cricket::VideoChannel*>(channel));
4767       break;
4768     case cricket::MEDIA_TYPE_DATA:
4769       channel_manager()->DestroyRtpDataChannel(
4770           static_cast<cricket::RtpDataChannel*>(channel));
4771       break;
4772     default:
4773       RTC_NOTREACHED() << "Unknown media type: " << channel->media_type();
4774       break;
4775   }
4776 }
4777 
DestroyAllChannels()4778 void SdpOfferAnswerHandler::DestroyAllChannels() {
4779   RTC_DCHECK_RUN_ON(signaling_thread());
4780   if (!transceivers()) {
4781     return;
4782   }
4783   // Destroy video channels first since they may have a pointer to a voice
4784   // channel.
4785   for (const auto& transceiver : transceivers()->List()) {
4786     if (transceiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
4787       DestroyTransceiverChannel(transceiver);
4788     }
4789   }
4790   for (const auto& transceiver : transceivers()->List()) {
4791     if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) {
4792       DestroyTransceiverChannel(transceiver);
4793     }
4794   }
4795   DestroyDataChannelTransport();
4796 }
4797 
GenerateMediaDescriptionOptions(const SessionDescriptionInterface * session_desc,RtpTransceiverDirection audio_direction,RtpTransceiverDirection video_direction,absl::optional<size_t> * audio_index,absl::optional<size_t> * video_index,absl::optional<size_t> * data_index,cricket::MediaSessionOptions * session_options)4798 void SdpOfferAnswerHandler::GenerateMediaDescriptionOptions(
4799     const SessionDescriptionInterface* session_desc,
4800     RtpTransceiverDirection audio_direction,
4801     RtpTransceiverDirection video_direction,
4802     absl::optional<size_t>* audio_index,
4803     absl::optional<size_t>* video_index,
4804     absl::optional<size_t>* data_index,
4805     cricket::MediaSessionOptions* session_options) {
4806   RTC_DCHECK_RUN_ON(signaling_thread());
4807   for (const cricket::ContentInfo& content :
4808        session_desc->description()->contents()) {
4809     if (IsAudioContent(&content)) {
4810       // If we already have an audio m= section, reject this extra one.
4811       if (*audio_index) {
4812         session_options->media_description_options.push_back(
4813             cricket::MediaDescriptionOptions(
4814                 cricket::MEDIA_TYPE_AUDIO, content.name,
4815                 RtpTransceiverDirection::kInactive, /*stopped=*/true));
4816       } else {
4817         bool stopped = (audio_direction == RtpTransceiverDirection::kInactive);
4818         session_options->media_description_options.push_back(
4819             cricket::MediaDescriptionOptions(cricket::MEDIA_TYPE_AUDIO,
4820                                              content.name, audio_direction,
4821                                              stopped));
4822         *audio_index = session_options->media_description_options.size() - 1;
4823       }
4824       session_options->media_description_options.back().header_extensions =
4825           channel_manager()->GetSupportedAudioRtpHeaderExtensions();
4826     } else if (IsVideoContent(&content)) {
4827       // If we already have an video m= section, reject this extra one.
4828       if (*video_index) {
4829         session_options->media_description_options.push_back(
4830             cricket::MediaDescriptionOptions(
4831                 cricket::MEDIA_TYPE_VIDEO, content.name,
4832                 RtpTransceiverDirection::kInactive, /*stopped=*/true));
4833       } else {
4834         bool stopped = (video_direction == RtpTransceiverDirection::kInactive);
4835         session_options->media_description_options.push_back(
4836             cricket::MediaDescriptionOptions(cricket::MEDIA_TYPE_VIDEO,
4837                                              content.name, video_direction,
4838                                              stopped));
4839         *video_index = session_options->media_description_options.size() - 1;
4840       }
4841       session_options->media_description_options.back().header_extensions =
4842           channel_manager()->GetSupportedVideoRtpHeaderExtensions();
4843     } else if (IsUnsupportedContent(&content)) {
4844       session_options->media_description_options.push_back(
4845           cricket::MediaDescriptionOptions(cricket::MEDIA_TYPE_UNSUPPORTED,
4846                                            content.name,
4847                                            RtpTransceiverDirection::kInactive,
4848                                            /*stopped=*/true));
4849     } else {
4850       RTC_DCHECK(IsDataContent(&content));
4851       // If we already have an data m= section, reject this extra one.
4852       if (*data_index) {
4853         session_options->media_description_options.push_back(
4854             GetMediaDescriptionOptionsForRejectedData(content.name));
4855       } else {
4856         session_options->media_description_options.push_back(
4857             GetMediaDescriptionOptionsForActiveData(content.name));
4858         *data_index = session_options->media_description_options.size() - 1;
4859       }
4860     }
4861   }
4862 }
4863 
4864 cricket::MediaDescriptionOptions
GetMediaDescriptionOptionsForActiveData(const std::string & mid) const4865 SdpOfferAnswerHandler::GetMediaDescriptionOptionsForActiveData(
4866     const std::string& mid) const {
4867   RTC_DCHECK_RUN_ON(signaling_thread());
4868   // Direction for data sections is meaningless, but legacy endpoints might
4869   // expect sendrecv.
4870   cricket::MediaDescriptionOptions options(cricket::MEDIA_TYPE_DATA, mid,
4871                                            RtpTransceiverDirection::kSendRecv,
4872                                            /*stopped=*/false);
4873   AddRtpDataChannelOptions(*(data_channel_controller()->rtp_data_channels()),
4874                            &options);
4875   return options;
4876 }
4877 
4878 cricket::MediaDescriptionOptions
GetMediaDescriptionOptionsForRejectedData(const std::string & mid) const4879 SdpOfferAnswerHandler::GetMediaDescriptionOptionsForRejectedData(
4880     const std::string& mid) const {
4881   RTC_DCHECK_RUN_ON(signaling_thread());
4882   cricket::MediaDescriptionOptions options(cricket::MEDIA_TYPE_DATA, mid,
4883                                            RtpTransceiverDirection::kInactive,
4884                                            /*stopped=*/true);
4885   AddRtpDataChannelOptions(*(data_channel_controller()->rtp_data_channels()),
4886                            &options);
4887   return options;
4888 }
4889 
GetTransportName(const std::string & content_name)4890 const std::string SdpOfferAnswerHandler::GetTransportName(
4891     const std::string& content_name) {
4892   RTC_DCHECK_RUN_ON(signaling_thread());
4893   cricket::ChannelInterface* channel = pc_->GetChannel(content_name);
4894   if (channel) {
4895     return channel->transport_name();
4896   }
4897   if (data_channel_controller()->data_channel_transport()) {
4898     RTC_DCHECK(pc_->sctp_mid());
4899     if (content_name == *(pc_->sctp_mid())) {
4900       return *(pc_->sctp_transport_name());
4901     }
4902   }
4903   // Return an empty string if failed to retrieve the transport name.
4904   return "";
4905 }
4906 
UpdatePayloadTypeDemuxingState(cricket::ContentSource source)4907 bool SdpOfferAnswerHandler::UpdatePayloadTypeDemuxingState(
4908     cricket::ContentSource source) {
4909   RTC_DCHECK_RUN_ON(signaling_thread());
4910   // We may need to delete any created default streams and disable creation of
4911   // new ones on the basis of payload type. This is needed to avoid SSRC
4912   // collisions in Call's RtpDemuxer, in the case that a transceiver has
4913   // created a default stream, and then some other channel gets the SSRC
4914   // signaled in the corresponding Unified Plan "m=" section. Specifically, we
4915   // need to disable payload type based demuxing when two bundled "m=" sections
4916   // are using the same payload type(s). For more context
4917   // see https://bugs.chromium.org/p/webrtc/issues/detail?id=11477
4918   const SessionDescriptionInterface* sdesc =
4919       (source == cricket::CS_LOCAL ? local_description()
4920                                    : remote_description());
4921   const cricket::ContentGroup* bundle_group =
4922       sdesc->description()->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
4923   std::set<int> audio_payload_types;
4924   std::set<int> video_payload_types;
4925   bool pt_demuxing_enabled_audio = true;
4926   bool pt_demuxing_enabled_video = true;
4927   for (auto& content_info : sdesc->description()->contents()) {
4928     // If this m= section isn't bundled, it's safe to demux by payload type
4929     // since other m= sections using the same payload type will also be using
4930     // different transports.
4931     if (!bundle_group || !bundle_group->HasContentName(content_info.name)) {
4932       continue;
4933     }
4934     if (content_info.rejected ||
4935         (source == cricket::ContentSource::CS_LOCAL &&
4936          !RtpTransceiverDirectionHasRecv(
4937              content_info.media_description()->direction())) ||
4938         (source == cricket::ContentSource::CS_REMOTE &&
4939          !RtpTransceiverDirectionHasSend(
4940              content_info.media_description()->direction()))) {
4941       // Ignore transceivers that are not receiving.
4942       continue;
4943     }
4944     switch (content_info.media_description()->type()) {
4945       case cricket::MediaType::MEDIA_TYPE_AUDIO: {
4946         const cricket::AudioContentDescription* audio_desc =
4947             content_info.media_description()->as_audio();
4948         for (const cricket::AudioCodec& audio : audio_desc->codecs()) {
4949           if (audio_payload_types.count(audio.id)) {
4950             // Two m= sections are using the same payload type, thus demuxing
4951             // by payload type is not possible.
4952             pt_demuxing_enabled_audio = false;
4953           }
4954           audio_payload_types.insert(audio.id);
4955         }
4956         break;
4957       }
4958       case cricket::MediaType::MEDIA_TYPE_VIDEO: {
4959         const cricket::VideoContentDescription* video_desc =
4960             content_info.media_description()->as_video();
4961         for (const cricket::VideoCodec& video : video_desc->codecs()) {
4962           if (video_payload_types.count(video.id)) {
4963             // Two m= sections are using the same payload type, thus demuxing
4964             // by payload type is not possible.
4965             pt_demuxing_enabled_video = false;
4966           }
4967           video_payload_types.insert(video.id);
4968         }
4969         break;
4970       }
4971       default:
4972         // Ignore data channels.
4973         continue;
4974     }
4975   }
4976 
4977   // Gather all updates ahead of time so that all channels can be updated in a
4978   // single Invoke; necessary due to thread guards.
4979   std::vector<std::pair<RtpTransceiverDirection, cricket::ChannelInterface*>>
4980       channels_to_update;
4981   for (const auto& transceiver : transceivers()->List()) {
4982     cricket::ChannelInterface* channel = transceiver->internal()->channel();
4983     const ContentInfo* content =
4984         FindMediaSectionForTransceiver(transceiver, sdesc);
4985     if (!channel || !content) {
4986       continue;
4987     }
4988     RtpTransceiverDirection local_direction =
4989         content->media_description()->direction();
4990     if (source == cricket::CS_REMOTE) {
4991       local_direction = RtpTransceiverDirectionReversed(local_direction);
4992     }
4993     channels_to_update.emplace_back(local_direction,
4994                                     transceiver->internal()->channel());
4995   }
4996 
4997   if (channels_to_update.empty()) {
4998     return true;
4999   }
5000   return pc_->worker_thread()->Invoke<bool>(
5001       RTC_FROM_HERE, [&channels_to_update, bundle_group,
5002                       pt_demuxing_enabled_audio, pt_demuxing_enabled_video]() {
5003         for (const auto& it : channels_to_update) {
5004           RtpTransceiverDirection local_direction = it.first;
5005           cricket::ChannelInterface* channel = it.second;
5006           cricket::MediaType media_type = channel->media_type();
5007           bool in_bundle_group = (bundle_group && bundle_group->HasContentName(
5008                                                       channel->content_name()));
5009           if (media_type == cricket::MediaType::MEDIA_TYPE_AUDIO) {
5010             if (!channel->SetPayloadTypeDemuxingEnabled(
5011                     (!in_bundle_group || pt_demuxing_enabled_audio) &&
5012                     RtpTransceiverDirectionHasRecv(local_direction))) {
5013               return false;
5014             }
5015           } else if (media_type == cricket::MediaType::MEDIA_TYPE_VIDEO) {
5016             if (!channel->SetPayloadTypeDemuxingEnabled(
5017                     (!in_bundle_group || pt_demuxing_enabled_video) &&
5018                     RtpTransceiverDirectionHasRecv(local_direction))) {
5019               return false;
5020             }
5021           }
5022         }
5023         return true;
5024       });
5025 }
5026 
5027 }  // namespace webrtc
5028