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