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