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