blob: 3318a4fe5095b0c1bb0eee67885fae0c6081401c [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>
Florent Castellidcb9ffc2021-06-29 14:58:23 +020016
Harald Alvestrand1f7eab62020-10-18 16:51:47 +000017#include <functional>
Harald Alvestrandcdcfab02020-09-28 13:02:07 +000018#include <map>
19#include <memory>
20#include <set>
21#include <string>
22#include <utility>
23#include <vector>
24
Harald Alvestrand1f7eab62020-10-18 16:51:47 +000025#include "absl/types/optional.h"
Harald Alvestrand763f5a92020-10-22 10:39:40 +000026#include "api/audio_options.h"
Harald Alvestrand1f7eab62020-10-18 16:51:47 +000027#include "api/candidate.h"
28#include "api/jsep.h"
Harald Alvestrandcdcfab02020-09-28 13:02:07 +000029#include "api/jsep_ice_candidate.h"
Harald Alvestrand1f7eab62020-10-18 16:51:47 +000030#include "api/media_stream_interface.h"
31#include "api/media_types.h"
Harald Alvestrandcdcfab02020-09-28 13:02:07 +000032#include "api/peer_connection_interface.h"
Harald Alvestrand1f7eab62020-10-18 16:51:47 +000033#include "api/rtc_error.h"
34#include "api/rtp_transceiver_direction.h"
35#include "api/rtp_transceiver_interface.h"
36#include "api/scoped_refptr.h"
Artem Titovd15a5752021-02-10 14:31:24 +010037#include "api/sequence_checker.h"
Harald Alvestrand1f7eab62020-10-18 16:51:47 +000038#include "api/set_local_description_observer_interface.h"
39#include "api/set_remote_description_observer_interface.h"
Harald Alvestrandcdcfab02020-09-28 13:02:07 +000040#include "api/transport/data_channel_transport_interface.h"
41#include "api/turn_customizer.h"
Tomas Gunnarsson2efb8a52021-04-01 16:26:57 +020042#include "api/uma_metrics.h"
Harald Alvestrandf01bd6c2020-10-23 13:30:46 +000043#include "api/video/video_bitrate_allocator_factory.h"
Harald Alvestrand763f5a92020-10-22 10:39:40 +000044#include "media/base/media_channel.h"
Harald Alvestrand1f7eab62020-10-18 16:51:47 +000045#include "media/base/stream_params.h"
46#include "p2p/base/port_allocator.h"
47#include "pc/channel.h"
48#include "pc/channel_interface.h"
49#include "pc/channel_manager.h"
Harald Alvestrandcdcfab02020-09-28 13:02:07 +000050#include "pc/data_channel_controller.h"
51#include "pc/ice_server_parsing.h"
52#include "pc/jsep_transport_controller.h"
Harald Alvestrand1f7eab62020-10-18 16:51:47 +000053#include "pc/media_session.h"
54#include "pc/media_stream_observer.h"
Harald Alvestrandcdcfab02020-09-28 13:02:07 +000055#include "pc/peer_connection_factory.h"
56#include "pc/peer_connection_internal.h"
57#include "pc/rtc_stats_collector.h"
Harald Alvestrand1f7eab62020-10-18 16:51:47 +000058#include "pc/rtp_receiver.h"
Harald Alvestrandcdcfab02020-09-28 13:02:07 +000059#include "pc/rtp_sender.h"
60#include "pc/rtp_transceiver.h"
Harald Alvestrand763f5a92020-10-22 10:39:40 +000061#include "pc/rtp_transmission_manager.h"
Harald Alvestrandcdcfab02020-09-28 13:02:07 +000062#include "pc/sctp_transport.h"
Harald Alvestrandf01bd6c2020-10-23 13:30:46 +000063#include "pc/sdp_state_provider.h"
Harald Alvestrand1f7eab62020-10-18 16:51:47 +000064#include "pc/session_description.h"
Harald Alvestrandcdcfab02020-09-28 13:02:07 +000065#include "pc/stats_collector.h"
66#include "pc/stream_collection.h"
Harald Alvestrand1f7eab62020-10-18 16:51:47 +000067#include "pc/transceiver_list.h"
Harald Alvestrandcdcfab02020-09-28 13:02:07 +000068#include "pc/webrtc_session_description_factory.h"
Harald Alvestrand1f7eab62020-10-18 16:51:47 +000069#include "rtc_base/checks.h"
Harald Alvestrandcdcfab02020-09-28 13:02:07 +000070#include "rtc_base/experiments/field_trial_parser.h"
71#include "rtc_base/operations_chain.h"
72#include "rtc_base/race_checker.h"
Harald Alvestrandf01bd6c2020-10-23 13:30:46 +000073#include "rtc_base/rtc_certificate.h"
74#include "rtc_base/ssl_stream_adapter.h"
Harald Alvestrandf01bd6c2020-10-23 13:30:46 +000075#include "rtc_base/third_party/sigslot/sigslot.h"
Harald Alvestrand1f7eab62020-10-18 16:51:47 +000076#include "rtc_base/thread.h"
77#include "rtc_base/thread_annotations.h"
Harald Alvestrandcdcfab02020-09-28 13:02:07 +000078#include "rtc_base/unique_id_generator.h"
79#include "rtc_base/weak_ptr.h"
80
81namespace webrtc {
82
Harald Alvestrandcdcfab02020-09-28 13:02:07 +000083// SdpOfferAnswerHandler is a component
84// of the PeerConnection object as defined
85// by the PeerConnectionInterface API surface.
86// The class is responsible for the following:
87// - Parsing and interpreting SDP.
88// - Generating offers and answers based on the current state.
89// This class lives on the signaling thread.
Harald Alvestrandf01bd6c2020-10-23 13:30:46 +000090class SdpOfferAnswerHandler : public SdpStateProvider,
91 public sigslot::has_slots<> {
Harald Alvestrandcdcfab02020-09-28 13:02:07 +000092 public:
Harald Alvestrandcdcfab02020-09-28 13:02:07 +000093 ~SdpOfferAnswerHandler();
94
Harald Alvestrand9cd199d2020-10-27 07:10:43 +000095 // Creates an SdpOfferAnswerHandler. Modifies dependencies.
96 static std::unique_ptr<SdpOfferAnswerHandler> Create(
97 PeerConnection* pc,
Harald Alvestrand763f5a92020-10-22 10:39:40 +000098 const PeerConnectionInterface::RTCConfiguration& configuration,
Harald Alvestrand9cd199d2020-10-27 07:10:43 +000099 PeerConnectionDependencies& dependencies);
Harald Alvestrand763f5a92020-10-22 10:39:40 +0000100
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000101 void ResetSessionDescFactory() {
102 RTC_DCHECK_RUN_ON(signaling_thread());
103 webrtc_session_desc_factory_.reset();
104 }
105 const WebRtcSessionDescriptionFactory* webrtc_session_desc_factory() const {
106 RTC_DCHECK_RUN_ON(signaling_thread());
107 return webrtc_session_desc_factory_.get();
108 }
109
110 // Change signaling state to Closed, and perform appropriate actions.
111 void Close();
112
113 // Called as part of destroying the owning PeerConnection.
114 void PrepareForShutdown();
115
Harald Alvestrandf01bd6c2020-10-23 13:30:46 +0000116 // Implementation of SdpStateProvider
117 PeerConnectionInterface::SignalingState signaling_state() const override;
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000118
Harald Alvestrandf01bd6c2020-10-23 13:30:46 +0000119 const SessionDescriptionInterface* local_description() const override;
120 const SessionDescriptionInterface* remote_description() const override;
121 const SessionDescriptionInterface* current_local_description() const override;
122 const SessionDescriptionInterface* current_remote_description()
123 const override;
124 const SessionDescriptionInterface* pending_local_description() const override;
125 const SessionDescriptionInterface* pending_remote_description()
126 const override;
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000127
Harald Alvestrandf01bd6c2020-10-23 13:30:46 +0000128 bool NeedsIceRestart(const std::string& content_name) const override;
129 bool IceRestartPending(const std::string& content_name) const override;
130 absl::optional<rtc::SSLRole> GetDtlsRole(
131 const std::string& mid) const override;
Harald Alvestrandd89ce532020-10-21 08:59:22 +0000132
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000133 void RestartIce();
134
135 // JSEP01
136 void CreateOffer(
137 CreateSessionDescriptionObserver* observer,
138 const PeerConnectionInterface::RTCOfferAnswerOptions& options);
139 void CreateAnswer(
140 CreateSessionDescriptionObserver* observer,
141 const PeerConnectionInterface::RTCOfferAnswerOptions& options);
142
143 void SetLocalDescription(
144 std::unique_ptr<SessionDescriptionInterface> desc,
145 rtc::scoped_refptr<SetLocalDescriptionObserverInterface> observer);
146 void SetLocalDescription(
147 rtc::scoped_refptr<SetLocalDescriptionObserverInterface> observer);
148 void SetLocalDescription(SetSessionDescriptionObserver* observer,
149 SessionDescriptionInterface* desc);
150 void SetLocalDescription(SetSessionDescriptionObserver* observer);
151
152 void SetRemoteDescription(
153 std::unique_ptr<SessionDescriptionInterface> desc,
154 rtc::scoped_refptr<SetRemoteDescriptionObserverInterface> observer);
155 void SetRemoteDescription(SetSessionDescriptionObserver* observer,
156 SessionDescriptionInterface* desc);
157
158 PeerConnectionInterface::RTCConfiguration GetConfiguration();
159 RTCError SetConfiguration(
160 const PeerConnectionInterface::RTCConfiguration& configuration);
161 bool AddIceCandidate(const IceCandidateInterface* candidate);
162 void AddIceCandidate(std::unique_ptr<IceCandidateInterface> candidate,
163 std::function<void(RTCError)> callback);
164 bool RemoveIceCandidates(const std::vector<cricket::Candidate>& candidates);
165 // Adds a locally generated candidate to the local description.
166 void AddLocalIceCandidate(const JsepIceCandidate* candidate);
167 void RemoveLocalIceCandidates(
168 const std::vector<cricket::Candidate>& candidates);
169 bool ShouldFireNegotiationNeededEvent(uint32_t event_id);
170
Harald Alvestrand6f04b652020-10-09 11:42:17 +0000171 bool AddStream(MediaStreamInterface* local_stream);
172 void RemoveStream(MediaStreamInterface* local_stream);
173
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000174 absl::optional<bool> is_caller();
175 bool HasNewIceCredentials();
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000176 void UpdateNegotiationNeeded();
177
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000178 // Destroys all BaseChannels and destroys the SCTP data channel, if present.
179 void DestroyAllChannels();
180
Harald Alvestrand6f04b652020-10-09 11:42:17 +0000181 rtc::scoped_refptr<StreamCollectionInterface> local_streams();
182 rtc::scoped_refptr<StreamCollectionInterface> remote_streams();
183
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000184 private:
Tomas Gunnarsson1c7c09b2022-01-12 13:11:04 +0100185 class RemoteDescriptionOperation;
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000186 class ImplicitCreateSessionDescriptionObserver;
Harald Alvestrand1f7eab62020-10-18 16:51:47 +0000187
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000188 friend class ImplicitCreateSessionDescriptionObserver;
189 class SetSessionDescriptionObserverAdapter;
Harald Alvestrand1f7eab62020-10-18 16:51:47 +0000190
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000191 friend class SetSessionDescriptionObserverAdapter;
192
Harald Alvestranda474fbf2020-10-01 16:47:23 +0000193 enum class SessionError {
194 kNone, // No error.
195 kContent, // Error in BaseChannel SetLocalContent/SetRemoteContent.
196 kTransport, // Error from the underlying transport.
197 };
198
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000199 // Represents the [[LocalIceCredentialsToReplace]] internal slot in the spec.
200 // It makes the next CreateOffer() produce new ICE credentials even if
201 // RTCOfferAnswerOptions::ice_restart is false.
202 // https://w3c.github.io/webrtc-pc/#dfn-localufragstoreplace
203 // TODO(hbos): When JsepTransportController/JsepTransport supports rollback,
204 // move this type of logic to JsepTransportController/JsepTransport.
205 class LocalIceCredentialsToReplace;
206
Harald Alvestrand9cd199d2020-10-27 07:10:43 +0000207 // Only called by the Create() function.
208 explicit SdpOfferAnswerHandler(PeerConnection* pc);
209 // Called from the `Create()` function. Can only be called
210 // once. Modifies dependencies.
211 void Initialize(
212 const PeerConnectionInterface::RTCConfiguration& configuration,
213 PeerConnectionDependencies& dependencies);
214
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000215 rtc::Thread* signaling_thread() const;
216 // Non-const versions of local_description()/remote_description(), for use
217 // internally.
218 SessionDescriptionInterface* mutable_local_description()
219 RTC_RUN_ON(signaling_thread()) {
220 return pending_local_description_ ? pending_local_description_.get()
221 : current_local_description_.get();
222 }
223 SessionDescriptionInterface* mutable_remote_description()
224 RTC_RUN_ON(signaling_thread()) {
225 return pending_remote_description_ ? pending_remote_description_.get()
226 : current_remote_description_.get();
227 }
228
229 // Synchronous implementations of SetLocalDescription/SetRemoteDescription
230 // that return an RTCError instead of invoking a callback.
231 RTCError ApplyLocalDescription(
Henrik Boströmf8187e02021-04-26 21:04:26 +0200232 std::unique_ptr<SessionDescriptionInterface> desc,
233 const std::map<std::string, const cricket::ContentGroup*>&
234 bundle_groups_by_mid);
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000235 RTCError ApplyRemoteDescription(
Henrik Boströmf8187e02021-04-26 21:04:26 +0200236 std::unique_ptr<SessionDescriptionInterface> desc,
237 const std::map<std::string, const cricket::ContentGroup*>&
238 bundle_groups_by_mid);
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000239
Tomas Gunnarsson651586c2022-01-07 18:33:12 +0000240 // Part of ApplyRemoteDescription steps specific to Unified Plan.
241 void ApplyRemoteDescriptionUpdateTransceiverState(SdpType sdp_type);
242
Tomas Gunnarssonb625edf2022-01-07 16:22:14 +0000243 // Part of ApplyRemoteDescription steps specific to plan b.
244 void PlanBUpdateSendersAndReceivers(
245 const cricket::ContentInfo* audio_content,
246 const cricket::AudioContentDescription* audio_desc,
247 const cricket::ContentInfo* video_content,
248 const cricket::VideoContentDescription* video_desc);
249
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000250 // Implementation of the offer/answer exchange operations. These are chained
Artem Titov880fa812021-07-30 22:30:23 +0200251 // onto the `operations_chain_` when the public CreateOffer(), CreateAnswer(),
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000252 // SetLocalDescription() and SetRemoteDescription() methods are invoked.
253 void DoCreateOffer(
254 const PeerConnectionInterface::RTCOfferAnswerOptions& options,
255 rtc::scoped_refptr<CreateSessionDescriptionObserver> observer);
256 void DoCreateAnswer(
257 const PeerConnectionInterface::RTCOfferAnswerOptions& options,
258 rtc::scoped_refptr<CreateSessionDescriptionObserver> observer);
259 void DoSetLocalDescription(
260 std::unique_ptr<SessionDescriptionInterface> desc,
261 rtc::scoped_refptr<SetLocalDescriptionObserverInterface> observer);
262 void DoSetRemoteDescription(
Tomas Gunnarsson1c7c09b2022-01-12 13:11:04 +0100263 std::unique_ptr<RemoteDescriptionOperation> operation);
264
265 // Called after a DoSetRemoteDescription operation completes.
266 void SetRemoteDescriptionPostProcess(bool was_answer)
267 RTC_RUN_ON(signaling_thread());
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000268
269 // Update the state, signaling if necessary.
270 void ChangeSignalingState(
271 PeerConnectionInterface::SignalingState signaling_state);
272
Henrik Boströmf8187e02021-04-26 21:04:26 +0200273 RTCError UpdateSessionState(
274 SdpType type,
275 cricket::ContentSource source,
276 const cricket::SessionDescription* description,
277 const std::map<std::string, const cricket::ContentGroup*>&
278 bundle_groups_by_mid);
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000279
280 bool IsUnifiedPlan() const RTC_RUN_ON(signaling_thread());
281
Harald Alvestrand763f5a92020-10-22 10:39:40 +0000282 // Signals from MediaStreamObserver.
283 void OnAudioTrackAdded(AudioTrackInterface* track,
284 MediaStreamInterface* stream)
285 RTC_RUN_ON(signaling_thread());
286 void OnAudioTrackRemoved(AudioTrackInterface* track,
287 MediaStreamInterface* stream)
288 RTC_RUN_ON(signaling_thread());
289 void OnVideoTrackAdded(VideoTrackInterface* track,
290 MediaStreamInterface* stream)
291 RTC_RUN_ON(signaling_thread());
292 void OnVideoTrackRemoved(VideoTrackInterface* track,
293 MediaStreamInterface* stream)
294 RTC_RUN_ON(signaling_thread());
295
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000296 // | desc_type | is the type of the description that caused the rollback.
297 RTCError Rollback(SdpType desc_type);
298 void OnOperationsChainEmpty();
299
300 // Runs the algorithm **set the associated remote streams** specified in
301 // https://w3c.github.io/webrtc-pc/#set-associated-remote-streams.
302 void SetAssociatedRemoteStreams(
303 rtc::scoped_refptr<RtpReceiverInternal> receiver,
304 const std::vector<std::string>& stream_ids,
305 std::vector<rtc::scoped_refptr<MediaStreamInterface>>* added_streams,
306 std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams);
307
308 bool CheckIfNegotiationIsNeeded();
309 void GenerateNegotiationNeededEvent();
310 // Helper method which verifies SDP.
Henrik Boströmf8187e02021-04-26 21:04:26 +0200311 RTCError ValidateSessionDescription(
312 const SessionDescriptionInterface* sdesc,
313 cricket::ContentSource source,
314 const std::map<std::string, const cricket::ContentGroup*>&
315 bundle_groups_by_mid) RTC_RUN_ON(signaling_thread());
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000316
317 // Updates the local RtpTransceivers according to the JSEP rules. Called as
318 // part of setting the local/remote description.
319 RTCError UpdateTransceiversAndDataChannels(
320 cricket::ContentSource source,
321 const SessionDescriptionInterface& new_session,
322 const SessionDescriptionInterface* old_local_description,
Henrik Boströmf8187e02021-04-26 21:04:26 +0200323 const SessionDescriptionInterface* old_remote_description,
324 const std::map<std::string, const cricket::ContentGroup*>&
325 bundle_groups_by_mid);
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000326
327 // Associate the given transceiver according to the JSEP rules.
328 RTCErrorOr<
329 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>>
330 AssociateTransceiver(cricket::ContentSource source,
331 SdpType type,
332 size_t mline_index,
333 const cricket::ContentInfo& content,
334 const cricket::ContentInfo* old_local_content,
335 const cricket::ContentInfo* old_remote_content)
336 RTC_RUN_ON(signaling_thread());
337
Harald Alvestrand85466662021-04-19 21:21:36 +0000338 // Returns the media section in the given session description that is
339 // associated with the RtpTransceiver. Returns null if none found or this
340 // RtpTransceiver is not associated. Logic varies depending on the
341 // SdpSemantics specified in the configuration.
342 const cricket::ContentInfo* FindMediaSectionForTransceiver(
343 const RtpTransceiver* transceiver,
344 const SessionDescriptionInterface* sdesc) const;
345
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000346 // Either creates or destroys the transceiver's BaseChannel according to the
347 // given media section.
348 RTCError UpdateTransceiverChannel(
349 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
350 transceiver,
351 const cricket::ContentInfo& content,
352 const cricket::ContentGroup* bundle_group) RTC_RUN_ON(signaling_thread());
353
354 // Either creates or destroys the local data channel according to the given
355 // media section.
356 RTCError UpdateDataChannel(cricket::ContentSource source,
357 const cricket::ContentInfo& content,
358 const cricket::ContentGroup* bundle_group)
359 RTC_RUN_ON(signaling_thread());
Harald Alvestrandc06e3742020-10-01 10:23:33 +0000360 // Check if a call to SetLocalDescription is acceptable with a session
361 // description of the given type.
362 bool ExpectSetLocalDescription(SdpType type);
363 // Check if a call to SetRemoteDescription is acceptable with a session
364 // description of the given type.
365 bool ExpectSetRemoteDescription(SdpType type);
366
367 // The offer/answer machinery assumes the media section MID is present and
368 // unique. To support legacy end points that do not supply a=mid lines, this
369 // method will modify the session description to add MIDs generated according
370 // to the SDP semantics.
371 void FillInMissingRemoteMids(cricket::SessionDescription* remote_description);
372
373 // Returns an RtpTransciever, if available, that can be used to receive the
374 // given media type according to JSEP rules.
375 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
376 FindAvailableTransceiverToReceive(cricket::MediaType media_type) const;
377
Artem Titov880fa812021-07-30 22:30:23 +0200378 // Returns a MediaSessionOptions struct with options decided by `options`,
Harald Alvestrandc06e3742020-10-01 10:23:33 +0000379 // the local MediaStreams and DataChannels.
380 void GetOptionsForOffer(const PeerConnectionInterface::RTCOfferAnswerOptions&
381 offer_answer_options,
382 cricket::MediaSessionOptions* session_options);
383 void GetOptionsForPlanBOffer(
384 const PeerConnectionInterface::RTCOfferAnswerOptions&
385 offer_answer_options,
386 cricket::MediaSessionOptions* session_options)
387 RTC_RUN_ON(signaling_thread());
388 void GetOptionsForUnifiedPlanOffer(
389 const PeerConnectionInterface::RTCOfferAnswerOptions&
390 offer_answer_options,
391 cricket::MediaSessionOptions* session_options)
392 RTC_RUN_ON(signaling_thread());
393
394 // Returns a MediaSessionOptions struct with options decided by
Artem Titov880fa812021-07-30 22:30:23 +0200395 // `constraints`, the local MediaStreams and DataChannels.
Harald Alvestrandc06e3742020-10-01 10:23:33 +0000396 void GetOptionsForAnswer(const PeerConnectionInterface::RTCOfferAnswerOptions&
397 offer_answer_options,
398 cricket::MediaSessionOptions* session_options);
399 void GetOptionsForPlanBAnswer(
400 const PeerConnectionInterface::RTCOfferAnswerOptions&
401 offer_answer_options,
402 cricket::MediaSessionOptions* session_options)
403 RTC_RUN_ON(signaling_thread());
404 void GetOptionsForUnifiedPlanAnswer(
405 const PeerConnectionInterface::RTCOfferAnswerOptions&
406 offer_answer_options,
407 cricket::MediaSessionOptions* session_options)
408 RTC_RUN_ON(signaling_thread());
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000409
Harald Alvestranda474fbf2020-10-01 16:47:23 +0000410 const char* SessionErrorToString(SessionError error) const;
411 std::string GetSessionErrorMsg();
412 // Returns the last error in the session. See the enum above for details.
413 SessionError session_error() const {
414 RTC_DCHECK_RUN_ON(signaling_thread());
415 return session_error_;
416 }
417 const std::string& session_error_desc() const { return session_error_desc_; }
418
419 RTCError HandleLegacyOfferOptions(
420 const PeerConnectionInterface::RTCOfferAnswerOptions& options);
421 void RemoveRecvDirectionFromReceivingTransceiversOfType(
422 cricket::MediaType media_type) RTC_RUN_ON(signaling_thread());
423 void AddUpToOneReceivingTransceiverOfType(cricket::MediaType media_type);
424
425 std::vector<
426 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>>
427 GetReceivingTransceiversOfType(cricket::MediaType media_type)
428 RTC_RUN_ON(signaling_thread());
429
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000430 // Runs the algorithm specified in
431 // https://w3c.github.io/webrtc-pc/#process-remote-track-removal
Harald Alvestranda474fbf2020-10-01 16:47:23 +0000432 // This method will update the following lists:
Artem Titov880fa812021-07-30 22:30:23 +0200433 // `remove_list` is the list of transceivers for which the receiving track is
Harald Alvestranda474fbf2020-10-01 16:47:23 +0000434 // being removed.
Artem Titov880fa812021-07-30 22:30:23 +0200435 // `removed_streams` is the list of streams which no longer have a receiving
Harald Alvestranda474fbf2020-10-01 16:47:23 +0000436 // track so should be removed.
Harald Alvestranda474fbf2020-10-01 16:47:23 +0000437 void ProcessRemovalOfRemoteTrack(
Harald Alvestrand85466662021-04-19 21:21:36 +0000438 const rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
Harald Alvestranda474fbf2020-10-01 16:47:23 +0000439 transceiver,
440 std::vector<rtc::scoped_refptr<RtpTransceiverInterface>>* remove_list,
441 std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams);
442
443 void RemoveRemoteStreamsIfEmpty(
444 const std::vector<rtc::scoped_refptr<MediaStreamInterface>>&
445 remote_streams,
446 std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams);
447
Artem Titov880fa812021-07-30 22:30:23 +0200448 // Remove all local and remote senders of type `media_type`.
Harald Alvestranda474fbf2020-10-01 16:47:23 +0000449 // Called when a media type is rejected (m-line set to port 0).
450 void RemoveSenders(cricket::MediaType media_type);
451
Artem Titov880fa812021-07-30 22:30:23 +0200452 // Loops through the vector of `streams` and finds added and removed
Harald Alvestranda474fbf2020-10-01 16:47:23 +0000453 // StreamParams since last time this method was called.
454 // For each new or removed StreamParam, OnLocalSenderSeen or
455 // OnLocalSenderRemoved is invoked.
456 void UpdateLocalSenders(const std::vector<cricket::StreamParams>& streams,
457 cricket::MediaType media_type);
458
Artem Titov880fa812021-07-30 22:30:23 +0200459 // Makes sure a MediaStreamTrack is created for each StreamParam in `streams`,
Harald Alvestranda474fbf2020-10-01 16:47:23 +0000460 // and existing MediaStreamTracks are removed if there is no corresponding
Artem Titov880fa812021-07-30 22:30:23 +0200461 // StreamParam. If `default_track_needed` is true, a default MediaStreamTrack
Harald Alvestranda474fbf2020-10-01 16:47:23 +0000462 // is created if it doesn't exist; if false, it's removed if it exists.
Artem Titov880fa812021-07-30 22:30:23 +0200463 // `media_type` is the type of the `streams` and can be either audio or video.
464 // If a new MediaStream is created it is added to `new_streams`.
Harald Alvestranda474fbf2020-10-01 16:47:23 +0000465 void UpdateRemoteSendersList(
466 const std::vector<cricket::StreamParams>& streams,
467 bool default_track_needed,
468 cricket::MediaType media_type,
469 StreamCollection* new_streams);
470
Taylor Brandstetterd0acbd82021-01-25 13:44:55 -0800471 // Enables media channels to allow sending of media.
Harald Alvestrand48171ec2021-04-20 15:06:03 +0000472 // This enables media to flow on all configured audio/video channels.
Taylor Brandstetterd0acbd82021-01-25 13:44:55 -0800473 void EnableSending();
Harald Alvestranda474fbf2020-10-01 16:47:23 +0000474 // Push the media parts of the local or remote session description
Taylor Brandstetterd0acbd82021-01-25 13:44:55 -0800475 // down to all of the channels.
Henrik Boströmf8187e02021-04-26 21:04:26 +0200476 RTCError PushdownMediaDescription(
477 SdpType type,
478 cricket::ContentSource source,
479 const std::map<std::string, const cricket::ContentGroup*>&
480 bundle_groups_by_mid);
Harald Alvestranda474fbf2020-10-01 16:47:23 +0000481
482 RTCError PushdownTransportDescription(cricket::ContentSource source,
483 SdpType type);
484 // Helper function to remove stopped transceivers.
485 void RemoveStoppedTransceivers();
Artem Titov880fa812021-07-30 22:30:23 +0200486 // Deletes the corresponding channel of contents that don't exist in `desc`.
487 // `desc` can be null. This means that all channels are deleted.
Harald Alvestranda474fbf2020-10-01 16:47:23 +0000488 void RemoveUnusedChannels(const cricket::SessionDescription* desc);
489
490 // Report inferred negotiated SDP semantics from a local/remote answer to the
491 // UMA observer.
492 void ReportNegotiatedSdpSemantics(const SessionDescriptionInterface& answer);
493
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000494 // Finds remote MediaStreams without any tracks and removes them from
Artem Titov880fa812021-07-30 22:30:23 +0200495 // `remote_streams_` and notifies the observer that the MediaStreams no longer
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000496 // exist.
497 void UpdateEndedRemoteMediaStreams();
498
Artem Titov880fa812021-07-30 22:30:23 +0200499 // Uses all remote candidates in `remote_desc` in this session.
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000500 bool UseCandidatesInSessionDescription(
501 const SessionDescriptionInterface* remote_desc);
Artem Titov880fa812021-07-30 22:30:23 +0200502 // Uses `candidate` in this session.
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000503 bool UseCandidate(const IceCandidateInterface* candidate);
504 // Returns true if we are ready to push down the remote candidate.
Artem Titov880fa812021-07-30 22:30:23 +0200505 // `remote_desc` is the new remote description, or NULL if the current remote
506 // description should be used. Output `valid` is true if the candidate media
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000507 // index is valid.
508 bool ReadyToUseRemoteCandidate(const IceCandidateInterface* candidate,
509 const SessionDescriptionInterface* remote_desc,
510 bool* valid);
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000511
512 RTCErrorOr<const cricket::ContentInfo*> FindContentInfo(
513 const SessionDescriptionInterface* description,
514 const IceCandidateInterface* candidate) RTC_RUN_ON(signaling_thread());
515
516 // Functions for dealing with transports.
517 // Note that cricket code uses the term "channel" for what other code
518 // refers to as "transport".
519
Artem Titov880fa812021-07-30 22:30:23 +0200520 // Allocates media channels based on the `desc`. If `desc` doesn't have
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000521 // the BUNDLE option, this method will disable BUNDLE in PortAllocator.
522 // This method will also delete any existing media channels before creating.
523 RTCError CreateChannels(const cricket::SessionDescription& desc);
524
525 // Helper methods to create media channels.
526 cricket::VoiceChannel* CreateVoiceChannel(const std::string& mid);
527 cricket::VideoChannel* CreateVideoChannel(const std::string& mid);
528 bool CreateDataChannel(const std::string& mid);
529
530 // Destroys and clears the BaseChannel associated with the given transceiver,
531 // if such channel is set.
532 void DestroyTransceiverChannel(
533 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
534 transceiver);
535
536 // Destroys the RTP data channel transport and/or the SCTP data channel
537 // transport and clears it.
Florent Castellidcb9ffc2021-06-29 14:58:23 +0200538 void DestroyDataChannelTransport(RTCError error);
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000539
540 // Destroys the given ChannelInterface.
541 // The channel cannot be accessed after this method is called.
542 void DestroyChannelInterface(cricket::ChannelInterface* channel);
Artem Titov880fa812021-07-30 22:30:23 +0200543 // Generates MediaDescriptionOptions for the `session_opts` based on existing
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000544 // local description or remote description.
545
546 void GenerateMediaDescriptionOptions(
547 const SessionDescriptionInterface* session_desc,
548 RtpTransceiverDirection audio_direction,
549 RtpTransceiverDirection video_direction,
550 absl::optional<size_t>* audio_index,
551 absl::optional<size_t>* video_index,
552 absl::optional<size_t>* data_index,
553 cricket::MediaSessionOptions* session_options);
554
555 // Generates the active MediaDescriptionOptions for the local data channel
556 // given the specified MID.
557 cricket::MediaDescriptionOptions GetMediaDescriptionOptionsForActiveData(
558 const std::string& mid) const;
559
560 // Generates the rejected MediaDescriptionOptions for the local data channel
561 // given the specified MID.
562 cricket::MediaDescriptionOptions GetMediaDescriptionOptionsForRejectedData(
563 const std::string& mid) const;
564
Taylor Brandstetterd0acbd82021-01-25 13:44:55 -0800565 // Based on number of transceivers per media type, enabled or disable
566 // payload type based demuxing in the affected channels.
Henrik Boströmf8187e02021-04-26 21:04:26 +0200567 bool UpdatePayloadTypeDemuxingState(
568 cricket::ContentSource source,
569 const std::map<std::string, const cricket::ContentGroup*>&
570 bundle_groups_by_mid);
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000571
Harald Alvestrand42b983a2021-10-19 21:50:01 +0000572 // Updates the error state, signaling if necessary.
573 void SetSessionError(SessionError error, const std::string& error_desc);
574
575 // Implements AddIceCandidate without reporting usage, but returns the
576 // particular success/error value that should be reported (and can be utilized
577 // for other purposes).
578 AddIceCandidateResult AddIceCandidateInternal(
579 const IceCandidateInterface* candidate);
580
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000581 // ==================================================================
582 // Access to pc_ variables
583 cricket::ChannelManager* channel_manager() const;
Harald Alvestrande15fb152020-10-19 13:28:05 +0000584 TransceiverList* transceivers();
585 const TransceiverList* transceivers() const;
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000586 DataChannelController* data_channel_controller();
587 const DataChannelController* data_channel_controller() const;
588 cricket::PortAllocator* port_allocator();
589 const cricket::PortAllocator* port_allocator() const;
Harald Alvestrande15fb152020-10-19 13:28:05 +0000590 RtpTransmissionManager* rtp_manager();
591 const RtpTransmissionManager* rtp_manager() const;
Harald Alvestrandf01bd6c2020-10-23 13:30:46 +0000592 JsepTransportController* transport_controller();
593 const JsepTransportController* transport_controller() const;
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000594 // ===================================================================
Harald Alvestrand763f5a92020-10-22 10:39:40 +0000595 const cricket::AudioOptions& audio_options() { return audio_options_; }
596 const cricket::VideoOptions& video_options() { return video_options_; }
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000597
598 PeerConnection* const pc_;
599
600 std::unique_ptr<WebRtcSessionDescriptionFactory> webrtc_session_desc_factory_
601 RTC_GUARDED_BY(signaling_thread());
602
603 std::unique_ptr<SessionDescriptionInterface> current_local_description_
604 RTC_GUARDED_BY(signaling_thread());
605 std::unique_ptr<SessionDescriptionInterface> pending_local_description_
606 RTC_GUARDED_BY(signaling_thread());
607 std::unique_ptr<SessionDescriptionInterface> current_remote_description_
608 RTC_GUARDED_BY(signaling_thread());
609 std::unique_ptr<SessionDescriptionInterface> pending_remote_description_
610 RTC_GUARDED_BY(signaling_thread());
611
612 PeerConnectionInterface::SignalingState signaling_state_
613 RTC_GUARDED_BY(signaling_thread()) = PeerConnectionInterface::kStable;
614
615 // Whether this peer is the caller. Set when the local description is applied.
616 absl::optional<bool> is_caller_ RTC_GUARDED_BY(signaling_thread());
617
Harald Alvestrand6f04b652020-10-09 11:42:17 +0000618 // Streams added via AddStream.
619 const rtc::scoped_refptr<StreamCollection> local_streams_
620 RTC_GUARDED_BY(signaling_thread());
621 // Streams created as a result of SetRemoteDescription.
622 const rtc::scoped_refptr<StreamCollection> remote_streams_
623 RTC_GUARDED_BY(signaling_thread());
624
625 std::vector<std::unique_ptr<MediaStreamObserver>> stream_observers_
626 RTC_GUARDED_BY(signaling_thread());
627
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000628 // The operations chain is used by the offer/answer exchange methods to ensure
629 // they are executed in the right order. For example, if
630 // SetRemoteDescription() is invoked while CreateOffer() is still pending, the
631 // SRD operation will not start until CreateOffer() has completed. See
632 // https://w3c.github.io/webrtc-pc/#dfn-operations-chain.
633 rtc::scoped_refptr<rtc::OperationsChain> operations_chain_
634 RTC_GUARDED_BY(signaling_thread());
635
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000636 // One PeerConnection has only one RTCP CNAME.
637 // https://tools.ietf.org/html/draft-ietf-rtcweb-rtp-usage-26#section-4.9
638 const std::string rtcp_cname_;
639
640 // MIDs will be generated using this generator which will keep track of
641 // all the MIDs that have been seen over the life of the PeerConnection.
642 rtc::UniqueStringGenerator mid_generator_ RTC_GUARDED_BY(signaling_thread());
643
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000644 // List of content names for which the remote side triggered an ICE restart.
645 std::set<std::string> pending_ice_restarts_
646 RTC_GUARDED_BY(signaling_thread());
647
648 std::unique_ptr<LocalIceCredentialsToReplace>
649 local_ice_credentials_to_replace_ RTC_GUARDED_BY(signaling_thread());
650
651 bool remote_peer_supports_msid_ RTC_GUARDED_BY(signaling_thread()) = false;
652 bool is_negotiation_needed_ RTC_GUARDED_BY(signaling_thread()) = false;
Harald Alvestrand42b983a2021-10-19 21:50:01 +0000653 uint32_t negotiation_needed_event_id_ RTC_GUARDED_BY(signaling_thread()) = 0;
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000654 bool update_negotiation_needed_on_empty_chain_
655 RTC_GUARDED_BY(signaling_thread()) = false;
Henrik Boström4ea80f32021-06-09 10:29:50 +0200656 // If PT demuxing is successfully negotiated one time we will allow PT
657 // demuxing for the rest of the session so that PT-based apps default to PT
658 // demuxing in follow-up O/A exchanges.
Harald Alvestrand42b983a2021-10-19 21:50:01 +0000659 bool pt_demuxing_has_been_used_audio_ RTC_GUARDED_BY(signaling_thread()) =
660 false;
661 bool pt_demuxing_has_been_used_video_ RTC_GUARDED_BY(signaling_thread()) =
662 false;
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000663
664 // In Unified Plan, if we encounter remote SDP that does not contain an a=msid
665 // line we create and use a stream with a random ID for our receivers. This is
666 // to support legacy endpoints that do not support the a=msid attribute (as
667 // opposed to streamless tracks with "a=msid:-").
668 rtc::scoped_refptr<MediaStreamInterface> missing_msid_default_stream_
669 RTC_GUARDED_BY(signaling_thread());
670
Harald Alvestranda474fbf2020-10-01 16:47:23 +0000671 SessionError session_error_ RTC_GUARDED_BY(signaling_thread()) =
672 SessionError::kNone;
673 std::string session_error_desc_ RTC_GUARDED_BY(signaling_thread());
674
Harald Alvestrand763f5a92020-10-22 10:39:40 +0000675 // Member variables for caching global options.
676 cricket::AudioOptions audio_options_ RTC_GUARDED_BY(signaling_thread());
677 cricket::VideoOptions video_options_ RTC_GUARDED_BY(signaling_thread());
678
679 // This object should be used to generate any SSRC that is not explicitly
680 // specified by the user (or by the remote party).
681 // The generator is not used directly, instead it is passed on to the
682 // channel manager and the session description factory.
Tomas Gunnarsson64099bc2021-04-09 09:51:37 +0200683 // TODO(bugs.webrtc.org/12666): This variable is used from both the signaling
684 // and worker threads. See if we can't restrict usage to a single thread.
685 rtc::UniqueRandomIdGenerator ssrc_generator_;
Harald Alvestrand763f5a92020-10-22 10:39:40 +0000686
687 // A video bitrate allocator factory.
688 // This can be injected using the PeerConnectionDependencies,
689 // or else the CreateBuiltinVideoBitrateAllocatorFactory() will be called.
690 // Note that one can still choose to override this in a MediaEngine
691 // if one wants too.
692 std::unique_ptr<webrtc::VideoBitrateAllocatorFactory>
Harald Alvestrand42b983a2021-10-19 21:50:01 +0000693 video_bitrate_allocator_factory_ RTC_GUARDED_BY(signaling_thread());
Harald Alvestrand763f5a92020-10-22 10:39:40 +0000694
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000695 rtc::WeakPtrFactory<SdpOfferAnswerHandler> weak_ptr_factory_
696 RTC_GUARDED_BY(signaling_thread());
697};
698
699} // namespace webrtc
700
701#endif // PC_SDP_OFFER_ANSWER_H_