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