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