blob: bc5087c2b35e655cd0398f2cc5a46d925515e27e [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
Harald Alvestrand9e334b72022-05-04 13:38:31 +000066namespace cricket {
67class ChannelManager;
68}
69
Harald Alvestrandcdcfab02020-09-28 13:02:07 +000070namespace webrtc {
71
Harald Alvestrandcdcfab02020-09-28 13:02:07 +000072// SdpOfferAnswerHandler is a component
73// of the PeerConnection object as defined
74// by the PeerConnectionInterface API surface.
75// The class is responsible for the following:
76// - Parsing and interpreting SDP.
77// - Generating offers and answers based on the current state.
78// This class lives on the signaling thread.
Niels Möller61892072022-06-23 13:14:22 +020079class SdpOfferAnswerHandler : public SdpStateProvider {
Harald Alvestrandcdcfab02020-09-28 13:02:07 +000080 public:
Harald Alvestrandcdcfab02020-09-28 13:02:07 +000081 ~SdpOfferAnswerHandler();
82
Harald Alvestrand9cd199d2020-10-27 07:10:43 +000083 // Creates an SdpOfferAnswerHandler. Modifies dependencies.
84 static std::unique_ptr<SdpOfferAnswerHandler> Create(
Harald Alvestrand5b661302022-01-28 13:08:34 +000085 PeerConnectionSdpMethods* pc,
Harald Alvestrand763f5a92020-10-22 10:39:40 +000086 const PeerConnectionInterface::RTCConfiguration& configuration,
Harald Alvestrand66c40362022-01-28 17:41:30 +000087 PeerConnectionDependencies& dependencies,
88 ConnectionContext* context);
Harald Alvestrand763f5a92020-10-22 10:39:40 +000089
Harald Alvestrandcdcfab02020-09-28 13:02:07 +000090 void ResetSessionDescFactory() {
91 RTC_DCHECK_RUN_ON(signaling_thread());
92 webrtc_session_desc_factory_.reset();
93 }
94 const WebRtcSessionDescriptionFactory* webrtc_session_desc_factory() const {
95 RTC_DCHECK_RUN_ON(signaling_thread());
96 return webrtc_session_desc_factory_.get();
97 }
98
99 // Change signaling state to Closed, and perform appropriate actions.
100 void Close();
101
102 // Called as part of destroying the owning PeerConnection.
103 void PrepareForShutdown();
104
Harald Alvestrandf01bd6c2020-10-23 13:30:46 +0000105 // Implementation of SdpStateProvider
106 PeerConnectionInterface::SignalingState signaling_state() const override;
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000107
Harald Alvestrandf01bd6c2020-10-23 13:30:46 +0000108 const SessionDescriptionInterface* local_description() const override;
109 const SessionDescriptionInterface* remote_description() const override;
110 const SessionDescriptionInterface* current_local_description() const override;
111 const SessionDescriptionInterface* current_remote_description()
112 const override;
113 const SessionDescriptionInterface* pending_local_description() const override;
114 const SessionDescriptionInterface* pending_remote_description()
115 const override;
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000116
Harald Alvestrandf01bd6c2020-10-23 13:30:46 +0000117 bool NeedsIceRestart(const std::string& content_name) const override;
118 bool IceRestartPending(const std::string& content_name) const override;
119 absl::optional<rtc::SSLRole> GetDtlsRole(
120 const std::string& mid) const override;
Harald Alvestrandd89ce532020-10-21 08:59:22 +0000121
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000122 void RestartIce();
123
124 // JSEP01
125 void CreateOffer(
126 CreateSessionDescriptionObserver* observer,
127 const PeerConnectionInterface::RTCOfferAnswerOptions& options);
128 void CreateAnswer(
129 CreateSessionDescriptionObserver* observer,
130 const PeerConnectionInterface::RTCOfferAnswerOptions& options);
131
132 void SetLocalDescription(
133 std::unique_ptr<SessionDescriptionInterface> desc,
134 rtc::scoped_refptr<SetLocalDescriptionObserverInterface> observer);
135 void SetLocalDescription(
136 rtc::scoped_refptr<SetLocalDescriptionObserverInterface> observer);
137 void SetLocalDescription(SetSessionDescriptionObserver* observer,
138 SessionDescriptionInterface* desc);
139 void SetLocalDescription(SetSessionDescriptionObserver* observer);
140
141 void SetRemoteDescription(
142 std::unique_ptr<SessionDescriptionInterface> desc,
143 rtc::scoped_refptr<SetRemoteDescriptionObserverInterface> observer);
144 void SetRemoteDescription(SetSessionDescriptionObserver* observer,
145 SessionDescriptionInterface* desc);
146
147 PeerConnectionInterface::RTCConfiguration GetConfiguration();
148 RTCError SetConfiguration(
149 const PeerConnectionInterface::RTCConfiguration& configuration);
150 bool AddIceCandidate(const IceCandidateInterface* candidate);
151 void AddIceCandidate(std::unique_ptr<IceCandidateInterface> candidate,
152 std::function<void(RTCError)> callback);
153 bool RemoveIceCandidates(const std::vector<cricket::Candidate>& candidates);
154 // Adds a locally generated candidate to the local description.
155 void AddLocalIceCandidate(const JsepIceCandidate* candidate);
156 void RemoveLocalIceCandidates(
157 const std::vector<cricket::Candidate>& candidates);
158 bool ShouldFireNegotiationNeededEvent(uint32_t event_id);
159
Harald Alvestrand6f04b652020-10-09 11:42:17 +0000160 bool AddStream(MediaStreamInterface* local_stream);
161 void RemoveStream(MediaStreamInterface* local_stream);
162
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000163 absl::optional<bool> is_caller();
164 bool HasNewIceCredentials();
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000165 void UpdateNegotiationNeeded();
166
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000167 // Destroys all BaseChannels and destroys the SCTP data channel, if present.
168 void DestroyAllChannels();
169
Harald Alvestrand6f04b652020-10-09 11:42:17 +0000170 rtc::scoped_refptr<StreamCollectionInterface> local_streams();
171 rtc::scoped_refptr<StreamCollectionInterface> remote_streams();
172
Harald Alvestrandbc32c562022-02-09 12:08:47 +0000173 bool initial_offerer() {
174 RTC_DCHECK_RUN_ON(signaling_thread());
175 if (initial_offerer_) {
176 return *initial_offerer_;
177 }
178 return false;
179 }
180
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000181 private:
Tomas Gunnarsson1c7c09b2022-01-12 13:11:04 +0100182 class RemoteDescriptionOperation;
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000183 class ImplicitCreateSessionDescriptionObserver;
Harald Alvestrand1f7eab62020-10-18 16:51:47 +0000184
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000185 friend class ImplicitCreateSessionDescriptionObserver;
186 class SetSessionDescriptionObserverAdapter;
Harald Alvestrand1f7eab62020-10-18 16:51:47 +0000187
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000188 friend class SetSessionDescriptionObserverAdapter;
189
Harald Alvestranda474fbf2020-10-01 16:47:23 +0000190 enum class SessionError {
191 kNone, // No error.
192 kContent, // Error in BaseChannel SetLocalContent/SetRemoteContent.
193 kTransport, // Error from the underlying transport.
194 };
195
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000196 // Represents the [[LocalIceCredentialsToReplace]] internal slot in the spec.
197 // It makes the next CreateOffer() produce new ICE credentials even if
198 // RTCOfferAnswerOptions::ice_restart is false.
199 // https://w3c.github.io/webrtc-pc/#dfn-localufragstoreplace
200 // TODO(hbos): When JsepTransportController/JsepTransport supports rollback,
201 // move this type of logic to JsepTransportController/JsepTransport.
202 class LocalIceCredentialsToReplace;
203
Harald Alvestrand9cd199d2020-10-27 07:10:43 +0000204 // Only called by the Create() function.
Harald Alvestrand66c40362022-01-28 17:41:30 +0000205 explicit SdpOfferAnswerHandler(PeerConnectionSdpMethods* pc,
206 ConnectionContext* context);
Harald Alvestrand9cd199d2020-10-27 07:10:43 +0000207 // Called from the `Create()` function. Can only be called
208 // once. Modifies dependencies.
209 void Initialize(
210 const PeerConnectionInterface::RTCConfiguration& configuration,
Jonas Orelanded99dae2022-03-09 09:28:10 +0100211 PeerConnectionDependencies& dependencies,
212 ConnectionContext* context);
Harald Alvestrand9cd199d2020-10-27 07:10:43 +0000213
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000214 rtc::Thread* signaling_thread() const;
Harald Alvestrandbc32c562022-02-09 12:08:47 +0000215 rtc::Thread* network_thread() const;
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000216 // 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);
Tomas Gunnarsson0dd75392022-01-17 19:19:56 +0100235 void ApplyRemoteDescription(
236 std::unique_ptr<RemoteDescriptionOperation> operation);
237
238 RTCError ReplaceRemoteDescription(
Henrik Boströmf8187e02021-04-26 21:04:26 +0200239 std::unique_ptr<SessionDescriptionInterface> desc,
Tomas Gunnarsson0dd75392022-01-17 19:19:56 +0100240 SdpType sdp_type,
241 std::unique_ptr<SessionDescriptionInterface>* replaced_description)
242 RTC_RUN_ON(signaling_thread());
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000243
Tomas Gunnarsson651586c2022-01-07 18:33:12 +0000244 // Part of ApplyRemoteDescription steps specific to Unified Plan.
245 void ApplyRemoteDescriptionUpdateTransceiverState(SdpType sdp_type);
246
Tomas Gunnarssonb625edf2022-01-07 16:22:14 +0000247 // Part of ApplyRemoteDescription steps specific to plan b.
248 void PlanBUpdateSendersAndReceivers(
249 const cricket::ContentInfo* audio_content,
250 const cricket::AudioContentDescription* audio_desc,
251 const cricket::ContentInfo* video_content,
252 const cricket::VideoContentDescription* video_desc);
253
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000254 // Implementation of the offer/answer exchange operations. These are chained
Artem Titov880fa812021-07-30 22:30:23 +0200255 // onto the `operations_chain_` when the public CreateOffer(), CreateAnswer(),
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000256 // SetLocalDescription() and SetRemoteDescription() methods are invoked.
257 void DoCreateOffer(
258 const PeerConnectionInterface::RTCOfferAnswerOptions& options,
259 rtc::scoped_refptr<CreateSessionDescriptionObserver> observer);
260 void DoCreateAnswer(
261 const PeerConnectionInterface::RTCOfferAnswerOptions& options,
262 rtc::scoped_refptr<CreateSessionDescriptionObserver> observer);
263 void DoSetLocalDescription(
264 std::unique_ptr<SessionDescriptionInterface> desc,
265 rtc::scoped_refptr<SetLocalDescriptionObserverInterface> observer);
266 void DoSetRemoteDescription(
Tomas Gunnarsson1c7c09b2022-01-12 13:11:04 +0100267 std::unique_ptr<RemoteDescriptionOperation> operation);
268
269 // Called after a DoSetRemoteDescription operation completes.
270 void SetRemoteDescriptionPostProcess(bool was_answer)
271 RTC_RUN_ON(signaling_thread());
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000272
273 // Update the state, signaling if necessary.
274 void ChangeSignalingState(
275 PeerConnectionInterface::SignalingState signaling_state);
276
Henrik Boströmf8187e02021-04-26 21:04:26 +0200277 RTCError UpdateSessionState(
278 SdpType type,
279 cricket::ContentSource source,
280 const cricket::SessionDescription* description,
281 const std::map<std::string, const cricket::ContentGroup*>&
282 bundle_groups_by_mid);
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000283
284 bool IsUnifiedPlan() const RTC_RUN_ON(signaling_thread());
285
Harald Alvestrand763f5a92020-10-22 10:39:40 +0000286 // Signals from MediaStreamObserver.
287 void OnAudioTrackAdded(AudioTrackInterface* track,
288 MediaStreamInterface* stream)
289 RTC_RUN_ON(signaling_thread());
290 void OnAudioTrackRemoved(AudioTrackInterface* track,
291 MediaStreamInterface* stream)
292 RTC_RUN_ON(signaling_thread());
293 void OnVideoTrackAdded(VideoTrackInterface* track,
294 MediaStreamInterface* stream)
295 RTC_RUN_ON(signaling_thread());
296 void OnVideoTrackRemoved(VideoTrackInterface* track,
297 MediaStreamInterface* stream)
298 RTC_RUN_ON(signaling_thread());
299
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000300 // | desc_type | is the type of the description that caused the rollback.
301 RTCError Rollback(SdpType desc_type);
302 void OnOperationsChainEmpty();
303
304 // Runs the algorithm **set the associated remote streams** specified in
305 // https://w3c.github.io/webrtc-pc/#set-associated-remote-streams.
306 void SetAssociatedRemoteStreams(
307 rtc::scoped_refptr<RtpReceiverInternal> receiver,
308 const std::vector<std::string>& stream_ids,
309 std::vector<rtc::scoped_refptr<MediaStreamInterface>>* added_streams,
310 std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams);
311
312 bool CheckIfNegotiationIsNeeded();
313 void GenerateNegotiationNeededEvent();
314 // Helper method which verifies SDP.
Henrik Boströmf8187e02021-04-26 21:04:26 +0200315 RTCError ValidateSessionDescription(
316 const SessionDescriptionInterface* sdesc,
317 cricket::ContentSource source,
318 const std::map<std::string, const cricket::ContentGroup*>&
319 bundle_groups_by_mid) RTC_RUN_ON(signaling_thread());
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000320
321 // Updates the local RtpTransceivers according to the JSEP rules. Called as
322 // part of setting the local/remote description.
323 RTCError UpdateTransceiversAndDataChannels(
324 cricket::ContentSource source,
325 const SessionDescriptionInterface& new_session,
326 const SessionDescriptionInterface* old_local_description,
Henrik Boströmf8187e02021-04-26 21:04:26 +0200327 const SessionDescriptionInterface* old_remote_description,
328 const std::map<std::string, const cricket::ContentGroup*>&
329 bundle_groups_by_mid);
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000330
331 // Associate the given transceiver according to the JSEP rules.
332 RTCErrorOr<
333 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>>
334 AssociateTransceiver(cricket::ContentSource source,
335 SdpType type,
336 size_t mline_index,
337 const cricket::ContentInfo& content,
338 const cricket::ContentInfo* old_local_content,
339 const cricket::ContentInfo* old_remote_content)
340 RTC_RUN_ON(signaling_thread());
341
Harald Alvestrand85466662021-04-19 21:21:36 +0000342 // Returns the media section in the given session description that is
343 // associated with the RtpTransceiver. Returns null if none found or this
344 // RtpTransceiver is not associated. Logic varies depending on the
345 // SdpSemantics specified in the configuration.
346 const cricket::ContentInfo* FindMediaSectionForTransceiver(
347 const RtpTransceiver* transceiver,
348 const SessionDescriptionInterface* sdesc) const;
349
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000350 // Either creates or destroys the transceiver's BaseChannel according to the
351 // given media section.
352 RTCError UpdateTransceiverChannel(
353 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
354 transceiver,
355 const cricket::ContentInfo& content,
356 const cricket::ContentGroup* bundle_group) RTC_RUN_ON(signaling_thread());
357
358 // Either creates or destroys the local data channel according to the given
359 // media section.
360 RTCError UpdateDataChannel(cricket::ContentSource source,
361 const cricket::ContentInfo& content,
362 const cricket::ContentGroup* bundle_group)
363 RTC_RUN_ON(signaling_thread());
Harald Alvestrandc06e3742020-10-01 10:23:33 +0000364 // Check if a call to SetLocalDescription is acceptable with a session
365 // description of the given type.
366 bool ExpectSetLocalDescription(SdpType type);
367 // Check if a call to SetRemoteDescription is acceptable with a session
368 // description of the given type.
369 bool ExpectSetRemoteDescription(SdpType type);
370
371 // The offer/answer machinery assumes the media section MID is present and
372 // unique. To support legacy end points that do not supply a=mid lines, this
373 // method will modify the session description to add MIDs generated according
374 // to the SDP semantics.
375 void FillInMissingRemoteMids(cricket::SessionDescription* remote_description);
376
Niels Möllerbe74b802022-03-18 14:10:15 +0100377 // Returns an RtpTransceiver, if available, that can be used to receive the
Harald Alvestrandc06e3742020-10-01 10:23:33 +0000378 // given media type according to JSEP rules.
379 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
380 FindAvailableTransceiverToReceive(cricket::MediaType media_type) const;
381
Artem Titov880fa812021-07-30 22:30:23 +0200382 // Returns a MediaSessionOptions struct with options decided by `options`,
Harald Alvestrandc06e3742020-10-01 10:23:33 +0000383 // the local MediaStreams and DataChannels.
384 void GetOptionsForOffer(const PeerConnectionInterface::RTCOfferAnswerOptions&
385 offer_answer_options,
386 cricket::MediaSessionOptions* session_options);
387 void GetOptionsForPlanBOffer(
388 const PeerConnectionInterface::RTCOfferAnswerOptions&
389 offer_answer_options,
390 cricket::MediaSessionOptions* session_options)
391 RTC_RUN_ON(signaling_thread());
392 void GetOptionsForUnifiedPlanOffer(
393 const PeerConnectionInterface::RTCOfferAnswerOptions&
394 offer_answer_options,
395 cricket::MediaSessionOptions* session_options)
396 RTC_RUN_ON(signaling_thread());
397
398 // Returns a MediaSessionOptions struct with options decided by
Artem Titov880fa812021-07-30 22:30:23 +0200399 // `constraints`, the local MediaStreams and DataChannels.
Harald Alvestrandc06e3742020-10-01 10:23:33 +0000400 void GetOptionsForAnswer(const PeerConnectionInterface::RTCOfferAnswerOptions&
401 offer_answer_options,
402 cricket::MediaSessionOptions* session_options);
403 void GetOptionsForPlanBAnswer(
404 const PeerConnectionInterface::RTCOfferAnswerOptions&
405 offer_answer_options,
406 cricket::MediaSessionOptions* session_options)
407 RTC_RUN_ON(signaling_thread());
408 void GetOptionsForUnifiedPlanAnswer(
409 const PeerConnectionInterface::RTCOfferAnswerOptions&
410 offer_answer_options,
411 cricket::MediaSessionOptions* session_options)
412 RTC_RUN_ON(signaling_thread());
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000413
Harald Alvestranda474fbf2020-10-01 16:47:23 +0000414 const char* SessionErrorToString(SessionError error) const;
415 std::string GetSessionErrorMsg();
416 // Returns the last error in the session. See the enum above for details.
417 SessionError session_error() const {
418 RTC_DCHECK_RUN_ON(signaling_thread());
419 return session_error_;
420 }
421 const std::string& session_error_desc() const { return session_error_desc_; }
422
423 RTCError HandleLegacyOfferOptions(
424 const PeerConnectionInterface::RTCOfferAnswerOptions& options);
425 void RemoveRecvDirectionFromReceivingTransceiversOfType(
426 cricket::MediaType media_type) RTC_RUN_ON(signaling_thread());
427 void AddUpToOneReceivingTransceiverOfType(cricket::MediaType media_type);
428
429 std::vector<
430 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>>
431 GetReceivingTransceiversOfType(cricket::MediaType media_type)
432 RTC_RUN_ON(signaling_thread());
433
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000434 // Runs the algorithm specified in
435 // https://w3c.github.io/webrtc-pc/#process-remote-track-removal
Harald Alvestranda474fbf2020-10-01 16:47:23 +0000436 // This method will update the following lists:
Artem Titov880fa812021-07-30 22:30:23 +0200437 // `remove_list` is the list of transceivers for which the receiving track is
Harald Alvestranda474fbf2020-10-01 16:47:23 +0000438 // being removed.
Artem Titov880fa812021-07-30 22:30:23 +0200439 // `removed_streams` is the list of streams which no longer have a receiving
Harald Alvestranda474fbf2020-10-01 16:47:23 +0000440 // track so should be removed.
Harald Alvestranda474fbf2020-10-01 16:47:23 +0000441 void ProcessRemovalOfRemoteTrack(
Harald Alvestrand85466662021-04-19 21:21:36 +0000442 const rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
Harald Alvestranda474fbf2020-10-01 16:47:23 +0000443 transceiver,
444 std::vector<rtc::scoped_refptr<RtpTransceiverInterface>>* remove_list,
445 std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams);
446
447 void RemoveRemoteStreamsIfEmpty(
448 const std::vector<rtc::scoped_refptr<MediaStreamInterface>>&
449 remote_streams,
450 std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams);
451
Artem Titov880fa812021-07-30 22:30:23 +0200452 // Remove all local and remote senders of type `media_type`.
Harald Alvestranda474fbf2020-10-01 16:47:23 +0000453 // Called when a media type is rejected (m-line set to port 0).
454 void RemoveSenders(cricket::MediaType media_type);
455
Artem Titov880fa812021-07-30 22:30:23 +0200456 // Loops through the vector of `streams` and finds added and removed
Harald Alvestranda474fbf2020-10-01 16:47:23 +0000457 // StreamParams since last time this method was called.
458 // For each new or removed StreamParam, OnLocalSenderSeen or
459 // OnLocalSenderRemoved is invoked.
460 void UpdateLocalSenders(const std::vector<cricket::StreamParams>& streams,
461 cricket::MediaType media_type);
462
Artem Titov880fa812021-07-30 22:30:23 +0200463 // Makes sure a MediaStreamTrack is created for each StreamParam in `streams`,
Harald Alvestranda474fbf2020-10-01 16:47:23 +0000464 // and existing MediaStreamTracks are removed if there is no corresponding
Artem Titov880fa812021-07-30 22:30:23 +0200465 // StreamParam. If `default_track_needed` is true, a default MediaStreamTrack
Harald Alvestranda474fbf2020-10-01 16:47:23 +0000466 // is created if it doesn't exist; if false, it's removed if it exists.
Artem Titov880fa812021-07-30 22:30:23 +0200467 // `media_type` is the type of the `streams` and can be either audio or video.
468 // If a new MediaStream is created it is added to `new_streams`.
Harald Alvestranda474fbf2020-10-01 16:47:23 +0000469 void UpdateRemoteSendersList(
470 const std::vector<cricket::StreamParams>& streams,
471 bool default_track_needed,
472 cricket::MediaType media_type,
473 StreamCollection* new_streams);
474
Taylor Brandstetterd0acbd82021-01-25 13:44:55 -0800475 // Enables media channels to allow sending of media.
Harald Alvestrand48171ec2021-04-20 15:06:03 +0000476 // This enables media to flow on all configured audio/video channels.
Taylor Brandstetterd0acbd82021-01-25 13:44:55 -0800477 void EnableSending();
Harald Alvestranda474fbf2020-10-01 16:47:23 +0000478 // Push the media parts of the local or remote session description
Harald Alvestrand8101e7b2022-05-23 14:57:47 +0000479 // down to all of the channels, and start SCTP if needed.
Henrik Boströmf8187e02021-04-26 21:04:26 +0200480 RTCError PushdownMediaDescription(
481 SdpType type,
482 cricket::ContentSource source,
483 const std::map<std::string, const cricket::ContentGroup*>&
484 bundle_groups_by_mid);
Harald Alvestranda474fbf2020-10-01 16:47:23 +0000485
486 RTCError PushdownTransportDescription(cricket::ContentSource source,
487 SdpType type);
488 // Helper function to remove stopped transceivers.
489 void RemoveStoppedTransceivers();
Artem Titov880fa812021-07-30 22:30:23 +0200490 // Deletes the corresponding channel of contents that don't exist in `desc`.
491 // `desc` can be null. This means that all channels are deleted.
Harald Alvestranda474fbf2020-10-01 16:47:23 +0000492 void RemoveUnusedChannels(const cricket::SessionDescription* desc);
493
494 // Report inferred negotiated SDP semantics from a local/remote answer to the
495 // UMA observer.
496 void ReportNegotiatedSdpSemantics(const SessionDescriptionInterface& answer);
497
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000498 // Finds remote MediaStreams without any tracks and removes them from
Artem Titov880fa812021-07-30 22:30:23 +0200499 // `remote_streams_` and notifies the observer that the MediaStreams no longer
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000500 // exist.
501 void UpdateEndedRemoteMediaStreams();
502
Tomas Gunnarsson0dd75392022-01-17 19:19:56 +0100503 // Uses all remote candidates in the currently set remote_description().
504 // If no remote description is currently set (nullptr), the return value will
505 // be true. If `UseCandidate()` fails for any candidate in the remote
506 // description, the return value will be false.
507 bool UseCandidatesInRemoteDescription();
Artem Titov880fa812021-07-30 22:30:23 +0200508 // Uses `candidate` in this session.
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000509 bool UseCandidate(const IceCandidateInterface* candidate);
510 // Returns true if we are ready to push down the remote candidate.
Artem Titov880fa812021-07-30 22:30:23 +0200511 // `remote_desc` is the new remote description, or NULL if the current remote
512 // description should be used. Output `valid` is true if the candidate media
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000513 // index is valid.
514 bool ReadyToUseRemoteCandidate(const IceCandidateInterface* candidate,
515 const SessionDescriptionInterface* remote_desc,
516 bool* valid);
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000517
518 RTCErrorOr<const cricket::ContentInfo*> FindContentInfo(
519 const SessionDescriptionInterface* description,
520 const IceCandidateInterface* candidate) RTC_RUN_ON(signaling_thread());
521
522 // Functions for dealing with transports.
523 // Note that cricket code uses the term "channel" for what other code
524 // refers to as "transport".
525
Artem Titov880fa812021-07-30 22:30:23 +0200526 // Allocates media channels based on the `desc`. If `desc` doesn't have
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000527 // the BUNDLE option, this method will disable BUNDLE in PortAllocator.
528 // This method will also delete any existing media channels before creating.
529 RTCError CreateChannels(const cricket::SessionDescription& desc);
530
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000531 bool CreateDataChannel(const std::string& mid);
532
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000533 // Destroys the RTP data channel transport and/or the SCTP data channel
534 // transport and clears it.
Florent Castellidcb9ffc2021-06-29 14:58:23 +0200535 void DestroyDataChannelTransport(RTCError error);
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000536
Artem Titov880fa812021-07-30 22:30:23 +0200537 // Generates MediaDescriptionOptions for the `session_opts` based on existing
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000538 // local description or remote description.
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000539 void GenerateMediaDescriptionOptions(
540 const SessionDescriptionInterface* session_desc,
541 RtpTransceiverDirection audio_direction,
542 RtpTransceiverDirection video_direction,
543 absl::optional<size_t>* audio_index,
544 absl::optional<size_t>* video_index,
545 absl::optional<size_t>* data_index,
546 cricket::MediaSessionOptions* session_options);
547
548 // Generates the active MediaDescriptionOptions for the local data channel
549 // given the specified MID.
550 cricket::MediaDescriptionOptions GetMediaDescriptionOptionsForActiveData(
551 const std::string& mid) const;
552
553 // Generates the rejected MediaDescriptionOptions for the local data channel
554 // given the specified MID.
555 cricket::MediaDescriptionOptions GetMediaDescriptionOptionsForRejectedData(
556 const std::string& mid) const;
557
Taylor Brandstetterd0acbd82021-01-25 13:44:55 -0800558 // Based on number of transceivers per media type, enabled or disable
559 // payload type based demuxing in the affected channels.
Henrik Boströmf8187e02021-04-26 21:04:26 +0200560 bool UpdatePayloadTypeDemuxingState(
561 cricket::ContentSource source,
562 const std::map<std::string, const cricket::ContentGroup*>&
563 bundle_groups_by_mid);
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000564
Harald Alvestrand42b983a2021-10-19 21:50:01 +0000565 // Updates the error state, signaling if necessary.
566 void SetSessionError(SessionError error, const std::string& error_desc);
567
568 // Implements AddIceCandidate without reporting usage, but returns the
569 // particular success/error value that should be reported (and can be utilized
570 // for other purposes).
571 AddIceCandidateResult AddIceCandidateInternal(
572 const IceCandidateInterface* candidate);
573
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000574 // ==================================================================
575 // Access to pc_ variables
576 cricket::ChannelManager* channel_manager() const;
Harald Alvestrand35f4b4c2022-05-16 10:36:43 +0000577 cricket::MediaEngineInterface* media_engine() const;
Harald Alvestrande15fb152020-10-19 13:28:05 +0000578 TransceiverList* transceivers();
579 const TransceiverList* transceivers() const;
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000580 DataChannelController* data_channel_controller();
581 const DataChannelController* data_channel_controller() const;
582 cricket::PortAllocator* port_allocator();
583 const cricket::PortAllocator* port_allocator() const;
Harald Alvestrande15fb152020-10-19 13:28:05 +0000584 RtpTransmissionManager* rtp_manager();
585 const RtpTransmissionManager* rtp_manager() const;
Harald Alvestrandbc32c562022-02-09 12:08:47 +0000586 JsepTransportController* transport_controller_s()
587 RTC_RUN_ON(signaling_thread());
588 const JsepTransportController* transport_controller_s() const
589 RTC_RUN_ON(signaling_thread());
590 JsepTransportController* transport_controller_n()
591 RTC_RUN_ON(network_thread());
592 const JsepTransportController* transport_controller_n() const
593 RTC_RUN_ON(network_thread());
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 Alvestrand8101e7b2022-05-23 14:57:47 +0000597 bool ConfiguredForMedia() const;
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000598
Harald Alvestrand5b661302022-01-28 13:08:34 +0000599 PeerConnectionSdpMethods* const pc_;
Harald Alvestrand66c40362022-01-28 17:41:30 +0000600 ConnectionContext* const context_;
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000601
602 std::unique_ptr<WebRtcSessionDescriptionFactory> webrtc_session_desc_factory_
603 RTC_GUARDED_BY(signaling_thread());
604
605 std::unique_ptr<SessionDescriptionInterface> current_local_description_
606 RTC_GUARDED_BY(signaling_thread());
607 std::unique_ptr<SessionDescriptionInterface> pending_local_description_
608 RTC_GUARDED_BY(signaling_thread());
609 std::unique_ptr<SessionDescriptionInterface> current_remote_description_
610 RTC_GUARDED_BY(signaling_thread());
611 std::unique_ptr<SessionDescriptionInterface> pending_remote_description_
612 RTC_GUARDED_BY(signaling_thread());
613
614 PeerConnectionInterface::SignalingState signaling_state_
615 RTC_GUARDED_BY(signaling_thread()) = PeerConnectionInterface::kStable;
616
617 // Whether this peer is the caller. Set when the local description is applied.
618 absl::optional<bool> is_caller_ RTC_GUARDED_BY(signaling_thread());
619
Harald Alvestrand6f04b652020-10-09 11:42:17 +0000620 // Streams added via AddStream.
621 const rtc::scoped_refptr<StreamCollection> local_streams_
622 RTC_GUARDED_BY(signaling_thread());
623 // Streams created as a result of SetRemoteDescription.
624 const rtc::scoped_refptr<StreamCollection> remote_streams_
625 RTC_GUARDED_BY(signaling_thread());
626
627 std::vector<std::unique_ptr<MediaStreamObserver>> stream_observers_
628 RTC_GUARDED_BY(signaling_thread());
629
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000630 // The operations chain is used by the offer/answer exchange methods to ensure
631 // they are executed in the right order. For example, if
632 // SetRemoteDescription() is invoked while CreateOffer() is still pending, the
633 // SRD operation will not start until CreateOffer() has completed. See
634 // https://w3c.github.io/webrtc-pc/#dfn-operations-chain.
635 rtc::scoped_refptr<rtc::OperationsChain> operations_chain_
636 RTC_GUARDED_BY(signaling_thread());
637
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000638 // One PeerConnection has only one RTCP CNAME.
639 // https://tools.ietf.org/html/draft-ietf-rtcweb-rtp-usage-26#section-4.9
640 const std::string rtcp_cname_;
641
642 // MIDs will be generated using this generator which will keep track of
643 // all the MIDs that have been seen over the life of the PeerConnection.
644 rtc::UniqueStringGenerator mid_generator_ RTC_GUARDED_BY(signaling_thread());
645
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000646 // List of content names for which the remote side triggered an ICE restart.
647 std::set<std::string> pending_ice_restarts_
648 RTC_GUARDED_BY(signaling_thread());
649
650 std::unique_ptr<LocalIceCredentialsToReplace>
651 local_ice_credentials_to_replace_ RTC_GUARDED_BY(signaling_thread());
652
653 bool remote_peer_supports_msid_ RTC_GUARDED_BY(signaling_thread()) = false;
654 bool is_negotiation_needed_ RTC_GUARDED_BY(signaling_thread()) = false;
Harald Alvestrand42b983a2021-10-19 21:50:01 +0000655 uint32_t negotiation_needed_event_id_ RTC_GUARDED_BY(signaling_thread()) = 0;
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000656 bool update_negotiation_needed_on_empty_chain_
657 RTC_GUARDED_BY(signaling_thread()) = false;
Henrik Boström4ea80f32021-06-09 10:29:50 +0200658 // If PT demuxing is successfully negotiated one time we will allow PT
659 // demuxing for the rest of the session so that PT-based apps default to PT
660 // demuxing in follow-up O/A exchanges.
Harald Alvestrand42b983a2021-10-19 21:50:01 +0000661 bool pt_demuxing_has_been_used_audio_ RTC_GUARDED_BY(signaling_thread()) =
662 false;
663 bool pt_demuxing_has_been_used_video_ RTC_GUARDED_BY(signaling_thread()) =
664 false;
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000665
666 // In Unified Plan, if we encounter remote SDP that does not contain an a=msid
667 // line we create and use a stream with a random ID for our receivers. This is
668 // to support legacy endpoints that do not support the a=msid attribute (as
669 // opposed to streamless tracks with "a=msid:-").
670 rtc::scoped_refptr<MediaStreamInterface> missing_msid_default_stream_
671 RTC_GUARDED_BY(signaling_thread());
672
Harald Alvestranda474fbf2020-10-01 16:47:23 +0000673 SessionError session_error_ RTC_GUARDED_BY(signaling_thread()) =
674 SessionError::kNone;
675 std::string session_error_desc_ RTC_GUARDED_BY(signaling_thread());
676
Harald Alvestrand763f5a92020-10-22 10:39:40 +0000677 // Member variables for caching global options.
678 cricket::AudioOptions audio_options_ RTC_GUARDED_BY(signaling_thread());
679 cricket::VideoOptions video_options_ RTC_GUARDED_BY(signaling_thread());
680
Harald Alvestrand763f5a92020-10-22 10:39:40 +0000681 // A video bitrate allocator factory.
682 // This can be injected using the PeerConnectionDependencies,
683 // or else the CreateBuiltinVideoBitrateAllocatorFactory() will be called.
684 // Note that one can still choose to override this in a MediaEngine
685 // if one wants too.
686 std::unique_ptr<webrtc::VideoBitrateAllocatorFactory>
Harald Alvestrand42b983a2021-10-19 21:50:01 +0000687 video_bitrate_allocator_factory_ RTC_GUARDED_BY(signaling_thread());
Harald Alvestrand763f5a92020-10-22 10:39:40 +0000688
Harald Alvestrandbc32c562022-02-09 12:08:47 +0000689 // Whether we are the initial offerer on the association. This
690 // determines the SSL role.
691 absl::optional<bool> initial_offerer_ RTC_GUARDED_BY(signaling_thread());
692
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000693 rtc::WeakPtrFactory<SdpOfferAnswerHandler> weak_ptr_factory_
694 RTC_GUARDED_BY(signaling_thread());
695};
696
697} // namespace webrtc
698
699#endif // PC_SDP_OFFER_ANSWER_H_