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