blob: 67ead4724241b6ffb2c1bae2b1e2ece8553cca3b [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 Alvestrand8e344192022-02-08 12:55:42 +000050#include "pc/connection_context.h"
Harald Alvestrandcdcfab02020-09-28 13:02:07 +000051#include "pc/data_channel_controller.h"
52#include "pc/ice_server_parsing.h"
53#include "pc/jsep_transport_controller.h"
Harald Alvestrand1f7eab62020-10-18 16:51:47 +000054#include "pc/media_session.h"
55#include "pc/media_stream_observer.h"
Harald Alvestrandcdcfab02020-09-28 13:02:07 +000056#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(
Harald Alvestrand5b661302022-01-28 13:08:34 +000097 PeerConnectionSdpMethods* pc,
Harald Alvestrand763f5a92020-10-22 10:39:40 +000098 const PeerConnectionInterface::RTCConfiguration& configuration,
Harald Alvestrand66c40362022-01-28 17:41:30 +000099 PeerConnectionDependencies& dependencies,
100 ConnectionContext* context);
Harald Alvestrand763f5a92020-10-22 10:39:40 +0000101
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000102 void ResetSessionDescFactory() {
103 RTC_DCHECK_RUN_ON(signaling_thread());
104 webrtc_session_desc_factory_.reset();
105 }
106 const WebRtcSessionDescriptionFactory* webrtc_session_desc_factory() const {
107 RTC_DCHECK_RUN_ON(signaling_thread());
108 return webrtc_session_desc_factory_.get();
109 }
110
111 // Change signaling state to Closed, and perform appropriate actions.
112 void Close();
113
114 // Called as part of destroying the owning PeerConnection.
115 void PrepareForShutdown();
116
Harald Alvestrandf01bd6c2020-10-23 13:30:46 +0000117 // Implementation of SdpStateProvider
118 PeerConnectionInterface::SignalingState signaling_state() const override;
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000119
Harald Alvestrandf01bd6c2020-10-23 13:30:46 +0000120 const SessionDescriptionInterface* local_description() const override;
121 const SessionDescriptionInterface* remote_description() const override;
122 const SessionDescriptionInterface* current_local_description() const override;
123 const SessionDescriptionInterface* current_remote_description()
124 const override;
125 const SessionDescriptionInterface* pending_local_description() const override;
126 const SessionDescriptionInterface* pending_remote_description()
127 const override;
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000128
Harald Alvestrandf01bd6c2020-10-23 13:30:46 +0000129 bool NeedsIceRestart(const std::string& content_name) const override;
130 bool IceRestartPending(const std::string& content_name) const override;
131 absl::optional<rtc::SSLRole> GetDtlsRole(
132 const std::string& mid) const override;
Harald Alvestrandd89ce532020-10-21 08:59:22 +0000133
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000134 void RestartIce();
135
136 // JSEP01
137 void CreateOffer(
138 CreateSessionDescriptionObserver* observer,
139 const PeerConnectionInterface::RTCOfferAnswerOptions& options);
140 void CreateAnswer(
141 CreateSessionDescriptionObserver* observer,
142 const PeerConnectionInterface::RTCOfferAnswerOptions& options);
143
144 void SetLocalDescription(
145 std::unique_ptr<SessionDescriptionInterface> desc,
146 rtc::scoped_refptr<SetLocalDescriptionObserverInterface> observer);
147 void SetLocalDescription(
148 rtc::scoped_refptr<SetLocalDescriptionObserverInterface> observer);
149 void SetLocalDescription(SetSessionDescriptionObserver* observer,
150 SessionDescriptionInterface* desc);
151 void SetLocalDescription(SetSessionDescriptionObserver* observer);
152
153 void SetRemoteDescription(
154 std::unique_ptr<SessionDescriptionInterface> desc,
155 rtc::scoped_refptr<SetRemoteDescriptionObserverInterface> observer);
156 void SetRemoteDescription(SetSessionDescriptionObserver* observer,
157 SessionDescriptionInterface* desc);
158
159 PeerConnectionInterface::RTCConfiguration GetConfiguration();
160 RTCError SetConfiguration(
161 const PeerConnectionInterface::RTCConfiguration& configuration);
162 bool AddIceCandidate(const IceCandidateInterface* candidate);
163 void AddIceCandidate(std::unique_ptr<IceCandidateInterface> candidate,
164 std::function<void(RTCError)> callback);
165 bool RemoveIceCandidates(const std::vector<cricket::Candidate>& candidates);
166 // Adds a locally generated candidate to the local description.
167 void AddLocalIceCandidate(const JsepIceCandidate* candidate);
168 void RemoveLocalIceCandidates(
169 const std::vector<cricket::Candidate>& candidates);
170 bool ShouldFireNegotiationNeededEvent(uint32_t event_id);
171
Harald Alvestrand6f04b652020-10-09 11:42:17 +0000172 bool AddStream(MediaStreamInterface* local_stream);
173 void RemoveStream(MediaStreamInterface* local_stream);
174
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000175 absl::optional<bool> is_caller();
176 bool HasNewIceCredentials();
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000177 void UpdateNegotiationNeeded();
178
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000179 // Destroys all BaseChannels and destroys the SCTP data channel, if present.
180 void DestroyAllChannels();
181
Harald Alvestrand6f04b652020-10-09 11:42:17 +0000182 rtc::scoped_refptr<StreamCollectionInterface> local_streams();
183 rtc::scoped_refptr<StreamCollectionInterface> remote_streams();
184
Harald Alvestrandbc32c562022-02-09 12:08:47 +0000185 bool initial_offerer() {
186 RTC_DCHECK_RUN_ON(signaling_thread());
187 if (initial_offerer_) {
188 return *initial_offerer_;
189 }
190 return false;
191 }
192
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000193 private:
Tomas Gunnarsson1c7c09b2022-01-12 13:11:04 +0100194 class RemoteDescriptionOperation;
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000195 class ImplicitCreateSessionDescriptionObserver;
Harald Alvestrand1f7eab62020-10-18 16:51:47 +0000196
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000197 friend class ImplicitCreateSessionDescriptionObserver;
198 class SetSessionDescriptionObserverAdapter;
Harald Alvestrand1f7eab62020-10-18 16:51:47 +0000199
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000200 friend class SetSessionDescriptionObserverAdapter;
201
Harald Alvestranda474fbf2020-10-01 16:47:23 +0000202 enum class SessionError {
203 kNone, // No error.
204 kContent, // Error in BaseChannel SetLocalContent/SetRemoteContent.
205 kTransport, // Error from the underlying transport.
206 };
207
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000208 // Represents the [[LocalIceCredentialsToReplace]] internal slot in the spec.
209 // It makes the next CreateOffer() produce new ICE credentials even if
210 // RTCOfferAnswerOptions::ice_restart is false.
211 // https://w3c.github.io/webrtc-pc/#dfn-localufragstoreplace
212 // TODO(hbos): When JsepTransportController/JsepTransport supports rollback,
213 // move this type of logic to JsepTransportController/JsepTransport.
214 class LocalIceCredentialsToReplace;
215
Harald Alvestrand9cd199d2020-10-27 07:10:43 +0000216 // Only called by the Create() function.
Harald Alvestrand66c40362022-01-28 17:41:30 +0000217 explicit SdpOfferAnswerHandler(PeerConnectionSdpMethods* pc,
218 ConnectionContext* context);
Harald Alvestrand9cd199d2020-10-27 07:10:43 +0000219 // Called from the `Create()` function. Can only be called
220 // once. Modifies dependencies.
221 void Initialize(
222 const PeerConnectionInterface::RTCConfiguration& configuration,
223 PeerConnectionDependencies& dependencies);
224
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000225 rtc::Thread* signaling_thread() const;
Harald Alvestrandbc32c562022-02-09 12:08:47 +0000226 rtc::Thread* network_thread() const;
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000227 // Non-const versions of local_description()/remote_description(), for use
228 // internally.
229 SessionDescriptionInterface* mutable_local_description()
230 RTC_RUN_ON(signaling_thread()) {
231 return pending_local_description_ ? pending_local_description_.get()
232 : current_local_description_.get();
233 }
234 SessionDescriptionInterface* mutable_remote_description()
235 RTC_RUN_ON(signaling_thread()) {
236 return pending_remote_description_ ? pending_remote_description_.get()
237 : current_remote_description_.get();
238 }
239
240 // Synchronous implementations of SetLocalDescription/SetRemoteDescription
241 // that return an RTCError instead of invoking a callback.
242 RTCError ApplyLocalDescription(
Henrik Boströmf8187e02021-04-26 21:04:26 +0200243 std::unique_ptr<SessionDescriptionInterface> desc,
244 const std::map<std::string, const cricket::ContentGroup*>&
245 bundle_groups_by_mid);
Tomas Gunnarsson0dd75392022-01-17 19:19:56 +0100246 void ApplyRemoteDescription(
247 std::unique_ptr<RemoteDescriptionOperation> operation);
248
249 RTCError ReplaceRemoteDescription(
Henrik Boströmf8187e02021-04-26 21:04:26 +0200250 std::unique_ptr<SessionDescriptionInterface> desc,
Tomas Gunnarsson0dd75392022-01-17 19:19:56 +0100251 SdpType sdp_type,
252 std::unique_ptr<SessionDescriptionInterface>* replaced_description)
253 RTC_RUN_ON(signaling_thread());
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000254
Tomas Gunnarsson651586c2022-01-07 18:33:12 +0000255 // Part of ApplyRemoteDescription steps specific to Unified Plan.
256 void ApplyRemoteDescriptionUpdateTransceiverState(SdpType sdp_type);
257
Tomas Gunnarssonb625edf2022-01-07 16:22:14 +0000258 // Part of ApplyRemoteDescription steps specific to plan b.
259 void PlanBUpdateSendersAndReceivers(
260 const cricket::ContentInfo* audio_content,
261 const cricket::AudioContentDescription* audio_desc,
262 const cricket::ContentInfo* video_content,
263 const cricket::VideoContentDescription* video_desc);
264
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000265 // Implementation of the offer/answer exchange operations. These are chained
Artem Titov880fa812021-07-30 22:30:23 +0200266 // onto the `operations_chain_` when the public CreateOffer(), CreateAnswer(),
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000267 // SetLocalDescription() and SetRemoteDescription() methods are invoked.
268 void DoCreateOffer(
269 const PeerConnectionInterface::RTCOfferAnswerOptions& options,
270 rtc::scoped_refptr<CreateSessionDescriptionObserver> observer);
271 void DoCreateAnswer(
272 const PeerConnectionInterface::RTCOfferAnswerOptions& options,
273 rtc::scoped_refptr<CreateSessionDescriptionObserver> observer);
274 void DoSetLocalDescription(
275 std::unique_ptr<SessionDescriptionInterface> desc,
276 rtc::scoped_refptr<SetLocalDescriptionObserverInterface> observer);
277 void DoSetRemoteDescription(
Tomas Gunnarsson1c7c09b2022-01-12 13:11:04 +0100278 std::unique_ptr<RemoteDescriptionOperation> operation);
279
280 // Called after a DoSetRemoteDescription operation completes.
281 void SetRemoteDescriptionPostProcess(bool was_answer)
282 RTC_RUN_ON(signaling_thread());
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000283
284 // Update the state, signaling if necessary.
285 void ChangeSignalingState(
286 PeerConnectionInterface::SignalingState signaling_state);
287
Henrik Boströmf8187e02021-04-26 21:04:26 +0200288 RTCError UpdateSessionState(
289 SdpType type,
290 cricket::ContentSource source,
291 const cricket::SessionDescription* description,
292 const std::map<std::string, const cricket::ContentGroup*>&
293 bundle_groups_by_mid);
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000294
295 bool IsUnifiedPlan() const RTC_RUN_ON(signaling_thread());
296
Harald Alvestrand763f5a92020-10-22 10:39:40 +0000297 // Signals from MediaStreamObserver.
298 void OnAudioTrackAdded(AudioTrackInterface* track,
299 MediaStreamInterface* stream)
300 RTC_RUN_ON(signaling_thread());
301 void OnAudioTrackRemoved(AudioTrackInterface* track,
302 MediaStreamInterface* stream)
303 RTC_RUN_ON(signaling_thread());
304 void OnVideoTrackAdded(VideoTrackInterface* track,
305 MediaStreamInterface* stream)
306 RTC_RUN_ON(signaling_thread());
307 void OnVideoTrackRemoved(VideoTrackInterface* track,
308 MediaStreamInterface* stream)
309 RTC_RUN_ON(signaling_thread());
310
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000311 // | desc_type | is the type of the description that caused the rollback.
312 RTCError Rollback(SdpType desc_type);
313 void OnOperationsChainEmpty();
314
315 // Runs the algorithm **set the associated remote streams** specified in
316 // https://w3c.github.io/webrtc-pc/#set-associated-remote-streams.
317 void SetAssociatedRemoteStreams(
318 rtc::scoped_refptr<RtpReceiverInternal> receiver,
319 const std::vector<std::string>& stream_ids,
320 std::vector<rtc::scoped_refptr<MediaStreamInterface>>* added_streams,
321 std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams);
322
323 bool CheckIfNegotiationIsNeeded();
324 void GenerateNegotiationNeededEvent();
325 // Helper method which verifies SDP.
Henrik Boströmf8187e02021-04-26 21:04:26 +0200326 RTCError ValidateSessionDescription(
327 const SessionDescriptionInterface* sdesc,
328 cricket::ContentSource source,
329 const std::map<std::string, const cricket::ContentGroup*>&
330 bundle_groups_by_mid) RTC_RUN_ON(signaling_thread());
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000331
332 // Updates the local RtpTransceivers according to the JSEP rules. Called as
333 // part of setting the local/remote description.
334 RTCError UpdateTransceiversAndDataChannels(
335 cricket::ContentSource source,
336 const SessionDescriptionInterface& new_session,
337 const SessionDescriptionInterface* old_local_description,
Henrik Boströmf8187e02021-04-26 21:04:26 +0200338 const SessionDescriptionInterface* old_remote_description,
339 const std::map<std::string, const cricket::ContentGroup*>&
340 bundle_groups_by_mid);
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000341
342 // Associate the given transceiver according to the JSEP rules.
343 RTCErrorOr<
344 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>>
345 AssociateTransceiver(cricket::ContentSource source,
346 SdpType type,
347 size_t mline_index,
348 const cricket::ContentInfo& content,
349 const cricket::ContentInfo* old_local_content,
350 const cricket::ContentInfo* old_remote_content)
351 RTC_RUN_ON(signaling_thread());
352
Harald Alvestrand85466662021-04-19 21:21:36 +0000353 // Returns the media section in the given session description that is
354 // associated with the RtpTransceiver. Returns null if none found or this
355 // RtpTransceiver is not associated. Logic varies depending on the
356 // SdpSemantics specified in the configuration.
357 const cricket::ContentInfo* FindMediaSectionForTransceiver(
358 const RtpTransceiver* transceiver,
359 const SessionDescriptionInterface* sdesc) const;
360
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000361 // Either creates or destroys the transceiver's BaseChannel according to the
362 // given media section.
363 RTCError UpdateTransceiverChannel(
364 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
365 transceiver,
366 const cricket::ContentInfo& content,
367 const cricket::ContentGroup* bundle_group) RTC_RUN_ON(signaling_thread());
368
369 // Either creates or destroys the local data channel according to the given
370 // media section.
371 RTCError UpdateDataChannel(cricket::ContentSource source,
372 const cricket::ContentInfo& content,
373 const cricket::ContentGroup* bundle_group)
374 RTC_RUN_ON(signaling_thread());
Harald Alvestrandc06e3742020-10-01 10:23:33 +0000375 // Check if a call to SetLocalDescription is acceptable with a session
376 // description of the given type.
377 bool ExpectSetLocalDescription(SdpType type);
378 // Check if a call to SetRemoteDescription is acceptable with a session
379 // description of the given type.
380 bool ExpectSetRemoteDescription(SdpType type);
381
382 // The offer/answer machinery assumes the media section MID is present and
383 // unique. To support legacy end points that do not supply a=mid lines, this
384 // method will modify the session description to add MIDs generated according
385 // to the SDP semantics.
386 void FillInMissingRemoteMids(cricket::SessionDescription* remote_description);
387
388 // Returns an RtpTransciever, if available, that can be used to receive the
389 // given media type according to JSEP rules.
390 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
391 FindAvailableTransceiverToReceive(cricket::MediaType media_type) const;
392
Artem Titov880fa812021-07-30 22:30:23 +0200393 // Returns a MediaSessionOptions struct with options decided by `options`,
Harald Alvestrandc06e3742020-10-01 10:23:33 +0000394 // the local MediaStreams and DataChannels.
395 void GetOptionsForOffer(const PeerConnectionInterface::RTCOfferAnswerOptions&
396 offer_answer_options,
397 cricket::MediaSessionOptions* session_options);
398 void GetOptionsForPlanBOffer(
399 const PeerConnectionInterface::RTCOfferAnswerOptions&
400 offer_answer_options,
401 cricket::MediaSessionOptions* session_options)
402 RTC_RUN_ON(signaling_thread());
403 void GetOptionsForUnifiedPlanOffer(
404 const PeerConnectionInterface::RTCOfferAnswerOptions&
405 offer_answer_options,
406 cricket::MediaSessionOptions* session_options)
407 RTC_RUN_ON(signaling_thread());
408
409 // Returns a MediaSessionOptions struct with options decided by
Artem Titov880fa812021-07-30 22:30:23 +0200410 // `constraints`, the local MediaStreams and DataChannels.
Harald Alvestrandc06e3742020-10-01 10:23:33 +0000411 void GetOptionsForAnswer(const PeerConnectionInterface::RTCOfferAnswerOptions&
412 offer_answer_options,
413 cricket::MediaSessionOptions* session_options);
414 void GetOptionsForPlanBAnswer(
415 const PeerConnectionInterface::RTCOfferAnswerOptions&
416 offer_answer_options,
417 cricket::MediaSessionOptions* session_options)
418 RTC_RUN_ON(signaling_thread());
419 void GetOptionsForUnifiedPlanAnswer(
420 const PeerConnectionInterface::RTCOfferAnswerOptions&
421 offer_answer_options,
422 cricket::MediaSessionOptions* session_options)
423 RTC_RUN_ON(signaling_thread());
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000424
Harald Alvestranda474fbf2020-10-01 16:47:23 +0000425 const char* SessionErrorToString(SessionError error) const;
426 std::string GetSessionErrorMsg();
427 // Returns the last error in the session. See the enum above for details.
428 SessionError session_error() const {
429 RTC_DCHECK_RUN_ON(signaling_thread());
430 return session_error_;
431 }
432 const std::string& session_error_desc() const { return session_error_desc_; }
433
434 RTCError HandleLegacyOfferOptions(
435 const PeerConnectionInterface::RTCOfferAnswerOptions& options);
436 void RemoveRecvDirectionFromReceivingTransceiversOfType(
437 cricket::MediaType media_type) RTC_RUN_ON(signaling_thread());
438 void AddUpToOneReceivingTransceiverOfType(cricket::MediaType media_type);
439
440 std::vector<
441 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>>
442 GetReceivingTransceiversOfType(cricket::MediaType media_type)
443 RTC_RUN_ON(signaling_thread());
444
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000445 // Runs the algorithm specified in
446 // https://w3c.github.io/webrtc-pc/#process-remote-track-removal
Harald Alvestranda474fbf2020-10-01 16:47:23 +0000447 // This method will update the following lists:
Artem Titov880fa812021-07-30 22:30:23 +0200448 // `remove_list` is the list of transceivers for which the receiving track is
Harald Alvestranda474fbf2020-10-01 16:47:23 +0000449 // being removed.
Artem Titov880fa812021-07-30 22:30:23 +0200450 // `removed_streams` is the list of streams which no longer have a receiving
Harald Alvestranda474fbf2020-10-01 16:47:23 +0000451 // track so should be removed.
Harald Alvestranda474fbf2020-10-01 16:47:23 +0000452 void ProcessRemovalOfRemoteTrack(
Harald Alvestrand85466662021-04-19 21:21:36 +0000453 const rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
Harald Alvestranda474fbf2020-10-01 16:47:23 +0000454 transceiver,
455 std::vector<rtc::scoped_refptr<RtpTransceiverInterface>>* remove_list,
456 std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams);
457
458 void RemoveRemoteStreamsIfEmpty(
459 const std::vector<rtc::scoped_refptr<MediaStreamInterface>>&
460 remote_streams,
461 std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams);
462
Artem Titov880fa812021-07-30 22:30:23 +0200463 // Remove all local and remote senders of type `media_type`.
Harald Alvestranda474fbf2020-10-01 16:47:23 +0000464 // Called when a media type is rejected (m-line set to port 0).
465 void RemoveSenders(cricket::MediaType media_type);
466
Artem Titov880fa812021-07-30 22:30:23 +0200467 // Loops through the vector of `streams` and finds added and removed
Harald Alvestranda474fbf2020-10-01 16:47:23 +0000468 // StreamParams since last time this method was called.
469 // For each new or removed StreamParam, OnLocalSenderSeen or
470 // OnLocalSenderRemoved is invoked.
471 void UpdateLocalSenders(const std::vector<cricket::StreamParams>& streams,
472 cricket::MediaType media_type);
473
Artem Titov880fa812021-07-30 22:30:23 +0200474 // Makes sure a MediaStreamTrack is created for each StreamParam in `streams`,
Harald Alvestranda474fbf2020-10-01 16:47:23 +0000475 // and existing MediaStreamTracks are removed if there is no corresponding
Artem Titov880fa812021-07-30 22:30:23 +0200476 // StreamParam. If `default_track_needed` is true, a default MediaStreamTrack
Harald Alvestranda474fbf2020-10-01 16:47:23 +0000477 // is created if it doesn't exist; if false, it's removed if it exists.
Artem Titov880fa812021-07-30 22:30:23 +0200478 // `media_type` is the type of the `streams` and can be either audio or video.
479 // If a new MediaStream is created it is added to `new_streams`.
Harald Alvestranda474fbf2020-10-01 16:47:23 +0000480 void UpdateRemoteSendersList(
481 const std::vector<cricket::StreamParams>& streams,
482 bool default_track_needed,
483 cricket::MediaType media_type,
484 StreamCollection* new_streams);
485
Taylor Brandstetterd0acbd82021-01-25 13:44:55 -0800486 // Enables media channels to allow sending of media.
Harald Alvestrand48171ec2021-04-20 15:06:03 +0000487 // This enables media to flow on all configured audio/video channels.
Taylor Brandstetterd0acbd82021-01-25 13:44:55 -0800488 void EnableSending();
Harald Alvestranda474fbf2020-10-01 16:47:23 +0000489 // Push the media parts of the local or remote session description
Taylor Brandstetterd0acbd82021-01-25 13:44:55 -0800490 // down to all of the channels.
Henrik Boströmf8187e02021-04-26 21:04:26 +0200491 RTCError PushdownMediaDescription(
492 SdpType type,
493 cricket::ContentSource source,
494 const std::map<std::string, const cricket::ContentGroup*>&
495 bundle_groups_by_mid);
Harald Alvestranda474fbf2020-10-01 16:47:23 +0000496
497 RTCError PushdownTransportDescription(cricket::ContentSource source,
498 SdpType type);
499 // Helper function to remove stopped transceivers.
500 void RemoveStoppedTransceivers();
Artem Titov880fa812021-07-30 22:30:23 +0200501 // Deletes the corresponding channel of contents that don't exist in `desc`.
502 // `desc` can be null. This means that all channels are deleted.
Harald Alvestranda474fbf2020-10-01 16:47:23 +0000503 void RemoveUnusedChannels(const cricket::SessionDescription* desc);
504
505 // Report inferred negotiated SDP semantics from a local/remote answer to the
506 // UMA observer.
507 void ReportNegotiatedSdpSemantics(const SessionDescriptionInterface& answer);
508
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000509 // Finds remote MediaStreams without any tracks and removes them from
Artem Titov880fa812021-07-30 22:30:23 +0200510 // `remote_streams_` and notifies the observer that the MediaStreams no longer
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000511 // exist.
512 void UpdateEndedRemoteMediaStreams();
513
Tomas Gunnarsson0dd75392022-01-17 19:19:56 +0100514 // Uses all remote candidates in the currently set remote_description().
515 // If no remote description is currently set (nullptr), the return value will
516 // be true. If `UseCandidate()` fails for any candidate in the remote
517 // description, the return value will be false.
518 bool UseCandidatesInRemoteDescription();
Artem Titov880fa812021-07-30 22:30:23 +0200519 // Uses `candidate` in this session.
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000520 bool UseCandidate(const IceCandidateInterface* candidate);
521 // Returns true if we are ready to push down the remote candidate.
Artem Titov880fa812021-07-30 22:30:23 +0200522 // `remote_desc` is the new remote description, or NULL if the current remote
523 // description should be used. Output `valid` is true if the candidate media
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000524 // index is valid.
525 bool ReadyToUseRemoteCandidate(const IceCandidateInterface* candidate,
526 const SessionDescriptionInterface* remote_desc,
527 bool* valid);
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000528
529 RTCErrorOr<const cricket::ContentInfo*> FindContentInfo(
530 const SessionDescriptionInterface* description,
531 const IceCandidateInterface* candidate) RTC_RUN_ON(signaling_thread());
532
533 // Functions for dealing with transports.
534 // Note that cricket code uses the term "channel" for what other code
535 // refers to as "transport".
536
Artem Titov880fa812021-07-30 22:30:23 +0200537 // Allocates media channels based on the `desc`. If `desc` doesn't have
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000538 // the BUNDLE option, this method will disable BUNDLE in PortAllocator.
539 // This method will also delete any existing media channels before creating.
540 RTCError CreateChannels(const cricket::SessionDescription& desc);
541
542 // Helper methods to create media channels.
543 cricket::VoiceChannel* CreateVoiceChannel(const std::string& mid);
544 cricket::VideoChannel* CreateVideoChannel(const std::string& mid);
545 bool CreateDataChannel(const std::string& mid);
546
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000547 // Destroys the RTP data channel transport and/or the SCTP data channel
548 // transport and clears it.
Florent Castellidcb9ffc2021-06-29 14:58:23 +0200549 void DestroyDataChannelTransport(RTCError error);
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000550
Artem Titov880fa812021-07-30 22:30:23 +0200551 // Generates MediaDescriptionOptions for the `session_opts` based on existing
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000552 // local description or remote description.
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000553 void GenerateMediaDescriptionOptions(
554 const SessionDescriptionInterface* session_desc,
555 RtpTransceiverDirection audio_direction,
556 RtpTransceiverDirection video_direction,
557 absl::optional<size_t>* audio_index,
558 absl::optional<size_t>* video_index,
559 absl::optional<size_t>* data_index,
560 cricket::MediaSessionOptions* session_options);
561
562 // Generates the active MediaDescriptionOptions for the local data channel
563 // given the specified MID.
564 cricket::MediaDescriptionOptions GetMediaDescriptionOptionsForActiveData(
565 const std::string& mid) const;
566
567 // Generates the rejected MediaDescriptionOptions for the local data channel
568 // given the specified MID.
569 cricket::MediaDescriptionOptions GetMediaDescriptionOptionsForRejectedData(
570 const std::string& mid) const;
571
Taylor Brandstetterd0acbd82021-01-25 13:44:55 -0800572 // Based on number of transceivers per media type, enabled or disable
573 // payload type based demuxing in the affected channels.
Henrik Boströmf8187e02021-04-26 21:04:26 +0200574 bool UpdatePayloadTypeDemuxingState(
575 cricket::ContentSource source,
576 const std::map<std::string, const cricket::ContentGroup*>&
577 bundle_groups_by_mid);
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000578
Harald Alvestrand42b983a2021-10-19 21:50:01 +0000579 // Updates the error state, signaling if necessary.
580 void SetSessionError(SessionError error, const std::string& error_desc);
581
582 // Implements AddIceCandidate without reporting usage, but returns the
583 // particular success/error value that should be reported (and can be utilized
584 // for other purposes).
585 AddIceCandidateResult AddIceCandidateInternal(
586 const IceCandidateInterface* candidate);
587
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000588 // ==================================================================
589 // Access to pc_ variables
590 cricket::ChannelManager* channel_manager() const;
Harald Alvestrande15fb152020-10-19 13:28:05 +0000591 TransceiverList* transceivers();
592 const TransceiverList* transceivers() const;
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000593 DataChannelController* data_channel_controller();
594 const DataChannelController* data_channel_controller() const;
595 cricket::PortAllocator* port_allocator();
596 const cricket::PortAllocator* port_allocator() const;
Harald Alvestrande15fb152020-10-19 13:28:05 +0000597 RtpTransmissionManager* rtp_manager();
598 const RtpTransmissionManager* rtp_manager() const;
Harald Alvestrandbc32c562022-02-09 12:08:47 +0000599 JsepTransportController* transport_controller_s()
600 RTC_RUN_ON(signaling_thread());
601 const JsepTransportController* transport_controller_s() const
602 RTC_RUN_ON(signaling_thread());
603 JsepTransportController* transport_controller_n()
604 RTC_RUN_ON(network_thread());
605 const JsepTransportController* transport_controller_n() const
606 RTC_RUN_ON(network_thread());
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000607 // ===================================================================
Harald Alvestrand763f5a92020-10-22 10:39:40 +0000608 const cricket::AudioOptions& audio_options() { return audio_options_; }
609 const cricket::VideoOptions& video_options() { return video_options_; }
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000610
Harald Alvestrand5b661302022-01-28 13:08:34 +0000611 PeerConnectionSdpMethods* const pc_;
Harald Alvestrand66c40362022-01-28 17:41:30 +0000612 ConnectionContext* const context_;
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000613
614 std::unique_ptr<WebRtcSessionDescriptionFactory> webrtc_session_desc_factory_
615 RTC_GUARDED_BY(signaling_thread());
616
617 std::unique_ptr<SessionDescriptionInterface> current_local_description_
618 RTC_GUARDED_BY(signaling_thread());
619 std::unique_ptr<SessionDescriptionInterface> pending_local_description_
620 RTC_GUARDED_BY(signaling_thread());
621 std::unique_ptr<SessionDescriptionInterface> current_remote_description_
622 RTC_GUARDED_BY(signaling_thread());
623 std::unique_ptr<SessionDescriptionInterface> pending_remote_description_
624 RTC_GUARDED_BY(signaling_thread());
625
626 PeerConnectionInterface::SignalingState signaling_state_
627 RTC_GUARDED_BY(signaling_thread()) = PeerConnectionInterface::kStable;
628
629 // Whether this peer is the caller. Set when the local description is applied.
630 absl::optional<bool> is_caller_ RTC_GUARDED_BY(signaling_thread());
631
Harald Alvestrand6f04b652020-10-09 11:42:17 +0000632 // Streams added via AddStream.
633 const rtc::scoped_refptr<StreamCollection> local_streams_
634 RTC_GUARDED_BY(signaling_thread());
635 // Streams created as a result of SetRemoteDescription.
636 const rtc::scoped_refptr<StreamCollection> remote_streams_
637 RTC_GUARDED_BY(signaling_thread());
638
639 std::vector<std::unique_ptr<MediaStreamObserver>> stream_observers_
640 RTC_GUARDED_BY(signaling_thread());
641
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000642 // The operations chain is used by the offer/answer exchange methods to ensure
643 // they are executed in the right order. For example, if
644 // SetRemoteDescription() is invoked while CreateOffer() is still pending, the
645 // SRD operation will not start until CreateOffer() has completed. See
646 // https://w3c.github.io/webrtc-pc/#dfn-operations-chain.
647 rtc::scoped_refptr<rtc::OperationsChain> operations_chain_
648 RTC_GUARDED_BY(signaling_thread());
649
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000650 // One PeerConnection has only one RTCP CNAME.
651 // https://tools.ietf.org/html/draft-ietf-rtcweb-rtp-usage-26#section-4.9
652 const std::string rtcp_cname_;
653
654 // MIDs will be generated using this generator which will keep track of
655 // all the MIDs that have been seen over the life of the PeerConnection.
656 rtc::UniqueStringGenerator mid_generator_ RTC_GUARDED_BY(signaling_thread());
657
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000658 // List of content names for which the remote side triggered an ICE restart.
659 std::set<std::string> pending_ice_restarts_
660 RTC_GUARDED_BY(signaling_thread());
661
662 std::unique_ptr<LocalIceCredentialsToReplace>
663 local_ice_credentials_to_replace_ RTC_GUARDED_BY(signaling_thread());
664
665 bool remote_peer_supports_msid_ RTC_GUARDED_BY(signaling_thread()) = false;
666 bool is_negotiation_needed_ RTC_GUARDED_BY(signaling_thread()) = false;
Harald Alvestrand42b983a2021-10-19 21:50:01 +0000667 uint32_t negotiation_needed_event_id_ RTC_GUARDED_BY(signaling_thread()) = 0;
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000668 bool update_negotiation_needed_on_empty_chain_
669 RTC_GUARDED_BY(signaling_thread()) = false;
Henrik Boström4ea80f32021-06-09 10:29:50 +0200670 // If PT demuxing is successfully negotiated one time we will allow PT
671 // demuxing for the rest of the session so that PT-based apps default to PT
672 // demuxing in follow-up O/A exchanges.
Harald Alvestrand42b983a2021-10-19 21:50:01 +0000673 bool pt_demuxing_has_been_used_audio_ RTC_GUARDED_BY(signaling_thread()) =
674 false;
675 bool pt_demuxing_has_been_used_video_ RTC_GUARDED_BY(signaling_thread()) =
676 false;
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000677
678 // In Unified Plan, if we encounter remote SDP that does not contain an a=msid
679 // line we create and use a stream with a random ID for our receivers. This is
680 // to support legacy endpoints that do not support the a=msid attribute (as
681 // opposed to streamless tracks with "a=msid:-").
682 rtc::scoped_refptr<MediaStreamInterface> missing_msid_default_stream_
683 RTC_GUARDED_BY(signaling_thread());
684
Harald Alvestranda474fbf2020-10-01 16:47:23 +0000685 SessionError session_error_ RTC_GUARDED_BY(signaling_thread()) =
686 SessionError::kNone;
687 std::string session_error_desc_ RTC_GUARDED_BY(signaling_thread());
688
Harald Alvestrand763f5a92020-10-22 10:39:40 +0000689 // Member variables for caching global options.
690 cricket::AudioOptions audio_options_ RTC_GUARDED_BY(signaling_thread());
691 cricket::VideoOptions video_options_ RTC_GUARDED_BY(signaling_thread());
692
Harald Alvestrand763f5a92020-10-22 10:39:40 +0000693 // A video bitrate allocator factory.
694 // This can be injected using the PeerConnectionDependencies,
695 // or else the CreateBuiltinVideoBitrateAllocatorFactory() will be called.
696 // Note that one can still choose to override this in a MediaEngine
697 // if one wants too.
698 std::unique_ptr<webrtc::VideoBitrateAllocatorFactory>
Harald Alvestrand42b983a2021-10-19 21:50:01 +0000699 video_bitrate_allocator_factory_ RTC_GUARDED_BY(signaling_thread());
Harald Alvestrand763f5a92020-10-22 10:39:40 +0000700
Harald Alvestrandbc32c562022-02-09 12:08:47 +0000701 // Whether we are the initial offerer on the association. This
702 // determines the SSL role.
703 absl::optional<bool> initial_offerer_ RTC_GUARDED_BY(signaling_thread());
704
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000705 rtc::WeakPtrFactory<SdpOfferAnswerHandler> weak_ptr_factory_
706 RTC_GUARDED_BY(signaling_thread());
707};
708
709} // namespace webrtc
710
711#endif // PC_SDP_OFFER_ANSWER_H_