blob: 4b14f20708c4839e6a52def9645c21e36f6d050e [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:
Harald Alvestrandcdcfab02020-09-28 13:02:07 +000091 ~SdpOfferAnswerHandler();
92
Harald Alvestrand9cd199d2020-10-27 07:10:43 +000093 // Creates an SdpOfferAnswerHandler. Modifies dependencies.
94 static std::unique_ptr<SdpOfferAnswerHandler> Create(
95 PeerConnection* pc,
Harald Alvestrand763f5a92020-10-22 10:39:40 +000096 const PeerConnectionInterface::RTCConfiguration& configuration,
Harald Alvestrand9cd199d2020-10-27 07:10:43 +000097 PeerConnectionDependencies& dependencies);
Harald Alvestrand763f5a92020-10-22 10:39:40 +000098
Harald Alvestrandcdcfab02020-09-28 13:02:07 +000099 void ResetSessionDescFactory() {
100 RTC_DCHECK_RUN_ON(signaling_thread());
101 webrtc_session_desc_factory_.reset();
102 }
103 const WebRtcSessionDescriptionFactory* webrtc_session_desc_factory() const {
104 RTC_DCHECK_RUN_ON(signaling_thread());
105 return webrtc_session_desc_factory_.get();
106 }
107
108 // Change signaling state to Closed, and perform appropriate actions.
109 void Close();
110
111 // Called as part of destroying the owning PeerConnection.
112 void PrepareForShutdown();
113
Harald Alvestrandf01bd6c2020-10-23 13:30:46 +0000114 // Implementation of SdpStateProvider
115 PeerConnectionInterface::SignalingState signaling_state() const override;
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000116
Harald Alvestrandf01bd6c2020-10-23 13:30:46 +0000117 const SessionDescriptionInterface* local_description() const override;
118 const SessionDescriptionInterface* remote_description() const override;
119 const SessionDescriptionInterface* current_local_description() const override;
120 const SessionDescriptionInterface* current_remote_description()
121 const override;
122 const SessionDescriptionInterface* pending_local_description() const override;
123 const SessionDescriptionInterface* pending_remote_description()
124 const override;
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000125
Harald Alvestrandf01bd6c2020-10-23 13:30:46 +0000126 bool NeedsIceRestart(const std::string& content_name) const override;
127 bool IceRestartPending(const std::string& content_name) const override;
128 absl::optional<rtc::SSLRole> GetDtlsRole(
129 const std::string& mid) const override;
Harald Alvestrandd89ce532020-10-21 08:59:22 +0000130
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000131 void RestartIce();
132
133 // JSEP01
134 void CreateOffer(
135 CreateSessionDescriptionObserver* observer,
136 const PeerConnectionInterface::RTCOfferAnswerOptions& options);
137 void CreateAnswer(
138 CreateSessionDescriptionObserver* observer,
139 const PeerConnectionInterface::RTCOfferAnswerOptions& options);
140
141 void SetLocalDescription(
142 std::unique_ptr<SessionDescriptionInterface> desc,
143 rtc::scoped_refptr<SetLocalDescriptionObserverInterface> observer);
144 void SetLocalDescription(
145 rtc::scoped_refptr<SetLocalDescriptionObserverInterface> observer);
146 void SetLocalDescription(SetSessionDescriptionObserver* observer,
147 SessionDescriptionInterface* desc);
148 void SetLocalDescription(SetSessionDescriptionObserver* observer);
149
150 void SetRemoteDescription(
151 std::unique_ptr<SessionDescriptionInterface> desc,
152 rtc::scoped_refptr<SetRemoteDescriptionObserverInterface> observer);
153 void SetRemoteDescription(SetSessionDescriptionObserver* observer,
154 SessionDescriptionInterface* desc);
155
156 PeerConnectionInterface::RTCConfiguration GetConfiguration();
157 RTCError SetConfiguration(
158 const PeerConnectionInterface::RTCConfiguration& configuration);
159 bool AddIceCandidate(const IceCandidateInterface* candidate);
160 void AddIceCandidate(std::unique_ptr<IceCandidateInterface> candidate,
161 std::function<void(RTCError)> callback);
162 bool RemoveIceCandidates(const std::vector<cricket::Candidate>& candidates);
163 // Adds a locally generated candidate to the local description.
164 void AddLocalIceCandidate(const JsepIceCandidate* candidate);
165 void RemoveLocalIceCandidates(
166 const std::vector<cricket::Candidate>& candidates);
167 bool ShouldFireNegotiationNeededEvent(uint32_t event_id);
168
Harald Alvestrand6f04b652020-10-09 11:42:17 +0000169 bool AddStream(MediaStreamInterface* local_stream);
170 void RemoveStream(MediaStreamInterface* local_stream);
171
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000172 absl::optional<bool> is_caller();
173 bool HasNewIceCredentials();
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000174 void UpdateNegotiationNeeded();
Harald Alvestrand75b9ab62020-09-30 22:17:18 +0000175 void SetHavePendingRtpDataChannel() {
176 RTC_DCHECK_RUN_ON(signaling_thread());
177 have_pending_rtp_data_channel_ = true;
178 }
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000179
Harald Alvestrandc06e3742020-10-01 10:23:33 +0000180 // Returns the media section in the given session description that is
181 // associated with the RtpTransceiver. Returns null if none found or this
182 // RtpTransceiver is not associated. Logic varies depending on the
183 // SdpSemantics specified in the configuration.
184 const cricket::ContentInfo* FindMediaSectionForTransceiver(
185 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
186 transceiver,
187 const SessionDescriptionInterface* sdesc) const;
188
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000189 // Destroys all BaseChannels and destroys the SCTP data channel, if present.
190 void DestroyAllChannels();
191
Harald Alvestrand6f04b652020-10-09 11:42:17 +0000192 rtc::scoped_refptr<StreamCollectionInterface> local_streams();
193 rtc::scoped_refptr<StreamCollectionInterface> remote_streams();
194
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000195 private:
196 class ImplicitCreateSessionDescriptionObserver;
Harald Alvestrand1f7eab62020-10-18 16:51:47 +0000197
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000198 friend class ImplicitCreateSessionDescriptionObserver;
199 class SetSessionDescriptionObserverAdapter;
Harald Alvestrand1f7eab62020-10-18 16:51:47 +0000200
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000201 friend class SetSessionDescriptionObserverAdapter;
202
Harald Alvestranda474fbf2020-10-01 16:47:23 +0000203 enum class SessionError {
204 kNone, // No error.
205 kContent, // Error in BaseChannel SetLocalContent/SetRemoteContent.
206 kTransport, // Error from the underlying transport.
207 };
208
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000209 // Represents the [[LocalIceCredentialsToReplace]] internal slot in the spec.
210 // It makes the next CreateOffer() produce new ICE credentials even if
211 // RTCOfferAnswerOptions::ice_restart is false.
212 // https://w3c.github.io/webrtc-pc/#dfn-localufragstoreplace
213 // TODO(hbos): When JsepTransportController/JsepTransport supports rollback,
214 // move this type of logic to JsepTransportController/JsepTransport.
215 class LocalIceCredentialsToReplace;
216
Harald Alvestrand9cd199d2020-10-27 07:10:43 +0000217 // Only called by the Create() function.
218 explicit SdpOfferAnswerHandler(PeerConnection* pc);
219 // Called from the `Create()` function. Can only be called
220 // once. Modifies dependencies.
221 void Initialize(
222 const PeerConnectionInterface::RTCConfiguration& configuration,
223 PeerConnectionDependencies& dependencies);
224
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000225 rtc::Thread* signaling_thread() const;
226 // Non-const versions of local_description()/remote_description(), for use
227 // internally.
228 SessionDescriptionInterface* mutable_local_description()
229 RTC_RUN_ON(signaling_thread()) {
230 return pending_local_description_ ? pending_local_description_.get()
231 : current_local_description_.get();
232 }
233 SessionDescriptionInterface* mutable_remote_description()
234 RTC_RUN_ON(signaling_thread()) {
235 return pending_remote_description_ ? pending_remote_description_.get()
236 : current_remote_description_.get();
237 }
238
239 // Synchronous implementations of SetLocalDescription/SetRemoteDescription
240 // that return an RTCError instead of invoking a callback.
241 RTCError ApplyLocalDescription(
242 std::unique_ptr<SessionDescriptionInterface> desc);
243 RTCError ApplyRemoteDescription(
244 std::unique_ptr<SessionDescriptionInterface> desc);
245
246 // Implementation of the offer/answer exchange operations. These are chained
247 // onto the |operations_chain_| when the public CreateOffer(), CreateAnswer(),
248 // SetLocalDescription() and SetRemoteDescription() methods are invoked.
249 void DoCreateOffer(
250 const PeerConnectionInterface::RTCOfferAnswerOptions& options,
251 rtc::scoped_refptr<CreateSessionDescriptionObserver> observer);
252 void DoCreateAnswer(
253 const PeerConnectionInterface::RTCOfferAnswerOptions& options,
254 rtc::scoped_refptr<CreateSessionDescriptionObserver> observer);
255 void DoSetLocalDescription(
256 std::unique_ptr<SessionDescriptionInterface> desc,
257 rtc::scoped_refptr<SetLocalDescriptionObserverInterface> observer);
258 void DoSetRemoteDescription(
259 std::unique_ptr<SessionDescriptionInterface> desc,
260 rtc::scoped_refptr<SetRemoteDescriptionObserverInterface> observer);
261
262 // Update the state, signaling if necessary.
263 void ChangeSignalingState(
264 PeerConnectionInterface::SignalingState signaling_state);
265
266 RTCError UpdateSessionState(SdpType type,
267 cricket::ContentSource source,
268 const cricket::SessionDescription* description);
269
270 bool IsUnifiedPlan() const RTC_RUN_ON(signaling_thread());
271
Harald Alvestrand763f5a92020-10-22 10:39:40 +0000272 // Signals from MediaStreamObserver.
273 void OnAudioTrackAdded(AudioTrackInterface* track,
274 MediaStreamInterface* stream)
275 RTC_RUN_ON(signaling_thread());
276 void OnAudioTrackRemoved(AudioTrackInterface* track,
277 MediaStreamInterface* stream)
278 RTC_RUN_ON(signaling_thread());
279 void OnVideoTrackAdded(VideoTrackInterface* track,
280 MediaStreamInterface* stream)
281 RTC_RUN_ON(signaling_thread());
282 void OnVideoTrackRemoved(VideoTrackInterface* track,
283 MediaStreamInterface* stream)
284 RTC_RUN_ON(signaling_thread());
285
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000286 // | desc_type | is the type of the description that caused the rollback.
287 RTCError Rollback(SdpType desc_type);
288 void OnOperationsChainEmpty();
289
290 // Runs the algorithm **set the associated remote streams** specified in
291 // https://w3c.github.io/webrtc-pc/#set-associated-remote-streams.
292 void SetAssociatedRemoteStreams(
293 rtc::scoped_refptr<RtpReceiverInternal> receiver,
294 const std::vector<std::string>& stream_ids,
295 std::vector<rtc::scoped_refptr<MediaStreamInterface>>* added_streams,
296 std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams);
297
298 bool CheckIfNegotiationIsNeeded();
299 void GenerateNegotiationNeededEvent();
300 // Helper method which verifies SDP.
301 RTCError ValidateSessionDescription(const SessionDescriptionInterface* sdesc,
302 cricket::ContentSource source)
303 RTC_RUN_ON(signaling_thread());
304
305 // Updates the local RtpTransceivers according to the JSEP rules. Called as
306 // part of setting the local/remote description.
307 RTCError UpdateTransceiversAndDataChannels(
308 cricket::ContentSource source,
309 const SessionDescriptionInterface& new_session,
310 const SessionDescriptionInterface* old_local_description,
311 const SessionDescriptionInterface* old_remote_description);
312
313 // Associate the given transceiver according to the JSEP rules.
314 RTCErrorOr<
315 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>>
316 AssociateTransceiver(cricket::ContentSource source,
317 SdpType type,
318 size_t mline_index,
319 const cricket::ContentInfo& content,
320 const cricket::ContentInfo* old_local_content,
321 const cricket::ContentInfo* old_remote_content)
322 RTC_RUN_ON(signaling_thread());
323
324 // If the BUNDLE policy is max-bundle, then we know for sure that all
325 // transports will be bundled from the start. This method returns the BUNDLE
326 // group if that's the case, or null if BUNDLE will be negotiated later. An
327 // error is returned if max-bundle is specified but the session description
328 // does not have a BUNDLE group.
329 RTCErrorOr<const cricket::ContentGroup*> GetEarlyBundleGroup(
330 const cricket::SessionDescription& desc) const
331 RTC_RUN_ON(signaling_thread());
332
333 // Either creates or destroys the transceiver's BaseChannel according to the
334 // given media section.
335 RTCError UpdateTransceiverChannel(
336 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
337 transceiver,
338 const cricket::ContentInfo& content,
339 const cricket::ContentGroup* bundle_group) RTC_RUN_ON(signaling_thread());
340
341 // Either creates or destroys the local data channel according to the given
342 // media section.
343 RTCError UpdateDataChannel(cricket::ContentSource source,
344 const cricket::ContentInfo& content,
345 const cricket::ContentGroup* bundle_group)
346 RTC_RUN_ON(signaling_thread());
Harald Alvestrandc06e3742020-10-01 10:23:33 +0000347 // Check if a call to SetLocalDescription is acceptable with a session
348 // description of the given type.
349 bool ExpectSetLocalDescription(SdpType type);
350 // Check if a call to SetRemoteDescription is acceptable with a session
351 // description of the given type.
352 bool ExpectSetRemoteDescription(SdpType type);
353
354 // The offer/answer machinery assumes the media section MID is present and
355 // unique. To support legacy end points that do not supply a=mid lines, this
356 // method will modify the session description to add MIDs generated according
357 // to the SDP semantics.
358 void FillInMissingRemoteMids(cricket::SessionDescription* remote_description);
359
360 // Returns an RtpTransciever, if available, that can be used to receive the
361 // given media type according to JSEP rules.
362 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
363 FindAvailableTransceiverToReceive(cricket::MediaType media_type) const;
364
365 // Returns a MediaSessionOptions struct with options decided by |options|,
366 // the local MediaStreams and DataChannels.
367 void GetOptionsForOffer(const PeerConnectionInterface::RTCOfferAnswerOptions&
368 offer_answer_options,
369 cricket::MediaSessionOptions* session_options);
370 void GetOptionsForPlanBOffer(
371 const PeerConnectionInterface::RTCOfferAnswerOptions&
372 offer_answer_options,
373 cricket::MediaSessionOptions* session_options)
374 RTC_RUN_ON(signaling_thread());
375 void GetOptionsForUnifiedPlanOffer(
376 const PeerConnectionInterface::RTCOfferAnswerOptions&
377 offer_answer_options,
378 cricket::MediaSessionOptions* session_options)
379 RTC_RUN_ON(signaling_thread());
380
381 // Returns a MediaSessionOptions struct with options decided by
382 // |constraints|, the local MediaStreams and DataChannels.
383 void GetOptionsForAnswer(const PeerConnectionInterface::RTCOfferAnswerOptions&
384 offer_answer_options,
385 cricket::MediaSessionOptions* session_options);
386 void GetOptionsForPlanBAnswer(
387 const PeerConnectionInterface::RTCOfferAnswerOptions&
388 offer_answer_options,
389 cricket::MediaSessionOptions* session_options)
390 RTC_RUN_ON(signaling_thread());
391 void GetOptionsForUnifiedPlanAnswer(
392 const PeerConnectionInterface::RTCOfferAnswerOptions&
393 offer_answer_options,
394 cricket::MediaSessionOptions* session_options)
395 RTC_RUN_ON(signaling_thread());
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000396
Harald Alvestranda474fbf2020-10-01 16:47:23 +0000397 const char* SessionErrorToString(SessionError error) const;
398 std::string GetSessionErrorMsg();
399 // Returns the last error in the session. See the enum above for details.
400 SessionError session_error() const {
401 RTC_DCHECK_RUN_ON(signaling_thread());
402 return session_error_;
403 }
404 const std::string& session_error_desc() const { return session_error_desc_; }
405
406 RTCError HandleLegacyOfferOptions(
407 const PeerConnectionInterface::RTCOfferAnswerOptions& options);
408 void RemoveRecvDirectionFromReceivingTransceiversOfType(
409 cricket::MediaType media_type) RTC_RUN_ON(signaling_thread());
410 void AddUpToOneReceivingTransceiverOfType(cricket::MediaType media_type);
411
412 std::vector<
413 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>>
414 GetReceivingTransceiversOfType(cricket::MediaType media_type)
415 RTC_RUN_ON(signaling_thread());
416
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000417 // Runs the algorithm specified in
418 // https://w3c.github.io/webrtc-pc/#process-remote-track-removal
Harald Alvestranda474fbf2020-10-01 16:47:23 +0000419 // This method will update the following lists:
420 // |remove_list| is the list of transceivers for which the receiving track is
421 // being removed.
422 // |removed_streams| is the list of streams which no longer have a receiving
423 // track so should be removed.
Harald Alvestranda474fbf2020-10-01 16:47:23 +0000424 void ProcessRemovalOfRemoteTrack(
425 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
426 transceiver,
427 std::vector<rtc::scoped_refptr<RtpTransceiverInterface>>* remove_list,
428 std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams);
429
430 void RemoveRemoteStreamsIfEmpty(
431 const std::vector<rtc::scoped_refptr<MediaStreamInterface>>&
432 remote_streams,
433 std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams);
434
435 // Remove all local and remote senders of type |media_type|.
436 // Called when a media type is rejected (m-line set to port 0).
437 void RemoveSenders(cricket::MediaType media_type);
438
439 // Loops through the vector of |streams| and finds added and removed
440 // StreamParams since last time this method was called.
441 // For each new or removed StreamParam, OnLocalSenderSeen or
442 // OnLocalSenderRemoved is invoked.
443 void UpdateLocalSenders(const std::vector<cricket::StreamParams>& streams,
444 cricket::MediaType media_type);
445
446 // Makes sure a MediaStreamTrack is created for each StreamParam in |streams|,
447 // and existing MediaStreamTracks are removed if there is no corresponding
448 // StreamParam. If |default_track_needed| is true, a default MediaStreamTrack
449 // is created if it doesn't exist; if false, it's removed if it exists.
450 // |media_type| is the type of the |streams| and can be either audio or video.
451 // If a new MediaStream is created it is added to |new_streams|.
452 void UpdateRemoteSendersList(
453 const std::vector<cricket::StreamParams>& streams,
454 bool default_track_needed,
455 cricket::MediaType media_type,
456 StreamCollection* new_streams);
457
Harald Alvestranda474fbf2020-10-01 16:47:23 +0000458 // Push the media parts of the local or remote session description
Taylor Brandstetterc1ad1ff2020-12-10 15:31:14 -0800459 // down to all of the channels, and enable sending if applicable.
Harald Alvestranda474fbf2020-10-01 16:47:23 +0000460 RTCError PushdownMediaDescription(SdpType type,
461 cricket::ContentSource source);
462
Taylor Brandstetterc1ad1ff2020-12-10 15:31:14 -0800463 struct PayloadTypeDemuxingUpdate {
464 PayloadTypeDemuxingUpdate(cricket::ChannelInterface* channel, bool enabled)
465 : channel(channel), enabled(enabled) {}
466 cricket::ChannelInterface* channel;
467 bool enabled;
468 };
469 struct ContentUpdate {
470 ContentUpdate(cricket::ChannelInterface* channel,
471 const cricket::MediaContentDescription* content_description)
472 : channel(channel), content_description(content_description) {}
473 cricket::ChannelInterface* channel;
474 const cricket::MediaContentDescription* content_description;
475 };
476 // Helper method used by PushdownMediaDescription to apply a batch of updates
477 // to BaseChannels on the worker thread.
478 RTCError ApplyChannelUpdates(
479 SdpType type,
480 cricket::ContentSource source,
481 std::vector<PayloadTypeDemuxingUpdate> payload_type_demuxing_updates,
482 std::vector<ContentUpdate> content_updates);
483
Harald Alvestranda474fbf2020-10-01 16:47:23 +0000484 RTCError PushdownTransportDescription(cricket::ContentSource source,
485 SdpType type);
486 // Helper function to remove stopped transceivers.
487 void RemoveStoppedTransceivers();
488 // Deletes the corresponding channel of contents that don't exist in |desc|.
489 // |desc| can be null. This means that all channels are deleted.
490 void RemoveUnusedChannels(const cricket::SessionDescription* desc);
491
492 // Report inferred negotiated SDP semantics from a local/remote answer to the
493 // UMA observer.
494 void ReportNegotiatedSdpSemantics(const SessionDescriptionInterface& answer);
495
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000496 // Finds remote MediaStreams without any tracks and removes them from
497 // |remote_streams_| and notifies the observer that the MediaStreams no longer
498 // exist.
499 void UpdateEndedRemoteMediaStreams();
500
501 // Uses all remote candidates in |remote_desc| in this session.
502 bool UseCandidatesInSessionDescription(
503 const SessionDescriptionInterface* remote_desc);
504 // Uses |candidate| in this session.
505 bool UseCandidate(const IceCandidateInterface* candidate);
506 // Returns true if we are ready to push down the remote candidate.
507 // |remote_desc| is the new remote description, or NULL if the current remote
508 // description should be used. Output |valid| is true if the candidate media
509 // index is valid.
510 bool ReadyToUseRemoteCandidate(const IceCandidateInterface* candidate,
511 const SessionDescriptionInterface* remote_desc,
512 bool* valid);
513 void ReportRemoteIceCandidateAdded(const cricket::Candidate& candidate)
514 RTC_RUN_ON(signaling_thread());
515
516 RTCErrorOr<const cricket::ContentInfo*> FindContentInfo(
517 const SessionDescriptionInterface* description,
518 const IceCandidateInterface* candidate) RTC_RUN_ON(signaling_thread());
519
520 // Functions for dealing with transports.
521 // Note that cricket code uses the term "channel" for what other code
522 // refers to as "transport".
523
524 // Allocates media channels based on the |desc|. If |desc| doesn't have
525 // the BUNDLE option, this method will disable BUNDLE in PortAllocator.
526 // This method will also delete any existing media channels before creating.
527 RTCError CreateChannels(const cricket::SessionDescription& desc);
528
529 // Helper methods to create media channels.
530 cricket::VoiceChannel* CreateVoiceChannel(const std::string& mid);
531 cricket::VideoChannel* CreateVideoChannel(const std::string& mid);
532 bool CreateDataChannel(const std::string& mid);
533
534 // Destroys and clears the BaseChannel associated with the given transceiver,
535 // if such channel is set.
536 void DestroyTransceiverChannel(
537 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
538 transceiver);
539
540 // Destroys the RTP data channel transport and/or the SCTP data channel
541 // transport and clears it.
542 void DestroyDataChannelTransport();
543
544 // Destroys the given ChannelInterface.
545 // The channel cannot be accessed after this method is called.
546 void DestroyChannelInterface(cricket::ChannelInterface* channel);
547 // Generates MediaDescriptionOptions for the |session_opts| based on existing
548 // local description or remote description.
549
550 void GenerateMediaDescriptionOptions(
551 const SessionDescriptionInterface* session_desc,
552 RtpTransceiverDirection audio_direction,
553 RtpTransceiverDirection video_direction,
554 absl::optional<size_t>* audio_index,
555 absl::optional<size_t>* video_index,
556 absl::optional<size_t>* data_index,
557 cricket::MediaSessionOptions* session_options);
558
559 // Generates the active MediaDescriptionOptions for the local data channel
560 // given the specified MID.
561 cricket::MediaDescriptionOptions GetMediaDescriptionOptionsForActiveData(
562 const std::string& mid) const;
563
564 // Generates the rejected MediaDescriptionOptions for the local data channel
565 // given the specified MID.
566 cricket::MediaDescriptionOptions GetMediaDescriptionOptionsForRejectedData(
567 const std::string& mid) const;
568
569 const std::string GetTransportName(const std::string& content_name);
Taylor Brandstetterc1ad1ff2020-12-10 15:31:14 -0800570
571 // Based on number of transceivers per media type, and their bundle status and
572 // payload types, determine whether payload type based demuxing should be
573 // enabled or disabled. Returns a list of channels and the corresponding
574 // value to be passed into SetPayloadTypeDemuxingEnabled, so that this action
575 // can be combined with other operations on the worker thread.
576 std::vector<PayloadTypeDemuxingUpdate> GetPayloadTypeDemuxingUpdates(
577 cricket::ContentSource source);
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000578
579 // ==================================================================
580 // Access to pc_ variables
581 cricket::ChannelManager* channel_manager() const;
Harald Alvestrande15fb152020-10-19 13:28:05 +0000582 TransceiverList* transceivers();
583 const TransceiverList* transceivers() const;
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000584 DataChannelController* data_channel_controller();
585 const DataChannelController* data_channel_controller() const;
586 cricket::PortAllocator* port_allocator();
587 const cricket::PortAllocator* port_allocator() const;
Harald Alvestrande15fb152020-10-19 13:28:05 +0000588 RtpTransmissionManager* rtp_manager();
589 const RtpTransmissionManager* rtp_manager() const;
Harald Alvestrandf01bd6c2020-10-23 13:30:46 +0000590 JsepTransportController* transport_controller();
591 const JsepTransportController* transport_controller() const;
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000592 // ===================================================================
Harald Alvestrand763f5a92020-10-22 10:39:40 +0000593 const cricket::AudioOptions& audio_options() { return audio_options_; }
594 const cricket::VideoOptions& video_options() { return video_options_; }
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000595
596 PeerConnection* const pc_;
597
598 std::unique_ptr<WebRtcSessionDescriptionFactory> webrtc_session_desc_factory_
599 RTC_GUARDED_BY(signaling_thread());
600
601 std::unique_ptr<SessionDescriptionInterface> current_local_description_
602 RTC_GUARDED_BY(signaling_thread());
603 std::unique_ptr<SessionDescriptionInterface> pending_local_description_
604 RTC_GUARDED_BY(signaling_thread());
605 std::unique_ptr<SessionDescriptionInterface> current_remote_description_
606 RTC_GUARDED_BY(signaling_thread());
607 std::unique_ptr<SessionDescriptionInterface> pending_remote_description_
608 RTC_GUARDED_BY(signaling_thread());
609
610 PeerConnectionInterface::SignalingState signaling_state_
611 RTC_GUARDED_BY(signaling_thread()) = PeerConnectionInterface::kStable;
612
613 // Whether this peer is the caller. Set when the local description is applied.
614 absl::optional<bool> is_caller_ RTC_GUARDED_BY(signaling_thread());
615
Harald Alvestrand6f04b652020-10-09 11:42:17 +0000616 // Streams added via AddStream.
617 const rtc::scoped_refptr<StreamCollection> local_streams_
618 RTC_GUARDED_BY(signaling_thread());
619 // Streams created as a result of SetRemoteDescription.
620 const rtc::scoped_refptr<StreamCollection> remote_streams_
621 RTC_GUARDED_BY(signaling_thread());
622
623 std::vector<std::unique_ptr<MediaStreamObserver>> stream_observers_
624 RTC_GUARDED_BY(signaling_thread());
625
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000626 // The operations chain is used by the offer/answer exchange methods to ensure
627 // they are executed in the right order. For example, if
628 // SetRemoteDescription() is invoked while CreateOffer() is still pending, the
629 // SRD operation will not start until CreateOffer() has completed. See
630 // https://w3c.github.io/webrtc-pc/#dfn-operations-chain.
631 rtc::scoped_refptr<rtc::OperationsChain> operations_chain_
632 RTC_GUARDED_BY(signaling_thread());
633
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000634 // One PeerConnection has only one RTCP CNAME.
635 // https://tools.ietf.org/html/draft-ietf-rtcweb-rtp-usage-26#section-4.9
636 const std::string rtcp_cname_;
637
638 // MIDs will be generated using this generator which will keep track of
639 // all the MIDs that have been seen over the life of the PeerConnection.
640 rtc::UniqueStringGenerator mid_generator_ RTC_GUARDED_BY(signaling_thread());
641
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000642 // List of content names for which the remote side triggered an ICE restart.
643 std::set<std::string> pending_ice_restarts_
644 RTC_GUARDED_BY(signaling_thread());
645
646 std::unique_ptr<LocalIceCredentialsToReplace>
647 local_ice_credentials_to_replace_ RTC_GUARDED_BY(signaling_thread());
648
649 bool remote_peer_supports_msid_ RTC_GUARDED_BY(signaling_thread()) = false;
650 bool is_negotiation_needed_ RTC_GUARDED_BY(signaling_thread()) = false;
651 uint32_t negotiation_needed_event_id_ = 0;
652 bool update_negotiation_needed_on_empty_chain_
653 RTC_GUARDED_BY(signaling_thread()) = false;
654
655 // In Unified Plan, if we encounter remote SDP that does not contain an a=msid
656 // line we create and use a stream with a random ID for our receivers. This is
657 // to support legacy endpoints that do not support the a=msid attribute (as
658 // opposed to streamless tracks with "a=msid:-").
659 rtc::scoped_refptr<MediaStreamInterface> missing_msid_default_stream_
660 RTC_GUARDED_BY(signaling_thread());
661
Harald Alvestrand75b9ab62020-09-30 22:17:18 +0000662 // Used when rolling back RTP data channels.
663 bool have_pending_rtp_data_channel_ RTC_GUARDED_BY(signaling_thread()) =
664 false;
665
Harald Alvestranda474fbf2020-10-01 16:47:23 +0000666 // Updates the error state, signaling if necessary.
667 void SetSessionError(SessionError error, const std::string& error_desc);
668
669 SessionError session_error_ RTC_GUARDED_BY(signaling_thread()) =
670 SessionError::kNone;
671 std::string session_error_desc_ RTC_GUARDED_BY(signaling_thread());
672
Harald Alvestrand763f5a92020-10-22 10:39:40 +0000673 // Member variables for caching global options.
674 cricket::AudioOptions audio_options_ RTC_GUARDED_BY(signaling_thread());
675 cricket::VideoOptions video_options_ RTC_GUARDED_BY(signaling_thread());
676
677 // This object should be used to generate any SSRC that is not explicitly
678 // specified by the user (or by the remote party).
679 // The generator is not used directly, instead it is passed on to the
680 // channel manager and the session description factory.
681 rtc::UniqueRandomIdGenerator ssrc_generator_
682 RTC_GUARDED_BY(signaling_thread());
683
684 // A video bitrate allocator factory.
685 // This can be injected using the PeerConnectionDependencies,
686 // or else the CreateBuiltinVideoBitrateAllocatorFactory() will be called.
687 // Note that one can still choose to override this in a MediaEngine
688 // if one wants too.
689 std::unique_ptr<webrtc::VideoBitrateAllocatorFactory>
690 video_bitrate_allocator_factory_;
691
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000692 rtc::WeakPtrFactory<SdpOfferAnswerHandler> weak_ptr_factory_
693 RTC_GUARDED_BY(signaling_thread());
694};
695
696} // namespace webrtc
697
698#endif // PC_SDP_OFFER_ANSWER_H_