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 #ifndef PC_SDP_OFFER_ANSWER_H_
12 #define PC_SDP_OFFER_ANSWER_H_
13 
14 #include <stddef.h>
15 #include <stdint.h>
16 
17 #include <functional>
18 #include <map>
19 #include <memory>
20 #include <set>
21 #include <string>
22 #include <utility>
23 #include <vector>
24 
25 #include "absl/types/optional.h"
26 #include "api/audio_options.h"
27 #include "api/candidate.h"
28 #include "api/jsep.h"
29 #include "api/jsep_ice_candidate.h"
30 #include "api/media_stream_interface.h"
31 #include "api/media_types.h"
32 #include "api/peer_connection_interface.h"
33 #include "api/rtc_error.h"
34 #include "api/rtp_transceiver_direction.h"
35 #include "api/rtp_transceiver_interface.h"
36 #include "api/scoped_refptr.h"
37 #include "api/sequence_checker.h"
38 #include "api/set_local_description_observer_interface.h"
39 #include "api/set_remote_description_observer_interface.h"
40 #include "api/transport/data_channel_transport_interface.h"
41 #include "api/turn_customizer.h"
42 #include "api/uma_metrics.h"
43 #include "api/video/video_bitrate_allocator_factory.h"
44 #include "media/base/media_channel.h"
45 #include "media/base/stream_params.h"
46 #include "p2p/base/port_allocator.h"
47 #include "pc/channel.h"
48 #include "pc/channel_interface.h"
49 #include "pc/channel_manager.h"
50 #include "pc/data_channel_controller.h"
51 #include "pc/ice_server_parsing.h"
52 #include "pc/jsep_transport_controller.h"
53 #include "pc/media_session.h"
54 #include "pc/media_stream_observer.h"
55 #include "pc/peer_connection_factory.h"
56 #include "pc/peer_connection_internal.h"
57 #include "pc/rtc_stats_collector.h"
58 #include "pc/rtp_receiver.h"
59 #include "pc/rtp_sender.h"
60 #include "pc/rtp_transceiver.h"
61 #include "pc/rtp_transmission_manager.h"
62 #include "pc/sctp_transport.h"
63 #include "pc/sdp_state_provider.h"
64 #include "pc/session_description.h"
65 #include "pc/stats_collector.h"
66 #include "pc/stream_collection.h"
67 #include "pc/transceiver_list.h"
68 #include "pc/webrtc_session_description_factory.h"
69 #include "rtc_base/checks.h"
70 #include "rtc_base/experiments/field_trial_parser.h"
71 #include "rtc_base/operations_chain.h"
72 #include "rtc_base/race_checker.h"
73 #include "rtc_base/rtc_certificate.h"
74 #include "rtc_base/ssl_stream_adapter.h"
75 #include "rtc_base/third_party/sigslot/sigslot.h"
76 #include "rtc_base/thread.h"
77 #include "rtc_base/thread_annotations.h"
78 #include "rtc_base/unique_id_generator.h"
79 #include "rtc_base/weak_ptr.h"
80 
81 namespace webrtc {
82 
83 // SdpOfferAnswerHandler is a component
84 // of the PeerConnection object as defined
85 // by the PeerConnectionInterface API surface.
86 // The class is responsible for the following:
87 // - Parsing and interpreting SDP.
88 // - Generating offers and answers based on the current state.
89 // This class lives on the signaling thread.
90 class SdpOfferAnswerHandler : public SdpStateProvider,
91                               public sigslot::has_slots<> {
92  public:
93   ~SdpOfferAnswerHandler();
94 
95   // Creates an SdpOfferAnswerHandler. Modifies dependencies.
96   static std::unique_ptr<SdpOfferAnswerHandler> Create(
97       PeerConnection* pc,
98       const PeerConnectionInterface::RTCConfiguration& configuration,
99       PeerConnectionDependencies& dependencies);
100 
ResetSessionDescFactory()101   void ResetSessionDescFactory() {
102     RTC_DCHECK_RUN_ON(signaling_thread());
103     webrtc_session_desc_factory_.reset();
104   }
webrtc_session_desc_factory()105   const WebRtcSessionDescriptionFactory* webrtc_session_desc_factory() const {
106     RTC_DCHECK_RUN_ON(signaling_thread());
107     return webrtc_session_desc_factory_.get();
108   }
109 
110   // Change signaling state to Closed, and perform appropriate actions.
111   void Close();
112 
113   // Called as part of destroying the owning PeerConnection.
114   void PrepareForShutdown();
115 
116   // Implementation of SdpStateProvider
117   PeerConnectionInterface::SignalingState signaling_state() const override;
118 
119   const SessionDescriptionInterface* local_description() const override;
120   const SessionDescriptionInterface* remote_description() const override;
121   const SessionDescriptionInterface* current_local_description() const override;
122   const SessionDescriptionInterface* current_remote_description()
123       const override;
124   const SessionDescriptionInterface* pending_local_description() const override;
125   const SessionDescriptionInterface* pending_remote_description()
126       const override;
127 
128   bool NeedsIceRestart(const std::string& content_name) const override;
129   bool IceRestartPending(const std::string& content_name) const override;
130   absl::optional<rtc::SSLRole> GetDtlsRole(
131       const std::string& mid) const override;
132 
133   void RestartIce();
134 
135   // JSEP01
136   void CreateOffer(
137       CreateSessionDescriptionObserver* observer,
138       const PeerConnectionInterface::RTCOfferAnswerOptions& options);
139   void CreateAnswer(
140       CreateSessionDescriptionObserver* observer,
141       const PeerConnectionInterface::RTCOfferAnswerOptions& options);
142 
143   void SetLocalDescription(
144       std::unique_ptr<SessionDescriptionInterface> desc,
145       rtc::scoped_refptr<SetLocalDescriptionObserverInterface> observer);
146   void SetLocalDescription(
147       rtc::scoped_refptr<SetLocalDescriptionObserverInterface> observer);
148   void SetLocalDescription(SetSessionDescriptionObserver* observer,
149                            SessionDescriptionInterface* desc);
150   void SetLocalDescription(SetSessionDescriptionObserver* observer);
151 
152   void SetRemoteDescription(
153       std::unique_ptr<SessionDescriptionInterface> desc,
154       rtc::scoped_refptr<SetRemoteDescriptionObserverInterface> observer);
155   void SetRemoteDescription(SetSessionDescriptionObserver* observer,
156                             SessionDescriptionInterface* desc);
157 
158   PeerConnectionInterface::RTCConfiguration GetConfiguration();
159   RTCError SetConfiguration(
160       const PeerConnectionInterface::RTCConfiguration& configuration);
161   bool AddIceCandidate(const IceCandidateInterface* candidate);
162   void AddIceCandidate(std::unique_ptr<IceCandidateInterface> candidate,
163                        std::function<void(RTCError)> callback);
164   bool RemoveIceCandidates(const std::vector<cricket::Candidate>& candidates);
165   // Adds a locally generated candidate to the local description.
166   void AddLocalIceCandidate(const JsepIceCandidate* candidate);
167   void RemoveLocalIceCandidates(
168       const std::vector<cricket::Candidate>& candidates);
169   bool ShouldFireNegotiationNeededEvent(uint32_t event_id);
170 
171   bool AddStream(MediaStreamInterface* local_stream);
172   void RemoveStream(MediaStreamInterface* local_stream);
173 
174   absl::optional<bool> is_caller();
175   bool HasNewIceCredentials();
176   void UpdateNegotiationNeeded();
177 
178   // Returns the media section in the given session description that is
179   // associated with the RtpTransceiver. Returns null if none found or this
180   // RtpTransceiver is not associated. Logic varies depending on the
181   // SdpSemantics specified in the configuration.
182   const cricket::ContentInfo* FindMediaSectionForTransceiver(
183       rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
184           transceiver,
185       const SessionDescriptionInterface* sdesc) const;
186 
187   // Destroys all BaseChannels and destroys the SCTP data channel, if present.
188   void DestroyAllChannels();
189 
190   rtc::scoped_refptr<StreamCollectionInterface> local_streams();
191   rtc::scoped_refptr<StreamCollectionInterface> remote_streams();
192 
193  private:
194   class ImplicitCreateSessionDescriptionObserver;
195 
196   friend class ImplicitCreateSessionDescriptionObserver;
197   class SetSessionDescriptionObserverAdapter;
198 
199   friend class SetSessionDescriptionObserverAdapter;
200 
201   enum class SessionError {
202     kNone,       // No error.
203     kContent,    // Error in BaseChannel SetLocalContent/SetRemoteContent.
204     kTransport,  // Error from the underlying transport.
205   };
206 
207   // Represents the [[LocalIceCredentialsToReplace]] internal slot in the spec.
208   // It makes the next CreateOffer() produce new ICE credentials even if
209   // RTCOfferAnswerOptions::ice_restart is false.
210   // https://w3c.github.io/webrtc-pc/#dfn-localufragstoreplace
211   // TODO(hbos): When JsepTransportController/JsepTransport supports rollback,
212   // move this type of logic to JsepTransportController/JsepTransport.
213   class LocalIceCredentialsToReplace;
214 
215   // Only called by the Create() function.
216   explicit SdpOfferAnswerHandler(PeerConnection* pc);
217   // Called from the `Create()` function. Can only be called
218   // once. Modifies dependencies.
219   void Initialize(
220       const PeerConnectionInterface::RTCConfiguration& configuration,
221       PeerConnectionDependencies& dependencies);
222 
223   rtc::Thread* signaling_thread() const;
224   // Non-const versions of local_description()/remote_description(), for use
225   // internally.
mutable_local_description()226   SessionDescriptionInterface* mutable_local_description()
227       RTC_RUN_ON(signaling_thread()) {
228     return pending_local_description_ ? pending_local_description_.get()
229                                       : current_local_description_.get();
230   }
mutable_remote_description()231   SessionDescriptionInterface* mutable_remote_description()
232       RTC_RUN_ON(signaling_thread()) {
233     return pending_remote_description_ ? pending_remote_description_.get()
234                                        : current_remote_description_.get();
235   }
236 
237   // Synchronous implementations of SetLocalDescription/SetRemoteDescription
238   // that return an RTCError instead of invoking a callback.
239   RTCError ApplyLocalDescription(
240       std::unique_ptr<SessionDescriptionInterface> desc);
241   RTCError ApplyRemoteDescription(
242       std::unique_ptr<SessionDescriptionInterface> desc);
243 
244   // Implementation of the offer/answer exchange operations. These are chained
245   // onto the |operations_chain_| when the public CreateOffer(), CreateAnswer(),
246   // SetLocalDescription() and SetRemoteDescription() methods are invoked.
247   void DoCreateOffer(
248       const PeerConnectionInterface::RTCOfferAnswerOptions& options,
249       rtc::scoped_refptr<CreateSessionDescriptionObserver> observer);
250   void DoCreateAnswer(
251       const PeerConnectionInterface::RTCOfferAnswerOptions& options,
252       rtc::scoped_refptr<CreateSessionDescriptionObserver> observer);
253   void DoSetLocalDescription(
254       std::unique_ptr<SessionDescriptionInterface> desc,
255       rtc::scoped_refptr<SetLocalDescriptionObserverInterface> observer);
256   void DoSetRemoteDescription(
257       std::unique_ptr<SessionDescriptionInterface> desc,
258       rtc::scoped_refptr<SetRemoteDescriptionObserverInterface> observer);
259 
260   // Update the state, signaling if necessary.
261   void ChangeSignalingState(
262       PeerConnectionInterface::SignalingState signaling_state);
263 
264   RTCError UpdateSessionState(SdpType type,
265                               cricket::ContentSource source,
266                               const cricket::SessionDescription* description);
267 
268   bool IsUnifiedPlan() const RTC_RUN_ON(signaling_thread());
269 
270   // Signals from MediaStreamObserver.
271   void OnAudioTrackAdded(AudioTrackInterface* track,
272                          MediaStreamInterface* stream)
273       RTC_RUN_ON(signaling_thread());
274   void OnAudioTrackRemoved(AudioTrackInterface* track,
275                            MediaStreamInterface* stream)
276       RTC_RUN_ON(signaling_thread());
277   void OnVideoTrackAdded(VideoTrackInterface* track,
278                          MediaStreamInterface* stream)
279       RTC_RUN_ON(signaling_thread());
280   void OnVideoTrackRemoved(VideoTrackInterface* track,
281                            MediaStreamInterface* stream)
282       RTC_RUN_ON(signaling_thread());
283 
284   // | desc_type | is the type of the description that caused the rollback.
285   RTCError Rollback(SdpType desc_type);
286   void OnOperationsChainEmpty();
287 
288   // Runs the algorithm **set the associated remote streams** specified in
289   // https://w3c.github.io/webrtc-pc/#set-associated-remote-streams.
290   void SetAssociatedRemoteStreams(
291       rtc::scoped_refptr<RtpReceiverInternal> receiver,
292       const std::vector<std::string>& stream_ids,
293       std::vector<rtc::scoped_refptr<MediaStreamInterface>>* added_streams,
294       std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams);
295 
296   bool CheckIfNegotiationIsNeeded();
297   void GenerateNegotiationNeededEvent();
298   // Helper method which verifies SDP.
299   RTCError ValidateSessionDescription(const SessionDescriptionInterface* sdesc,
300                                       cricket::ContentSource source)
301       RTC_RUN_ON(signaling_thread());
302 
303   // Updates the local RtpTransceivers according to the JSEP rules. Called as
304   // part of setting the local/remote description.
305   RTCError UpdateTransceiversAndDataChannels(
306       cricket::ContentSource source,
307       const SessionDescriptionInterface& new_session,
308       const SessionDescriptionInterface* old_local_description,
309       const SessionDescriptionInterface* old_remote_description);
310 
311   // Associate the given transceiver according to the JSEP rules.
312   RTCErrorOr<
313       rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>>
314   AssociateTransceiver(cricket::ContentSource source,
315                        SdpType type,
316                        size_t mline_index,
317                        const cricket::ContentInfo& content,
318                        const cricket::ContentInfo* old_local_content,
319                        const cricket::ContentInfo* old_remote_content)
320       RTC_RUN_ON(signaling_thread());
321 
322   // If the BUNDLE policy is max-bundle, then we know for sure that all
323   // transports will be bundled from the start. This method returns the BUNDLE
324   // group if that's the case, or null if BUNDLE will be negotiated later. An
325   // error is returned if max-bundle is specified but the session description
326   // does not have a BUNDLE group.
327   RTCErrorOr<const cricket::ContentGroup*> GetEarlyBundleGroup(
328       const cricket::SessionDescription& desc) const
329       RTC_RUN_ON(signaling_thread());
330 
331   // Either creates or destroys the transceiver's BaseChannel according to the
332   // given media section.
333   RTCError UpdateTransceiverChannel(
334       rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
335           transceiver,
336       const cricket::ContentInfo& content,
337       const cricket::ContentGroup* bundle_group) RTC_RUN_ON(signaling_thread());
338 
339   // Either creates or destroys the local data channel according to the given
340   // media section.
341   RTCError UpdateDataChannel(cricket::ContentSource source,
342                              const cricket::ContentInfo& content,
343                              const cricket::ContentGroup* bundle_group)
344       RTC_RUN_ON(signaling_thread());
345   // Check if a call to SetLocalDescription is acceptable with a session
346   // description of the given type.
347   bool ExpectSetLocalDescription(SdpType type);
348   // Check if a call to SetRemoteDescription is acceptable with a session
349   // description of the given type.
350   bool ExpectSetRemoteDescription(SdpType type);
351 
352   // The offer/answer machinery assumes the media section MID is present and
353   // unique. To support legacy end points that do not supply a=mid lines, this
354   // method will modify the session description to add MIDs generated according
355   // to the SDP semantics.
356   void FillInMissingRemoteMids(cricket::SessionDescription* remote_description);
357 
358   // Returns an RtpTransciever, if available, that can be used to receive the
359   // given media type according to JSEP rules.
360   rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
361   FindAvailableTransceiverToReceive(cricket::MediaType media_type) const;
362 
363   // Returns a MediaSessionOptions struct with options decided by |options|,
364   // the local MediaStreams and DataChannels.
365   void GetOptionsForOffer(const PeerConnectionInterface::RTCOfferAnswerOptions&
366                               offer_answer_options,
367                           cricket::MediaSessionOptions* session_options);
368   void GetOptionsForPlanBOffer(
369       const PeerConnectionInterface::RTCOfferAnswerOptions&
370           offer_answer_options,
371       cricket::MediaSessionOptions* session_options)
372       RTC_RUN_ON(signaling_thread());
373   void GetOptionsForUnifiedPlanOffer(
374       const PeerConnectionInterface::RTCOfferAnswerOptions&
375           offer_answer_options,
376       cricket::MediaSessionOptions* session_options)
377       RTC_RUN_ON(signaling_thread());
378 
379   // Returns a MediaSessionOptions struct with options decided by
380   // |constraints|, the local MediaStreams and DataChannels.
381   void GetOptionsForAnswer(const PeerConnectionInterface::RTCOfferAnswerOptions&
382                                offer_answer_options,
383                            cricket::MediaSessionOptions* session_options);
384   void GetOptionsForPlanBAnswer(
385       const PeerConnectionInterface::RTCOfferAnswerOptions&
386           offer_answer_options,
387       cricket::MediaSessionOptions* session_options)
388       RTC_RUN_ON(signaling_thread());
389   void GetOptionsForUnifiedPlanAnswer(
390       const PeerConnectionInterface::RTCOfferAnswerOptions&
391           offer_answer_options,
392       cricket::MediaSessionOptions* session_options)
393       RTC_RUN_ON(signaling_thread());
394 
395   const char* SessionErrorToString(SessionError error) const;
396   std::string GetSessionErrorMsg();
397   // Returns the last error in the session. See the enum above for details.
session_error()398   SessionError session_error() const {
399     RTC_DCHECK_RUN_ON(signaling_thread());
400     return session_error_;
401   }
session_error_desc()402   const std::string& session_error_desc() const { return session_error_desc_; }
403 
404   RTCError HandleLegacyOfferOptions(
405       const PeerConnectionInterface::RTCOfferAnswerOptions& options);
406   void RemoveRecvDirectionFromReceivingTransceiversOfType(
407       cricket::MediaType media_type) RTC_RUN_ON(signaling_thread());
408   void AddUpToOneReceivingTransceiverOfType(cricket::MediaType media_type);
409 
410   std::vector<
411       rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>>
412   GetReceivingTransceiversOfType(cricket::MediaType media_type)
413       RTC_RUN_ON(signaling_thread());
414 
415   // Runs the algorithm specified in
416   // https://w3c.github.io/webrtc-pc/#process-remote-track-removal
417   // This method will update the following lists:
418   // |remove_list| is the list of transceivers for which the receiving track is
419   //     being removed.
420   // |removed_streams| is the list of streams which no longer have a receiving
421   //     track so should be removed.
422   void ProcessRemovalOfRemoteTrack(
423       rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
424           transceiver,
425       std::vector<rtc::scoped_refptr<RtpTransceiverInterface>>* remove_list,
426       std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams);
427 
428   void RemoveRemoteStreamsIfEmpty(
429       const std::vector<rtc::scoped_refptr<MediaStreamInterface>>&
430           remote_streams,
431       std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams);
432 
433   // Remove all local and remote senders of type |media_type|.
434   // Called when a media type is rejected (m-line set to port 0).
435   void RemoveSenders(cricket::MediaType media_type);
436 
437   // Loops through the vector of |streams| and finds added and removed
438   // StreamParams since last time this method was called.
439   // For each new or removed StreamParam, OnLocalSenderSeen or
440   // OnLocalSenderRemoved is invoked.
441   void UpdateLocalSenders(const std::vector<cricket::StreamParams>& streams,
442                           cricket::MediaType media_type);
443 
444   // Makes sure a MediaStreamTrack is created for each StreamParam in |streams|,
445   // and existing MediaStreamTracks are removed if there is no corresponding
446   // StreamParam. If |default_track_needed| is true, a default MediaStreamTrack
447   // is created if it doesn't exist; if false, it's removed if it exists.
448   // |media_type| is the type of the |streams| and can be either audio or video.
449   // If a new MediaStream is created it is added to |new_streams|.
450   void UpdateRemoteSendersList(
451       const std::vector<cricket::StreamParams>& streams,
452       bool default_track_needed,
453       cricket::MediaType media_type,
454       StreamCollection* new_streams);
455 
456   // Enables media channels to allow sending of media.
457   // This enables media to flow on all configured audio/video channels and the
458   // RtpDataChannel.
459   void EnableSending();
460   // Push the media parts of the local or remote session description
461   // down to all of the channels.
462   RTCError PushdownMediaDescription(SdpType type,
463                                     cricket::ContentSource source);
464 
465   RTCError PushdownTransportDescription(cricket::ContentSource source,
466                                         SdpType type);
467   // Helper function to remove stopped transceivers.
468   void RemoveStoppedTransceivers();
469   // Deletes the corresponding channel of contents that don't exist in |desc|.
470   // |desc| can be null. This means that all channels are deleted.
471   void RemoveUnusedChannels(const cricket::SessionDescription* desc);
472 
473   // Report inferred negotiated SDP semantics from a local/remote answer to the
474   // UMA observer.
475   void ReportNegotiatedSdpSemantics(const SessionDescriptionInterface& answer);
476 
477   // Finds remote MediaStreams without any tracks and removes them from
478   // |remote_streams_| and notifies the observer that the MediaStreams no longer
479   // exist.
480   void UpdateEndedRemoteMediaStreams();
481 
482   // Uses all remote candidates in |remote_desc| in this session.
483   bool UseCandidatesInSessionDescription(
484       const SessionDescriptionInterface* remote_desc);
485   // Uses |candidate| in this session.
486   bool UseCandidate(const IceCandidateInterface* candidate);
487   // Returns true if we are ready to push down the remote candidate.
488   // |remote_desc| is the new remote description, or NULL if the current remote
489   // description should be used. Output |valid| is true if the candidate media
490   // index is valid.
491   bool ReadyToUseRemoteCandidate(const IceCandidateInterface* candidate,
492                                  const SessionDescriptionInterface* remote_desc,
493                                  bool* valid);
494 
495   RTCErrorOr<const cricket::ContentInfo*> FindContentInfo(
496       const SessionDescriptionInterface* description,
497       const IceCandidateInterface* candidate) RTC_RUN_ON(signaling_thread());
498 
499   // Functions for dealing with transports.
500   // Note that cricket code uses the term "channel" for what other code
501   // refers to as "transport".
502 
503   // Allocates media channels based on the |desc|. If |desc| doesn't have
504   // the BUNDLE option, this method will disable BUNDLE in PortAllocator.
505   // This method will also delete any existing media channels before creating.
506   RTCError CreateChannels(const cricket::SessionDescription& desc);
507 
508   // Helper methods to create media channels.
509   cricket::VoiceChannel* CreateVoiceChannel(const std::string& mid);
510   cricket::VideoChannel* CreateVideoChannel(const std::string& mid);
511   bool CreateDataChannel(const std::string& mid);
512 
513   // Destroys and clears the BaseChannel associated with the given transceiver,
514   // if such channel is set.
515   void DestroyTransceiverChannel(
516       rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
517           transceiver);
518 
519   // Destroys the RTP data channel transport and/or the SCTP data channel
520   // transport and clears it.
521   void DestroyDataChannelTransport();
522 
523   // Destroys the given ChannelInterface.
524   // The channel cannot be accessed after this method is called.
525   void DestroyChannelInterface(cricket::ChannelInterface* channel);
526   // Generates MediaDescriptionOptions for the |session_opts| based on existing
527   // local description or remote description.
528 
529   void GenerateMediaDescriptionOptions(
530       const SessionDescriptionInterface* session_desc,
531       RtpTransceiverDirection audio_direction,
532       RtpTransceiverDirection video_direction,
533       absl::optional<size_t>* audio_index,
534       absl::optional<size_t>* video_index,
535       absl::optional<size_t>* data_index,
536       cricket::MediaSessionOptions* session_options);
537 
538   // Generates the active MediaDescriptionOptions for the local data channel
539   // given the specified MID.
540   cricket::MediaDescriptionOptions GetMediaDescriptionOptionsForActiveData(
541       const std::string& mid) const;
542 
543   // Generates the rejected MediaDescriptionOptions for the local data channel
544   // given the specified MID.
545   cricket::MediaDescriptionOptions GetMediaDescriptionOptionsForRejectedData(
546       const std::string& mid) const;
547 
548   // Based on number of transceivers per media type, enabled or disable
549   // payload type based demuxing in the affected channels.
550   bool UpdatePayloadTypeDemuxingState(cricket::ContentSource source);
551 
552   // ==================================================================
553   // Access to pc_ variables
554   cricket::ChannelManager* channel_manager() const;
555   TransceiverList* transceivers();
556   const TransceiverList* transceivers() const;
557   DataChannelController* data_channel_controller();
558   const DataChannelController* data_channel_controller() const;
559   cricket::PortAllocator* port_allocator();
560   const cricket::PortAllocator* port_allocator() const;
561   RtpTransmissionManager* rtp_manager();
562   const RtpTransmissionManager* rtp_manager() const;
563   JsepTransportController* transport_controller();
564   const JsepTransportController* transport_controller() const;
565   // ===================================================================
audio_options()566   const cricket::AudioOptions& audio_options() { return audio_options_; }
video_options()567   const cricket::VideoOptions& video_options() { return video_options_; }
568 
569   PeerConnection* const pc_;
570 
571   std::unique_ptr<WebRtcSessionDescriptionFactory> webrtc_session_desc_factory_
572       RTC_GUARDED_BY(signaling_thread());
573 
574   std::unique_ptr<SessionDescriptionInterface> current_local_description_
575       RTC_GUARDED_BY(signaling_thread());
576   std::unique_ptr<SessionDescriptionInterface> pending_local_description_
577       RTC_GUARDED_BY(signaling_thread());
578   std::unique_ptr<SessionDescriptionInterface> current_remote_description_
579       RTC_GUARDED_BY(signaling_thread());
580   std::unique_ptr<SessionDescriptionInterface> pending_remote_description_
581       RTC_GUARDED_BY(signaling_thread());
582 
583   PeerConnectionInterface::SignalingState signaling_state_
584       RTC_GUARDED_BY(signaling_thread()) = PeerConnectionInterface::kStable;
585 
586   // Whether this peer is the caller. Set when the local description is applied.
587   absl::optional<bool> is_caller_ RTC_GUARDED_BY(signaling_thread());
588 
589   // Streams added via AddStream.
590   const rtc::scoped_refptr<StreamCollection> local_streams_
591       RTC_GUARDED_BY(signaling_thread());
592   // Streams created as a result of SetRemoteDescription.
593   const rtc::scoped_refptr<StreamCollection> remote_streams_
594       RTC_GUARDED_BY(signaling_thread());
595 
596   std::vector<std::unique_ptr<MediaStreamObserver>> stream_observers_
597       RTC_GUARDED_BY(signaling_thread());
598 
599   // The operations chain is used by the offer/answer exchange methods to ensure
600   // they are executed in the right order. For example, if
601   // SetRemoteDescription() is invoked while CreateOffer() is still pending, the
602   // SRD operation will not start until CreateOffer() has completed. See
603   // https://w3c.github.io/webrtc-pc/#dfn-operations-chain.
604   rtc::scoped_refptr<rtc::OperationsChain> operations_chain_
605       RTC_GUARDED_BY(signaling_thread());
606 
607   // One PeerConnection has only one RTCP CNAME.
608   // https://tools.ietf.org/html/draft-ietf-rtcweb-rtp-usage-26#section-4.9
609   const std::string rtcp_cname_;
610 
611   // MIDs will be generated using this generator which will keep track of
612   // all the MIDs that have been seen over the life of the PeerConnection.
613   rtc::UniqueStringGenerator mid_generator_ RTC_GUARDED_BY(signaling_thread());
614 
615   // List of content names for which the remote side triggered an ICE restart.
616   std::set<std::string> pending_ice_restarts_
617       RTC_GUARDED_BY(signaling_thread());
618 
619   std::unique_ptr<LocalIceCredentialsToReplace>
620       local_ice_credentials_to_replace_ RTC_GUARDED_BY(signaling_thread());
621 
622   bool remote_peer_supports_msid_ RTC_GUARDED_BY(signaling_thread()) = false;
623   bool is_negotiation_needed_ RTC_GUARDED_BY(signaling_thread()) = false;
624   uint32_t negotiation_needed_event_id_ = 0;
625   bool update_negotiation_needed_on_empty_chain_
626       RTC_GUARDED_BY(signaling_thread()) = false;
627 
628   // In Unified Plan, if we encounter remote SDP that does not contain an a=msid
629   // line we create and use a stream with a random ID for our receivers. This is
630   // to support legacy endpoints that do not support the a=msid attribute (as
631   // opposed to streamless tracks with "a=msid:-").
632   rtc::scoped_refptr<MediaStreamInterface> missing_msid_default_stream_
633       RTC_GUARDED_BY(signaling_thread());
634 
635   // Used when rolling back RTP data channels.
636   bool have_pending_rtp_data_channel_ RTC_GUARDED_BY(signaling_thread()) =
637       false;
638 
639   // Updates the error state, signaling if necessary.
640   void SetSessionError(SessionError error, const std::string& error_desc);
641 
642   // Implements AddIceCandidate without reporting usage, but returns the
643   // particular success/error value that should be reported (and can be utilized
644   // for other purposes).
645   AddIceCandidateResult AddIceCandidateInternal(
646       const IceCandidateInterface* candidate);
647 
648   SessionError session_error_ RTC_GUARDED_BY(signaling_thread()) =
649       SessionError::kNone;
650   std::string session_error_desc_ RTC_GUARDED_BY(signaling_thread());
651 
652   // Member variables for caching global options.
653   cricket::AudioOptions audio_options_ RTC_GUARDED_BY(signaling_thread());
654   cricket::VideoOptions video_options_ RTC_GUARDED_BY(signaling_thread());
655 
656   // This object should be used to generate any SSRC that is not explicitly
657   // specified by the user (or by the remote party).
658   // The generator is not used directly, instead it is passed on to the
659   // channel manager and the session description factory.
660   rtc::UniqueRandomIdGenerator ssrc_generator_
661       RTC_GUARDED_BY(signaling_thread());
662 
663   // A video bitrate allocator factory.
664   // This can be injected using the PeerConnectionDependencies,
665   // or else the CreateBuiltinVideoBitrateAllocatorFactory() will be called.
666   // Note that one can still choose to override this in a MediaEngine
667   // if one wants too.
668   std::unique_ptr<webrtc::VideoBitrateAllocatorFactory>
669       video_bitrate_allocator_factory_;
670 
671   rtc::WeakPtrFactory<SdpOfferAnswerHandler> weak_ptr_factory_
672       RTC_GUARDED_BY(signaling_thread());
673 };
674 
675 }  // namespace webrtc
676 
677 #endif  // PC_SDP_OFFER_ANSWER_H_
678