blob: 2124ed86979d8f6eea09186d974191605252e732 [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>
Harald Alvestrandcdcfab02020-09-28 13:02:07 +000022#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"
Tomas Gunnarsson2efb8a52021-04-01 16:26:57 +020039#include "api/uma_metrics.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"
Harald Alvestrand8e344192022-02-08 12:55:42 +000044#include "pc/connection_context.h"
Harald Alvestrandcdcfab02020-09-28 13:02:07 +000045#include "pc/data_channel_controller.h"
Harald Alvestrandcdcfab02020-09-28 13:02:07 +000046#include "pc/jsep_transport_controller.h"
Harald Alvestrand1f7eab62020-10-18 16:51:47 +000047#include "pc/media_session.h"
48#include "pc/media_stream_observer.h"
Harald Alvestrandcdcfab02020-09-28 13:02:07 +000049#include "pc/peer_connection_internal.h"
Harald Alvestrand1f7eab62020-10-18 16:51:47 +000050#include "pc/rtp_receiver.h"
Harald Alvestrandcdcfab02020-09-28 13:02:07 +000051#include "pc/rtp_transceiver.h"
Harald Alvestrand763f5a92020-10-22 10:39:40 +000052#include "pc/rtp_transmission_manager.h"
Harald Alvestrandf01bd6c2020-10-23 13:30:46 +000053#include "pc/sdp_state_provider.h"
Harald Alvestrand1f7eab62020-10-18 16:51:47 +000054#include "pc/session_description.h"
Harald Alvestrandcdcfab02020-09-28 13:02:07 +000055#include "pc/stream_collection.h"
Harald Alvestrand1f7eab62020-10-18 16:51:47 +000056#include "pc/transceiver_list.h"
Harald Alvestrandcdcfab02020-09-28 13:02:07 +000057#include "pc/webrtc_session_description_factory.h"
Harald Alvestrand1f7eab62020-10-18 16:51:47 +000058#include "rtc_base/checks.h"
Harald Alvestrandcdcfab02020-09-28 13:02:07 +000059#include "rtc_base/operations_chain.h"
Harald Alvestrandf01bd6c2020-10-23 13:30:46 +000060#include "rtc_base/ssl_stream_adapter.h"
Harald Alvestrand1f7eab62020-10-18 16:51:47 +000061#include "rtc_base/thread.h"
62#include "rtc_base/thread_annotations.h"
Harald Alvestrandcdcfab02020-09-28 13:02:07 +000063#include "rtc_base/unique_id_generator.h"
64#include "rtc_base/weak_ptr.h"
65
66namespace webrtc {
67
Harald Alvestrandcdcfab02020-09-28 13:02:07 +000068// SdpOfferAnswerHandler is a component
69// of the PeerConnection object as defined
70// by the PeerConnectionInterface API surface.
71// The class is responsible for the following:
72// - Parsing and interpreting SDP.
73// - Generating offers and answers based on the current state.
74// This class lives on the signaling thread.
Niels Möller61892072022-06-23 13:14:22 +020075class SdpOfferAnswerHandler : public SdpStateProvider {
Harald Alvestrandcdcfab02020-09-28 13:02:07 +000076 public:
Harald Alvestrandcdcfab02020-09-28 13:02:07 +000077 ~SdpOfferAnswerHandler();
78
Harald Alvestrand9cd199d2020-10-27 07:10:43 +000079 // Creates an SdpOfferAnswerHandler. Modifies dependencies.
80 static std::unique_ptr<SdpOfferAnswerHandler> Create(
Harald Alvestrand5b661302022-01-28 13:08:34 +000081 PeerConnectionSdpMethods* pc,
Harald Alvestrand763f5a92020-10-22 10:39:40 +000082 const PeerConnectionInterface::RTCConfiguration& configuration,
Harald Alvestrand66c40362022-01-28 17:41:30 +000083 PeerConnectionDependencies& dependencies,
84 ConnectionContext* context);
Harald Alvestrand763f5a92020-10-22 10:39:40 +000085
Harald Alvestrandcdcfab02020-09-28 13:02:07 +000086 void ResetSessionDescFactory() {
87 RTC_DCHECK_RUN_ON(signaling_thread());
88 webrtc_session_desc_factory_.reset();
89 }
90 const WebRtcSessionDescriptionFactory* webrtc_session_desc_factory() const {
91 RTC_DCHECK_RUN_ON(signaling_thread());
92 return webrtc_session_desc_factory_.get();
93 }
94
95 // Change signaling state to Closed, and perform appropriate actions.
96 void Close();
97
98 // Called as part of destroying the owning PeerConnection.
99 void PrepareForShutdown();
100
Harald Alvestrandf01bd6c2020-10-23 13:30:46 +0000101 // Implementation of SdpStateProvider
102 PeerConnectionInterface::SignalingState signaling_state() const override;
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000103
Harald Alvestrandf01bd6c2020-10-23 13:30:46 +0000104 const SessionDescriptionInterface* local_description() const override;
105 const SessionDescriptionInterface* remote_description() const override;
106 const SessionDescriptionInterface* current_local_description() const override;
107 const SessionDescriptionInterface* current_remote_description()
108 const override;
109 const SessionDescriptionInterface* pending_local_description() const override;
110 const SessionDescriptionInterface* pending_remote_description()
111 const override;
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000112
Harald Alvestrandf01bd6c2020-10-23 13:30:46 +0000113 bool NeedsIceRestart(const std::string& content_name) const override;
114 bool IceRestartPending(const std::string& content_name) const override;
115 absl::optional<rtc::SSLRole> GetDtlsRole(
116 const std::string& mid) const override;
Harald Alvestrandd89ce532020-10-21 08:59:22 +0000117
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000118 void RestartIce();
119
120 // JSEP01
121 void CreateOffer(
122 CreateSessionDescriptionObserver* observer,
123 const PeerConnectionInterface::RTCOfferAnswerOptions& options);
124 void CreateAnswer(
125 CreateSessionDescriptionObserver* observer,
126 const PeerConnectionInterface::RTCOfferAnswerOptions& options);
127
128 void SetLocalDescription(
129 std::unique_ptr<SessionDescriptionInterface> desc,
130 rtc::scoped_refptr<SetLocalDescriptionObserverInterface> observer);
131 void SetLocalDescription(
132 rtc::scoped_refptr<SetLocalDescriptionObserverInterface> observer);
133 void SetLocalDescription(SetSessionDescriptionObserver* observer,
134 SessionDescriptionInterface* desc);
135 void SetLocalDescription(SetSessionDescriptionObserver* observer);
136
137 void SetRemoteDescription(
138 std::unique_ptr<SessionDescriptionInterface> desc,
139 rtc::scoped_refptr<SetRemoteDescriptionObserverInterface> observer);
140 void SetRemoteDescription(SetSessionDescriptionObserver* observer,
141 SessionDescriptionInterface* desc);
142
143 PeerConnectionInterface::RTCConfiguration GetConfiguration();
144 RTCError SetConfiguration(
145 const PeerConnectionInterface::RTCConfiguration& configuration);
146 bool AddIceCandidate(const IceCandidateInterface* candidate);
147 void AddIceCandidate(std::unique_ptr<IceCandidateInterface> candidate,
148 std::function<void(RTCError)> callback);
149 bool RemoveIceCandidates(const std::vector<cricket::Candidate>& candidates);
150 // Adds a locally generated candidate to the local description.
151 void AddLocalIceCandidate(const JsepIceCandidate* candidate);
152 void RemoveLocalIceCandidates(
153 const std::vector<cricket::Candidate>& candidates);
154 bool ShouldFireNegotiationNeededEvent(uint32_t event_id);
155
Harald Alvestrand6f04b652020-10-09 11:42:17 +0000156 bool AddStream(MediaStreamInterface* local_stream);
157 void RemoveStream(MediaStreamInterface* local_stream);
158
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000159 absl::optional<bool> is_caller();
160 bool HasNewIceCredentials();
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000161 void UpdateNegotiationNeeded();
162
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000163 // Destroys all BaseChannels and destroys the SCTP data channel, if present.
164 void DestroyAllChannels();
165
Harald Alvestrand6f04b652020-10-09 11:42:17 +0000166 rtc::scoped_refptr<StreamCollectionInterface> local_streams();
167 rtc::scoped_refptr<StreamCollectionInterface> remote_streams();
168
Harald Alvestrandbc32c562022-02-09 12:08:47 +0000169 bool initial_offerer() {
170 RTC_DCHECK_RUN_ON(signaling_thread());
171 if (initial_offerer_) {
172 return *initial_offerer_;
173 }
174 return false;
175 }
176
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000177 private:
Tomas Gunnarsson1c7c09b2022-01-12 13:11:04 +0100178 class RemoteDescriptionOperation;
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000179 class ImplicitCreateSessionDescriptionObserver;
Harald Alvestrand1f7eab62020-10-18 16:51:47 +0000180
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000181 friend class ImplicitCreateSessionDescriptionObserver;
182 class SetSessionDescriptionObserverAdapter;
Harald Alvestrand1f7eab62020-10-18 16:51:47 +0000183
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000184 friend class SetSessionDescriptionObserverAdapter;
185
Harald Alvestranda474fbf2020-10-01 16:47:23 +0000186 enum class SessionError {
187 kNone, // No error.
188 kContent, // Error in BaseChannel SetLocalContent/SetRemoteContent.
189 kTransport, // Error from the underlying transport.
190 };
191
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000192 // Represents the [[LocalIceCredentialsToReplace]] internal slot in the spec.
193 // It makes the next CreateOffer() produce new ICE credentials even if
194 // RTCOfferAnswerOptions::ice_restart is false.
195 // https://w3c.github.io/webrtc-pc/#dfn-localufragstoreplace
196 // TODO(hbos): When JsepTransportController/JsepTransport supports rollback,
197 // move this type of logic to JsepTransportController/JsepTransport.
198 class LocalIceCredentialsToReplace;
199
Harald Alvestrand9cd199d2020-10-27 07:10:43 +0000200 // Only called by the Create() function.
Harald Alvestrand66c40362022-01-28 17:41:30 +0000201 explicit SdpOfferAnswerHandler(PeerConnectionSdpMethods* pc,
202 ConnectionContext* context);
Harald Alvestrand9cd199d2020-10-27 07:10:43 +0000203 // Called from the `Create()` function. Can only be called
204 // once. Modifies dependencies.
205 void Initialize(
206 const PeerConnectionInterface::RTCConfiguration& configuration,
Jonas Orelanded99dae2022-03-09 09:28:10 +0100207 PeerConnectionDependencies& dependencies,
208 ConnectionContext* context);
Harald Alvestrand9cd199d2020-10-27 07:10:43 +0000209
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000210 rtc::Thread* signaling_thread() const;
Harald Alvestrandbc32c562022-02-09 12:08:47 +0000211 rtc::Thread* network_thread() const;
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000212 // Non-const versions of local_description()/remote_description(), for use
213 // internally.
214 SessionDescriptionInterface* mutable_local_description()
215 RTC_RUN_ON(signaling_thread()) {
216 return pending_local_description_ ? pending_local_description_.get()
217 : current_local_description_.get();
218 }
219 SessionDescriptionInterface* mutable_remote_description()
220 RTC_RUN_ON(signaling_thread()) {
221 return pending_remote_description_ ? pending_remote_description_.get()
222 : current_remote_description_.get();
223 }
224
225 // Synchronous implementations of SetLocalDescription/SetRemoteDescription
226 // that return an RTCError instead of invoking a callback.
227 RTCError ApplyLocalDescription(
Henrik Boströmf8187e02021-04-26 21:04:26 +0200228 std::unique_ptr<SessionDescriptionInterface> desc,
229 const std::map<std::string, const cricket::ContentGroup*>&
230 bundle_groups_by_mid);
Tomas Gunnarsson0dd75392022-01-17 19:19:56 +0100231 void ApplyRemoteDescription(
232 std::unique_ptr<RemoteDescriptionOperation> operation);
233
234 RTCError ReplaceRemoteDescription(
Henrik Boströmf8187e02021-04-26 21:04:26 +0200235 std::unique_ptr<SessionDescriptionInterface> desc,
Tomas Gunnarsson0dd75392022-01-17 19:19:56 +0100236 SdpType sdp_type,
237 std::unique_ptr<SessionDescriptionInterface>* replaced_description)
238 RTC_RUN_ON(signaling_thread());
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
Niels Möllerbe74b802022-03-18 14:10:15 +0100373 // Returns an RtpTransceiver, if available, that can be used to receive the
Harald Alvestrandc06e3742020-10-01 10:23:33 +0000374 // 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
Harald Alvestrand8101e7b2022-05-23 14:57:47 +0000475 // down to all of the channels, and start SCTP if needed.
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
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000490 // Finds remote MediaStreams without any tracks and removes them from
Artem Titov880fa812021-07-30 22:30:23 +0200491 // `remote_streams_` and notifies the observer that the MediaStreams no longer
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000492 // exist.
493 void UpdateEndedRemoteMediaStreams();
494
Tomas Gunnarsson0dd75392022-01-17 19:19:56 +0100495 // Uses all remote candidates in the currently set remote_description().
496 // If no remote description is currently set (nullptr), the return value will
497 // be true. If `UseCandidate()` fails for any candidate in the remote
498 // description, the return value will be false.
499 bool UseCandidatesInRemoteDescription();
Artem Titov880fa812021-07-30 22:30:23 +0200500 // Uses `candidate` in this session.
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000501 bool UseCandidate(const IceCandidateInterface* candidate);
502 // Returns true if we are ready to push down the remote candidate.
Artem Titov880fa812021-07-30 22:30:23 +0200503 // `remote_desc` is the new remote description, or NULL if the current remote
504 // description should be used. Output `valid` is true if the candidate media
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000505 // index is valid.
506 bool ReadyToUseRemoteCandidate(const IceCandidateInterface* candidate,
507 const SessionDescriptionInterface* remote_desc,
508 bool* valid);
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000509
510 RTCErrorOr<const cricket::ContentInfo*> FindContentInfo(
511 const SessionDescriptionInterface* description,
512 const IceCandidateInterface* candidate) RTC_RUN_ON(signaling_thread());
513
514 // Functions for dealing with transports.
515 // Note that cricket code uses the term "channel" for what other code
516 // refers to as "transport".
517
Artem Titov880fa812021-07-30 22:30:23 +0200518 // Allocates media channels based on the `desc`. If `desc` doesn't have
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000519 // the BUNDLE option, this method will disable BUNDLE in PortAllocator.
520 // This method will also delete any existing media channels before creating.
521 RTCError CreateChannels(const cricket::SessionDescription& desc);
522
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000523 bool CreateDataChannel(const std::string& mid);
524
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000525 // Destroys the RTP data channel transport and/or the SCTP data channel
526 // transport and clears it.
Florent Castellidcb9ffc2021-06-29 14:58:23 +0200527 void DestroyDataChannelTransport(RTCError error);
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000528
Artem Titov880fa812021-07-30 22:30:23 +0200529 // Generates MediaDescriptionOptions for the `session_opts` based on existing
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000530 // local description or remote description.
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000531 void GenerateMediaDescriptionOptions(
532 const SessionDescriptionInterface* session_desc,
533 RtpTransceiverDirection audio_direction,
534 RtpTransceiverDirection video_direction,
535 absl::optional<size_t>* audio_index,
536 absl::optional<size_t>* video_index,
537 absl::optional<size_t>* data_index,
538 cricket::MediaSessionOptions* session_options);
539
540 // Generates the active MediaDescriptionOptions for the local data channel
541 // given the specified MID.
542 cricket::MediaDescriptionOptions GetMediaDescriptionOptionsForActiveData(
543 const std::string& mid) const;
544
545 // Generates the rejected MediaDescriptionOptions for the local data channel
546 // given the specified MID.
547 cricket::MediaDescriptionOptions GetMediaDescriptionOptionsForRejectedData(
548 const std::string& mid) const;
549
Taylor Brandstetterd0acbd82021-01-25 13:44:55 -0800550 // Based on number of transceivers per media type, enabled or disable
551 // payload type based demuxing in the affected channels.
Henrik Boströmf8187e02021-04-26 21:04:26 +0200552 bool UpdatePayloadTypeDemuxingState(
553 cricket::ContentSource source,
554 const std::map<std::string, const cricket::ContentGroup*>&
555 bundle_groups_by_mid);
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000556
Harald Alvestrand42b983a2021-10-19 21:50:01 +0000557 // Updates the error state, signaling if necessary.
558 void SetSessionError(SessionError error, const std::string& error_desc);
559
560 // Implements AddIceCandidate without reporting usage, but returns the
561 // particular success/error value that should be reported (and can be utilized
562 // for other purposes).
563 AddIceCandidateResult AddIceCandidateInternal(
564 const IceCandidateInterface* candidate);
565
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000566 // ==================================================================
567 // Access to pc_ variables
Harald Alvestrand35f4b4c2022-05-16 10:36:43 +0000568 cricket::MediaEngineInterface* media_engine() const;
Harald Alvestrande15fb152020-10-19 13:28:05 +0000569 TransceiverList* transceivers();
570 const TransceiverList* transceivers() const;
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000571 DataChannelController* data_channel_controller();
572 const DataChannelController* data_channel_controller() const;
573 cricket::PortAllocator* port_allocator();
574 const cricket::PortAllocator* port_allocator() const;
Harald Alvestrande15fb152020-10-19 13:28:05 +0000575 RtpTransmissionManager* rtp_manager();
576 const RtpTransmissionManager* rtp_manager() const;
Harald Alvestrandbc32c562022-02-09 12:08:47 +0000577 JsepTransportController* transport_controller_s()
578 RTC_RUN_ON(signaling_thread());
579 const JsepTransportController* transport_controller_s() const
580 RTC_RUN_ON(signaling_thread());
581 JsepTransportController* transport_controller_n()
582 RTC_RUN_ON(network_thread());
583 const JsepTransportController* transport_controller_n() const
584 RTC_RUN_ON(network_thread());
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000585 // ===================================================================
Harald Alvestrand763f5a92020-10-22 10:39:40 +0000586 const cricket::AudioOptions& audio_options() { return audio_options_; }
587 const cricket::VideoOptions& video_options() { return video_options_; }
Harald Alvestrand8101e7b2022-05-23 14:57:47 +0000588 bool ConfiguredForMedia() const;
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000589
Harald Alvestrand5b661302022-01-28 13:08:34 +0000590 PeerConnectionSdpMethods* const pc_;
Harald Alvestrand66c40362022-01-28 17:41:30 +0000591 ConnectionContext* const context_;
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000592
593 std::unique_ptr<WebRtcSessionDescriptionFactory> webrtc_session_desc_factory_
594 RTC_GUARDED_BY(signaling_thread());
595
596 std::unique_ptr<SessionDescriptionInterface> current_local_description_
597 RTC_GUARDED_BY(signaling_thread());
598 std::unique_ptr<SessionDescriptionInterface> pending_local_description_
599 RTC_GUARDED_BY(signaling_thread());
600 std::unique_ptr<SessionDescriptionInterface> current_remote_description_
601 RTC_GUARDED_BY(signaling_thread());
602 std::unique_ptr<SessionDescriptionInterface> pending_remote_description_
603 RTC_GUARDED_BY(signaling_thread());
604
605 PeerConnectionInterface::SignalingState signaling_state_
606 RTC_GUARDED_BY(signaling_thread()) = PeerConnectionInterface::kStable;
607
608 // Whether this peer is the caller. Set when the local description is applied.
609 absl::optional<bool> is_caller_ RTC_GUARDED_BY(signaling_thread());
610
Harald Alvestrand6f04b652020-10-09 11:42:17 +0000611 // Streams added via AddStream.
612 const rtc::scoped_refptr<StreamCollection> local_streams_
613 RTC_GUARDED_BY(signaling_thread());
614 // Streams created as a result of SetRemoteDescription.
615 const rtc::scoped_refptr<StreamCollection> remote_streams_
616 RTC_GUARDED_BY(signaling_thread());
617
618 std::vector<std::unique_ptr<MediaStreamObserver>> stream_observers_
619 RTC_GUARDED_BY(signaling_thread());
620
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000621 // The operations chain is used by the offer/answer exchange methods to ensure
622 // they are executed in the right order. For example, if
623 // SetRemoteDescription() is invoked while CreateOffer() is still pending, the
624 // SRD operation will not start until CreateOffer() has completed. See
625 // https://w3c.github.io/webrtc-pc/#dfn-operations-chain.
626 rtc::scoped_refptr<rtc::OperationsChain> operations_chain_
627 RTC_GUARDED_BY(signaling_thread());
628
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000629 // One PeerConnection has only one RTCP CNAME.
630 // https://tools.ietf.org/html/draft-ietf-rtcweb-rtp-usage-26#section-4.9
631 const std::string rtcp_cname_;
632
633 // MIDs will be generated using this generator which will keep track of
634 // all the MIDs that have been seen over the life of the PeerConnection.
635 rtc::UniqueStringGenerator mid_generator_ RTC_GUARDED_BY(signaling_thread());
636
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000637 // List of content names for which the remote side triggered an ICE restart.
638 std::set<std::string> pending_ice_restarts_
639 RTC_GUARDED_BY(signaling_thread());
640
641 std::unique_ptr<LocalIceCredentialsToReplace>
642 local_ice_credentials_to_replace_ RTC_GUARDED_BY(signaling_thread());
643
644 bool remote_peer_supports_msid_ RTC_GUARDED_BY(signaling_thread()) = false;
645 bool is_negotiation_needed_ RTC_GUARDED_BY(signaling_thread()) = false;
Harald Alvestrand42b983a2021-10-19 21:50:01 +0000646 uint32_t negotiation_needed_event_id_ RTC_GUARDED_BY(signaling_thread()) = 0;
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000647 bool update_negotiation_needed_on_empty_chain_
648 RTC_GUARDED_BY(signaling_thread()) = false;
Henrik Boström4ea80f32021-06-09 10:29:50 +0200649 // If PT demuxing is successfully negotiated one time we will allow PT
650 // demuxing for the rest of the session so that PT-based apps default to PT
651 // demuxing in follow-up O/A exchanges.
Harald Alvestrand42b983a2021-10-19 21:50:01 +0000652 bool pt_demuxing_has_been_used_audio_ RTC_GUARDED_BY(signaling_thread()) =
653 false;
654 bool pt_demuxing_has_been_used_video_ RTC_GUARDED_BY(signaling_thread()) =
655 false;
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000656
657 // In Unified Plan, if we encounter remote SDP that does not contain an a=msid
658 // line we create and use a stream with a random ID for our receivers. This is
659 // to support legacy endpoints that do not support the a=msid attribute (as
660 // opposed to streamless tracks with "a=msid:-").
661 rtc::scoped_refptr<MediaStreamInterface> missing_msid_default_stream_
662 RTC_GUARDED_BY(signaling_thread());
663
Harald Alvestranda474fbf2020-10-01 16:47:23 +0000664 SessionError session_error_ RTC_GUARDED_BY(signaling_thread()) =
665 SessionError::kNone;
666 std::string session_error_desc_ RTC_GUARDED_BY(signaling_thread());
667
Harald Alvestrand763f5a92020-10-22 10:39:40 +0000668 // Member variables for caching global options.
669 cricket::AudioOptions audio_options_ RTC_GUARDED_BY(signaling_thread());
670 cricket::VideoOptions video_options_ RTC_GUARDED_BY(signaling_thread());
671
Harald Alvestrand763f5a92020-10-22 10:39:40 +0000672 // A video bitrate allocator factory.
673 // This can be injected using the PeerConnectionDependencies,
674 // or else the CreateBuiltinVideoBitrateAllocatorFactory() will be called.
675 // Note that one can still choose to override this in a MediaEngine
676 // if one wants too.
677 std::unique_ptr<webrtc::VideoBitrateAllocatorFactory>
Harald Alvestrand42b983a2021-10-19 21:50:01 +0000678 video_bitrate_allocator_factory_ RTC_GUARDED_BY(signaling_thread());
Harald Alvestrand763f5a92020-10-22 10:39:40 +0000679
Harald Alvestrandbc32c562022-02-09 12:08:47 +0000680 // Whether we are the initial offerer on the association. This
681 // determines the SSL role.
682 absl::optional<bool> initial_offerer_ RTC_GUARDED_BY(signaling_thread());
683
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000684 rtc::WeakPtrFactory<SdpOfferAnswerHandler> weak_ptr_factory_
685 RTC_GUARDED_BY(signaling_thread());
686};
687
688} // namespace webrtc
689
690#endif // PC_SDP_OFFER_ANSWER_H_