blob: 0608c38ce5d34f4c41effb95ba8b7315da92d213 [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"
Artem Titovd15a5752021-02-10 14:31:24 +010036#include "api/sequence_checker.h"
Harald Alvestrand1f7eab62020-10-18 16:51:47 +000037#include "api/set_local_description_observer_interface.h"
38#include "api/set_remote_description_observer_interface.h"
Harald Alvestrandcdcfab02020-09-28 13:02:07 +000039#include "api/transport/data_channel_transport_interface.h"
40#include "api/turn_customizer.h"
Tomas Gunnarsson2efb8a52021-04-01 16:26:57 +020041#include "api/uma_metrics.h"
Harald Alvestrandf01bd6c2020-10-23 13:30:46 +000042#include "api/video/video_bitrate_allocator_factory.h"
Harald Alvestrand763f5a92020-10-22 10:39:40 +000043#include "media/base/media_channel.h"
Harald Alvestrand1f7eab62020-10-18 16:51:47 +000044#include "media/base/stream_params.h"
45#include "p2p/base/port_allocator.h"
46#include "pc/channel.h"
47#include "pc/channel_interface.h"
48#include "pc/channel_manager.h"
Harald Alvestrandcdcfab02020-09-28 13:02:07 +000049#include "pc/data_channel_controller.h"
50#include "pc/ice_server_parsing.h"
51#include "pc/jsep_transport_controller.h"
Harald Alvestrand1f7eab62020-10-18 16:51:47 +000052#include "pc/media_session.h"
53#include "pc/media_stream_observer.h"
Harald Alvestrandcdcfab02020-09-28 13:02:07 +000054#include "pc/peer_connection_factory.h"
55#include "pc/peer_connection_internal.h"
56#include "pc/rtc_stats_collector.h"
Harald Alvestrand1f7eab62020-10-18 16:51:47 +000057#include "pc/rtp_receiver.h"
Harald Alvestrandcdcfab02020-09-28 13:02:07 +000058#include "pc/rtp_sender.h"
59#include "pc/rtp_transceiver.h"
Harald Alvestrand763f5a92020-10-22 10:39:40 +000060#include "pc/rtp_transmission_manager.h"
Harald Alvestrandcdcfab02020-09-28 13:02:07 +000061#include "pc/sctp_transport.h"
Harald Alvestrandf01bd6c2020-10-23 13:30:46 +000062#include "pc/sdp_state_provider.h"
Harald Alvestrand1f7eab62020-10-18 16:51:47 +000063#include "pc/session_description.h"
Harald Alvestrandcdcfab02020-09-28 13:02:07 +000064#include "pc/stats_collector.h"
65#include "pc/stream_collection.h"
Harald Alvestrand1f7eab62020-10-18 16:51:47 +000066#include "pc/transceiver_list.h"
Harald Alvestrandcdcfab02020-09-28 13:02:07 +000067#include "pc/webrtc_session_description_factory.h"
Harald Alvestrand1f7eab62020-10-18 16:51:47 +000068#include "rtc_base/checks.h"
Harald Alvestrandcdcfab02020-09-28 13:02:07 +000069#include "rtc_base/experiments/field_trial_parser.h"
70#include "rtc_base/operations_chain.h"
71#include "rtc_base/race_checker.h"
Harald Alvestrandf01bd6c2020-10-23 13:30:46 +000072#include "rtc_base/rtc_certificate.h"
73#include "rtc_base/ssl_stream_adapter.h"
Harald Alvestrandf01bd6c2020-10-23 13:30:46 +000074#include "rtc_base/third_party/sigslot/sigslot.h"
Harald Alvestrand1f7eab62020-10-18 16:51:47 +000075#include "rtc_base/thread.h"
76#include "rtc_base/thread_annotations.h"
Harald Alvestrandcdcfab02020-09-28 13:02:07 +000077#include "rtc_base/unique_id_generator.h"
78#include "rtc_base/weak_ptr.h"
79
80namespace webrtc {
81
Harald Alvestrandcdcfab02020-09-28 13:02:07 +000082// SdpOfferAnswerHandler is a component
83// of the PeerConnection object as defined
84// by the PeerConnectionInterface API surface.
85// The class is responsible for the following:
86// - Parsing and interpreting SDP.
87// - Generating offers and answers based on the current state.
88// This class lives on the signaling thread.
Harald Alvestrandf01bd6c2020-10-23 13:30:46 +000089class SdpOfferAnswerHandler : public SdpStateProvider,
90 public sigslot::has_slots<> {
Harald Alvestrandcdcfab02020-09-28 13:02:07 +000091 public:
Harald Alvestrandcdcfab02020-09-28 13:02:07 +000092 ~SdpOfferAnswerHandler();
93
Harald Alvestrand9cd199d2020-10-27 07:10:43 +000094 // Creates an SdpOfferAnswerHandler. Modifies dependencies.
95 static std::unique_ptr<SdpOfferAnswerHandler> Create(
96 PeerConnection* pc,
Harald Alvestrand763f5a92020-10-22 10:39:40 +000097 const PeerConnectionInterface::RTCConfiguration& configuration,
Harald Alvestrand9cd199d2020-10-27 07:10:43 +000098 PeerConnectionDependencies& dependencies);
Harald Alvestrand763f5a92020-10-22 10:39:40 +000099
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();
176
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000177 // Destroys all BaseChannels and destroys the SCTP data channel, if present.
178 void DestroyAllChannels();
179
Harald Alvestrand6f04b652020-10-09 11:42:17 +0000180 rtc::scoped_refptr<StreamCollectionInterface> local_streams();
181 rtc::scoped_refptr<StreamCollectionInterface> remote_streams();
182
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000183 private:
184 class ImplicitCreateSessionDescriptionObserver;
Harald Alvestrand1f7eab62020-10-18 16:51:47 +0000185
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000186 friend class ImplicitCreateSessionDescriptionObserver;
187 class SetSessionDescriptionObserverAdapter;
Harald Alvestrand1f7eab62020-10-18 16:51:47 +0000188
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000189 friend class SetSessionDescriptionObserverAdapter;
190
Harald Alvestranda474fbf2020-10-01 16:47:23 +0000191 enum class SessionError {
192 kNone, // No error.
193 kContent, // Error in BaseChannel SetLocalContent/SetRemoteContent.
194 kTransport, // Error from the underlying transport.
195 };
196
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000197 // Represents the [[LocalIceCredentialsToReplace]] internal slot in the spec.
198 // It makes the next CreateOffer() produce new ICE credentials even if
199 // RTCOfferAnswerOptions::ice_restart is false.
200 // https://w3c.github.io/webrtc-pc/#dfn-localufragstoreplace
201 // TODO(hbos): When JsepTransportController/JsepTransport supports rollback,
202 // move this type of logic to JsepTransportController/JsepTransport.
203 class LocalIceCredentialsToReplace;
204
Harald Alvestrand9cd199d2020-10-27 07:10:43 +0000205 // Only called by the Create() function.
206 explicit SdpOfferAnswerHandler(PeerConnection* pc);
207 // Called from the `Create()` function. Can only be called
208 // once. Modifies dependencies.
209 void Initialize(
210 const PeerConnectionInterface::RTCConfiguration& configuration,
211 PeerConnectionDependencies& dependencies);
212
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000213 rtc::Thread* signaling_thread() const;
214 // Non-const versions of local_description()/remote_description(), for use
215 // internally.
216 SessionDescriptionInterface* mutable_local_description()
217 RTC_RUN_ON(signaling_thread()) {
218 return pending_local_description_ ? pending_local_description_.get()
219 : current_local_description_.get();
220 }
221 SessionDescriptionInterface* mutable_remote_description()
222 RTC_RUN_ON(signaling_thread()) {
223 return pending_remote_description_ ? pending_remote_description_.get()
224 : current_remote_description_.get();
225 }
226
227 // Synchronous implementations of SetLocalDescription/SetRemoteDescription
228 // that return an RTCError instead of invoking a callback.
229 RTCError ApplyLocalDescription(
230 std::unique_ptr<SessionDescriptionInterface> desc);
231 RTCError ApplyRemoteDescription(
232 std::unique_ptr<SessionDescriptionInterface> desc);
233
234 // Implementation of the offer/answer exchange operations. These are chained
235 // onto the |operations_chain_| when the public CreateOffer(), CreateAnswer(),
236 // SetLocalDescription() and SetRemoteDescription() methods are invoked.
237 void DoCreateOffer(
238 const PeerConnectionInterface::RTCOfferAnswerOptions& options,
239 rtc::scoped_refptr<CreateSessionDescriptionObserver> observer);
240 void DoCreateAnswer(
241 const PeerConnectionInterface::RTCOfferAnswerOptions& options,
242 rtc::scoped_refptr<CreateSessionDescriptionObserver> observer);
243 void DoSetLocalDescription(
244 std::unique_ptr<SessionDescriptionInterface> desc,
245 rtc::scoped_refptr<SetLocalDescriptionObserverInterface> observer);
246 void DoSetRemoteDescription(
247 std::unique_ptr<SessionDescriptionInterface> desc,
248 rtc::scoped_refptr<SetRemoteDescriptionObserverInterface> observer);
249
250 // Update the state, signaling if necessary.
251 void ChangeSignalingState(
252 PeerConnectionInterface::SignalingState signaling_state);
253
254 RTCError UpdateSessionState(SdpType type,
255 cricket::ContentSource source,
256 const cricket::SessionDescription* description);
257
258 bool IsUnifiedPlan() const RTC_RUN_ON(signaling_thread());
259
Harald Alvestrand763f5a92020-10-22 10:39:40 +0000260 // Signals from MediaStreamObserver.
261 void OnAudioTrackAdded(AudioTrackInterface* track,
262 MediaStreamInterface* stream)
263 RTC_RUN_ON(signaling_thread());
264 void OnAudioTrackRemoved(AudioTrackInterface* track,
265 MediaStreamInterface* stream)
266 RTC_RUN_ON(signaling_thread());
267 void OnVideoTrackAdded(VideoTrackInterface* track,
268 MediaStreamInterface* stream)
269 RTC_RUN_ON(signaling_thread());
270 void OnVideoTrackRemoved(VideoTrackInterface* track,
271 MediaStreamInterface* stream)
272 RTC_RUN_ON(signaling_thread());
273
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000274 // | desc_type | is the type of the description that caused the rollback.
275 RTCError Rollback(SdpType desc_type);
276 void OnOperationsChainEmpty();
277
278 // Runs the algorithm **set the associated remote streams** specified in
279 // https://w3c.github.io/webrtc-pc/#set-associated-remote-streams.
280 void SetAssociatedRemoteStreams(
281 rtc::scoped_refptr<RtpReceiverInternal> receiver,
282 const std::vector<std::string>& stream_ids,
283 std::vector<rtc::scoped_refptr<MediaStreamInterface>>* added_streams,
284 std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams);
285
286 bool CheckIfNegotiationIsNeeded();
287 void GenerateNegotiationNeededEvent();
288 // Helper method which verifies SDP.
289 RTCError ValidateSessionDescription(const SessionDescriptionInterface* sdesc,
290 cricket::ContentSource source)
291 RTC_RUN_ON(signaling_thread());
292
293 // Updates the local RtpTransceivers according to the JSEP rules. Called as
294 // part of setting the local/remote description.
295 RTCError UpdateTransceiversAndDataChannels(
296 cricket::ContentSource source,
297 const SessionDescriptionInterface& new_session,
298 const SessionDescriptionInterface* old_local_description,
299 const SessionDescriptionInterface* old_remote_description);
300
301 // Associate the given transceiver according to the JSEP rules.
302 RTCErrorOr<
303 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>>
304 AssociateTransceiver(cricket::ContentSource source,
305 SdpType type,
306 size_t mline_index,
307 const cricket::ContentInfo& content,
308 const cricket::ContentInfo* old_local_content,
309 const cricket::ContentInfo* old_remote_content)
310 RTC_RUN_ON(signaling_thread());
311
Harald Alvestrand85466662021-04-19 21:21:36 +0000312 // Returns the media section in the given session description that is
313 // associated with the RtpTransceiver. Returns null if none found or this
314 // RtpTransceiver is not associated. Logic varies depending on the
315 // SdpSemantics specified in the configuration.
316 const cricket::ContentInfo* FindMediaSectionForTransceiver(
317 const RtpTransceiver* transceiver,
318 const SessionDescriptionInterface* sdesc) const;
319
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000320 // If the BUNDLE policy is max-bundle, then we know for sure that all
321 // transports will be bundled from the start. This method returns the BUNDLE
322 // group if that's the case, or null if BUNDLE will be negotiated later. An
323 // error is returned if max-bundle is specified but the session description
324 // does not have a BUNDLE group.
325 RTCErrorOr<const cricket::ContentGroup*> GetEarlyBundleGroup(
326 const cricket::SessionDescription& desc) const
327 RTC_RUN_ON(signaling_thread());
328
329 // Either creates or destroys the transceiver's BaseChannel according to the
330 // given media section.
331 RTCError UpdateTransceiverChannel(
332 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
333 transceiver,
334 const cricket::ContentInfo& content,
335 const cricket::ContentGroup* bundle_group) RTC_RUN_ON(signaling_thread());
336
337 // Either creates or destroys the local data channel according to the given
338 // media section.
339 RTCError UpdateDataChannel(cricket::ContentSource source,
340 const cricket::ContentInfo& content,
341 const cricket::ContentGroup* bundle_group)
342 RTC_RUN_ON(signaling_thread());
Harald Alvestrandc06e3742020-10-01 10:23:33 +0000343 // Check if a call to SetLocalDescription is acceptable with a session
344 // description of the given type.
345 bool ExpectSetLocalDescription(SdpType type);
346 // Check if a call to SetRemoteDescription is acceptable with a session
347 // description of the given type.
348 bool ExpectSetRemoteDescription(SdpType type);
349
350 // The offer/answer machinery assumes the media section MID is present and
351 // unique. To support legacy end points that do not supply a=mid lines, this
352 // method will modify the session description to add MIDs generated according
353 // to the SDP semantics.
354 void FillInMissingRemoteMids(cricket::SessionDescription* remote_description);
355
356 // Returns an RtpTransciever, if available, that can be used to receive the
357 // given media type according to JSEP rules.
358 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
359 FindAvailableTransceiverToReceive(cricket::MediaType media_type) const;
360
361 // Returns a MediaSessionOptions struct with options decided by |options|,
362 // the local MediaStreams and DataChannels.
363 void GetOptionsForOffer(const PeerConnectionInterface::RTCOfferAnswerOptions&
364 offer_answer_options,
365 cricket::MediaSessionOptions* session_options);
366 void GetOptionsForPlanBOffer(
367 const PeerConnectionInterface::RTCOfferAnswerOptions&
368 offer_answer_options,
369 cricket::MediaSessionOptions* session_options)
370 RTC_RUN_ON(signaling_thread());
371 void GetOptionsForUnifiedPlanOffer(
372 const PeerConnectionInterface::RTCOfferAnswerOptions&
373 offer_answer_options,
374 cricket::MediaSessionOptions* session_options)
375 RTC_RUN_ON(signaling_thread());
376
377 // Returns a MediaSessionOptions struct with options decided by
378 // |constraints|, the local MediaStreams and DataChannels.
379 void GetOptionsForAnswer(const PeerConnectionInterface::RTCOfferAnswerOptions&
380 offer_answer_options,
381 cricket::MediaSessionOptions* session_options);
382 void GetOptionsForPlanBAnswer(
383 const PeerConnectionInterface::RTCOfferAnswerOptions&
384 offer_answer_options,
385 cricket::MediaSessionOptions* session_options)
386 RTC_RUN_ON(signaling_thread());
387 void GetOptionsForUnifiedPlanAnswer(
388 const PeerConnectionInterface::RTCOfferAnswerOptions&
389 offer_answer_options,
390 cricket::MediaSessionOptions* session_options)
391 RTC_RUN_ON(signaling_thread());
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000392
Harald Alvestranda474fbf2020-10-01 16:47:23 +0000393 const char* SessionErrorToString(SessionError error) const;
394 std::string GetSessionErrorMsg();
395 // Returns the last error in the session. See the enum above for details.
396 SessionError session_error() const {
397 RTC_DCHECK_RUN_ON(signaling_thread());
398 return session_error_;
399 }
400 const std::string& session_error_desc() const { return session_error_desc_; }
401
402 RTCError HandleLegacyOfferOptions(
403 const PeerConnectionInterface::RTCOfferAnswerOptions& options);
404 void RemoveRecvDirectionFromReceivingTransceiversOfType(
405 cricket::MediaType media_type) RTC_RUN_ON(signaling_thread());
406 void AddUpToOneReceivingTransceiverOfType(cricket::MediaType media_type);
407
408 std::vector<
409 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>>
410 GetReceivingTransceiversOfType(cricket::MediaType media_type)
411 RTC_RUN_ON(signaling_thread());
412
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000413 // Runs the algorithm specified in
414 // https://w3c.github.io/webrtc-pc/#process-remote-track-removal
Harald Alvestranda474fbf2020-10-01 16:47:23 +0000415 // This method will update the following lists:
416 // |remove_list| is the list of transceivers for which the receiving track is
417 // being removed.
418 // |removed_streams| is the list of streams which no longer have a receiving
419 // track so should be removed.
Harald Alvestranda474fbf2020-10-01 16:47:23 +0000420 void ProcessRemovalOfRemoteTrack(
Harald Alvestrand85466662021-04-19 21:21:36 +0000421 const rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
Harald Alvestranda474fbf2020-10-01 16:47:23 +0000422 transceiver,
423 std::vector<rtc::scoped_refptr<RtpTransceiverInterface>>* remove_list,
424 std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams);
425
426 void RemoveRemoteStreamsIfEmpty(
427 const std::vector<rtc::scoped_refptr<MediaStreamInterface>>&
428 remote_streams,
429 std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams);
430
431 // Remove all local and remote senders of type |media_type|.
432 // Called when a media type is rejected (m-line set to port 0).
433 void RemoveSenders(cricket::MediaType media_type);
434
435 // Loops through the vector of |streams| and finds added and removed
436 // StreamParams since last time this method was called.
437 // For each new or removed StreamParam, OnLocalSenderSeen or
438 // OnLocalSenderRemoved is invoked.
439 void UpdateLocalSenders(const std::vector<cricket::StreamParams>& streams,
440 cricket::MediaType media_type);
441
442 // Makes sure a MediaStreamTrack is created for each StreamParam in |streams|,
443 // and existing MediaStreamTracks are removed if there is no corresponding
444 // StreamParam. If |default_track_needed| is true, a default MediaStreamTrack
445 // is created if it doesn't exist; if false, it's removed if it exists.
446 // |media_type| is the type of the |streams| and can be either audio or video.
447 // If a new MediaStream is created it is added to |new_streams|.
448 void UpdateRemoteSendersList(
449 const std::vector<cricket::StreamParams>& streams,
450 bool default_track_needed,
451 cricket::MediaType media_type,
452 StreamCollection* new_streams);
453
Taylor Brandstetterd0acbd82021-01-25 13:44:55 -0800454 // Enables media channels to allow sending of media.
455 // This enables media to flow on all configured audio/video channels and the
456 // RtpDataChannel.
457 void EnableSending();
Harald Alvestranda474fbf2020-10-01 16:47:23 +0000458 // Push the media parts of the local or remote session description
Taylor Brandstetterd0acbd82021-01-25 13:44:55 -0800459 // down to all of the channels.
Harald Alvestranda474fbf2020-10-01 16:47:23 +0000460 RTCError PushdownMediaDescription(SdpType type,
461 cricket::ContentSource source);
462
463 RTCError PushdownTransportDescription(cricket::ContentSource source,
464 SdpType type);
465 // Helper function to remove stopped transceivers.
466 void RemoveStoppedTransceivers();
467 // Deletes the corresponding channel of contents that don't exist in |desc|.
468 // |desc| can be null. This means that all channels are deleted.
469 void RemoveUnusedChannels(const cricket::SessionDescription* desc);
470
471 // Report inferred negotiated SDP semantics from a local/remote answer to the
472 // UMA observer.
473 void ReportNegotiatedSdpSemantics(const SessionDescriptionInterface& answer);
474
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000475 // Finds remote MediaStreams without any tracks and removes them from
476 // |remote_streams_| and notifies the observer that the MediaStreams no longer
477 // exist.
478 void UpdateEndedRemoteMediaStreams();
479
480 // Uses all remote candidates in |remote_desc| in this session.
481 bool UseCandidatesInSessionDescription(
482 const SessionDescriptionInterface* remote_desc);
483 // Uses |candidate| in this session.
484 bool UseCandidate(const IceCandidateInterface* candidate);
485 // Returns true if we are ready to push down the remote candidate.
486 // |remote_desc| is the new remote description, or NULL if the current remote
487 // description should be used. Output |valid| is true if the candidate media
488 // index is valid.
489 bool ReadyToUseRemoteCandidate(const IceCandidateInterface* candidate,
490 const SessionDescriptionInterface* remote_desc,
491 bool* valid);
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000492
493 RTCErrorOr<const cricket::ContentInfo*> FindContentInfo(
494 const SessionDescriptionInterface* description,
495 const IceCandidateInterface* candidate) RTC_RUN_ON(signaling_thread());
496
497 // Functions for dealing with transports.
498 // Note that cricket code uses the term "channel" for what other code
499 // refers to as "transport".
500
501 // Allocates media channels based on the |desc|. If |desc| doesn't have
502 // the BUNDLE option, this method will disable BUNDLE in PortAllocator.
503 // This method will also delete any existing media channels before creating.
504 RTCError CreateChannels(const cricket::SessionDescription& desc);
505
506 // Helper methods to create media channels.
507 cricket::VoiceChannel* CreateVoiceChannel(const std::string& mid);
508 cricket::VideoChannel* CreateVideoChannel(const std::string& mid);
509 bool CreateDataChannel(const std::string& mid);
510
511 // Destroys and clears the BaseChannel associated with the given transceiver,
512 // if such channel is set.
513 void DestroyTransceiverChannel(
514 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
515 transceiver);
516
517 // Destroys the RTP data channel transport and/or the SCTP data channel
518 // transport and clears it.
519 void DestroyDataChannelTransport();
520
521 // Destroys the given ChannelInterface.
522 // The channel cannot be accessed after this method is called.
523 void DestroyChannelInterface(cricket::ChannelInterface* channel);
524 // Generates MediaDescriptionOptions for the |session_opts| based on existing
525 // local description or remote description.
526
527 void GenerateMediaDescriptionOptions(
528 const SessionDescriptionInterface* session_desc,
529 RtpTransceiverDirection audio_direction,
530 RtpTransceiverDirection video_direction,
531 absl::optional<size_t>* audio_index,
532 absl::optional<size_t>* video_index,
533 absl::optional<size_t>* data_index,
534 cricket::MediaSessionOptions* session_options);
535
536 // Generates the active MediaDescriptionOptions for the local data channel
537 // given the specified MID.
538 cricket::MediaDescriptionOptions GetMediaDescriptionOptionsForActiveData(
539 const std::string& mid) const;
540
541 // Generates the rejected MediaDescriptionOptions for the local data channel
542 // given the specified MID.
543 cricket::MediaDescriptionOptions GetMediaDescriptionOptionsForRejectedData(
544 const std::string& mid) const;
545
Taylor Brandstetterd0acbd82021-01-25 13:44:55 -0800546 // Based on number of transceivers per media type, enabled or disable
547 // payload type based demuxing in the affected channels.
548 bool UpdatePayloadTypeDemuxingState(cricket::ContentSource source);
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000549
550 // ==================================================================
551 // Access to pc_ variables
552 cricket::ChannelManager* channel_manager() const;
Harald Alvestrande15fb152020-10-19 13:28:05 +0000553 TransceiverList* transceivers();
554 const TransceiverList* transceivers() const;
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000555 DataChannelController* data_channel_controller();
556 const DataChannelController* data_channel_controller() const;
557 cricket::PortAllocator* port_allocator();
558 const cricket::PortAllocator* port_allocator() const;
Harald Alvestrande15fb152020-10-19 13:28:05 +0000559 RtpTransmissionManager* rtp_manager();
560 const RtpTransmissionManager* rtp_manager() const;
Harald Alvestrandf01bd6c2020-10-23 13:30:46 +0000561 JsepTransportController* transport_controller();
562 const JsepTransportController* transport_controller() 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 Alvestranda474fbf2020-10-01 16:47:23 +0000633 // Updates the error state, signaling if necessary.
634 void SetSessionError(SessionError error, const std::string& error_desc);
635
Tomas Gunnarsson2efb8a52021-04-01 16:26:57 +0200636 // Implements AddIceCandidate without reporting usage, but returns the
637 // particular success/error value that should be reported (and can be utilized
638 // for other purposes).
639 AddIceCandidateResult AddIceCandidateInternal(
640 const IceCandidateInterface* candidate);
641
Harald Alvestranda474fbf2020-10-01 16:47:23 +0000642 SessionError session_error_ RTC_GUARDED_BY(signaling_thread()) =
643 SessionError::kNone;
644 std::string session_error_desc_ RTC_GUARDED_BY(signaling_thread());
645
Harald Alvestrand763f5a92020-10-22 10:39:40 +0000646 // Member variables for caching global options.
647 cricket::AudioOptions audio_options_ RTC_GUARDED_BY(signaling_thread());
648 cricket::VideoOptions video_options_ RTC_GUARDED_BY(signaling_thread());
649
650 // This object should be used to generate any SSRC that is not explicitly
651 // specified by the user (or by the remote party).
652 // The generator is not used directly, instead it is passed on to the
653 // channel manager and the session description factory.
Tomas Gunnarsson64099bc2021-04-09 09:51:37 +0200654 // TODO(bugs.webrtc.org/12666): This variable is used from both the signaling
655 // and worker threads. See if we can't restrict usage to a single thread.
656 rtc::UniqueRandomIdGenerator ssrc_generator_;
Harald Alvestrand763f5a92020-10-22 10:39:40 +0000657
658 // A video bitrate allocator factory.
659 // This can be injected using the PeerConnectionDependencies,
660 // or else the CreateBuiltinVideoBitrateAllocatorFactory() will be called.
661 // Note that one can still choose to override this in a MediaEngine
662 // if one wants too.
663 std::unique_ptr<webrtc::VideoBitrateAllocatorFactory>
664 video_bitrate_allocator_factory_;
665
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000666 rtc::WeakPtrFactory<SdpOfferAnswerHandler> weak_ptr_factory_
667 RTC_GUARDED_BY(signaling_thread());
668};
669
670} // namespace webrtc
671
672#endif // PC_SDP_OFFER_ANSWER_H_