blob: 6a0aaaad2723e4ccc61fe6a80b9943a33b17ac22 [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 Alvestrandf01bd6c2020-10-23 13:30:46 +000061#include "rtc_base/third_party/sigslot/sigslot.h"
Harald Alvestrand1f7eab62020-10-18 16:51:47 +000062#include "rtc_base/thread.h"
63#include "rtc_base/thread_annotations.h"
Harald Alvestrandcdcfab02020-09-28 13:02:07 +000064#include "rtc_base/unique_id_generator.h"
65#include "rtc_base/weak_ptr.h"
66
Harald Alvestrand9e334b72022-05-04 13:38:31 +000067namespace cricket {
68class ChannelManager;
69}
70
Harald Alvestrandcdcfab02020-09-28 13:02:07 +000071namespace webrtc {
72
Harald Alvestrandcdcfab02020-09-28 13:02:07 +000073// SdpOfferAnswerHandler is a component
74// of the PeerConnection object as defined
75// by the PeerConnectionInterface API surface.
76// The class is responsible for the following:
77// - Parsing and interpreting SDP.
78// - Generating offers and answers based on the current state.
79// This class lives on the signaling thread.
Harald Alvestrandf01bd6c2020-10-23 13:30:46 +000080class SdpOfferAnswerHandler : public SdpStateProvider,
81 public sigslot::has_slots<> {
Harald Alvestrandcdcfab02020-09-28 13:02:07 +000082 public:
Harald Alvestrandcdcfab02020-09-28 13:02:07 +000083 ~SdpOfferAnswerHandler();
84
Harald Alvestrand9cd199d2020-10-27 07:10:43 +000085 // Creates an SdpOfferAnswerHandler. Modifies dependencies.
86 static std::unique_ptr<SdpOfferAnswerHandler> Create(
Harald Alvestrand5b661302022-01-28 13:08:34 +000087 PeerConnectionSdpMethods* pc,
Harald Alvestrand763f5a92020-10-22 10:39:40 +000088 const PeerConnectionInterface::RTCConfiguration& configuration,
Harald Alvestrand66c40362022-01-28 17:41:30 +000089 PeerConnectionDependencies& dependencies,
90 ConnectionContext* context);
Harald Alvestrand763f5a92020-10-22 10:39:40 +000091
Harald Alvestrandcdcfab02020-09-28 13:02:07 +000092 void ResetSessionDescFactory() {
93 RTC_DCHECK_RUN_ON(signaling_thread());
94 webrtc_session_desc_factory_.reset();
95 }
96 const WebRtcSessionDescriptionFactory* webrtc_session_desc_factory() const {
97 RTC_DCHECK_RUN_ON(signaling_thread());
98 return webrtc_session_desc_factory_.get();
99 }
100
101 // Change signaling state to Closed, and perform appropriate actions.
102 void Close();
103
104 // Called as part of destroying the owning PeerConnection.
105 void PrepareForShutdown();
106
Harald Alvestrandf01bd6c2020-10-23 13:30:46 +0000107 // Implementation of SdpStateProvider
108 PeerConnectionInterface::SignalingState signaling_state() const override;
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000109
Harald Alvestrandf01bd6c2020-10-23 13:30:46 +0000110 const SessionDescriptionInterface* local_description() const override;
111 const SessionDescriptionInterface* remote_description() const override;
112 const SessionDescriptionInterface* current_local_description() const override;
113 const SessionDescriptionInterface* current_remote_description()
114 const override;
115 const SessionDescriptionInterface* pending_local_description() const override;
116 const SessionDescriptionInterface* pending_remote_description()
117 const override;
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000118
Harald Alvestrandf01bd6c2020-10-23 13:30:46 +0000119 bool NeedsIceRestart(const std::string& content_name) const override;
120 bool IceRestartPending(const std::string& content_name) const override;
121 absl::optional<rtc::SSLRole> GetDtlsRole(
122 const std::string& mid) const override;
Harald Alvestrandd89ce532020-10-21 08:59:22 +0000123
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000124 void RestartIce();
125
126 // JSEP01
127 void CreateOffer(
128 CreateSessionDescriptionObserver* observer,
129 const PeerConnectionInterface::RTCOfferAnswerOptions& options);
130 void CreateAnswer(
131 CreateSessionDescriptionObserver* observer,
132 const PeerConnectionInterface::RTCOfferAnswerOptions& options);
133
134 void SetLocalDescription(
135 std::unique_ptr<SessionDescriptionInterface> desc,
136 rtc::scoped_refptr<SetLocalDescriptionObserverInterface> observer);
137 void SetLocalDescription(
138 rtc::scoped_refptr<SetLocalDescriptionObserverInterface> observer);
139 void SetLocalDescription(SetSessionDescriptionObserver* observer,
140 SessionDescriptionInterface* desc);
141 void SetLocalDescription(SetSessionDescriptionObserver* observer);
142
143 void SetRemoteDescription(
144 std::unique_ptr<SessionDescriptionInterface> desc,
145 rtc::scoped_refptr<SetRemoteDescriptionObserverInterface> observer);
146 void SetRemoteDescription(SetSessionDescriptionObserver* observer,
147 SessionDescriptionInterface* desc);
148
149 PeerConnectionInterface::RTCConfiguration GetConfiguration();
150 RTCError SetConfiguration(
151 const PeerConnectionInterface::RTCConfiguration& configuration);
152 bool AddIceCandidate(const IceCandidateInterface* candidate);
153 void AddIceCandidate(std::unique_ptr<IceCandidateInterface> candidate,
154 std::function<void(RTCError)> callback);
155 bool RemoveIceCandidates(const std::vector<cricket::Candidate>& candidates);
156 // Adds a locally generated candidate to the local description.
157 void AddLocalIceCandidate(const JsepIceCandidate* candidate);
158 void RemoveLocalIceCandidates(
159 const std::vector<cricket::Candidate>& candidates);
160 bool ShouldFireNegotiationNeededEvent(uint32_t event_id);
161
Harald Alvestrand6f04b652020-10-09 11:42:17 +0000162 bool AddStream(MediaStreamInterface* local_stream);
163 void RemoveStream(MediaStreamInterface* local_stream);
164
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000165 absl::optional<bool> is_caller();
166 bool HasNewIceCredentials();
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000167 void UpdateNegotiationNeeded();
168
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000169 // Destroys all BaseChannels and destroys the SCTP data channel, if present.
170 void DestroyAllChannels();
171
Harald Alvestrand6f04b652020-10-09 11:42:17 +0000172 rtc::scoped_refptr<StreamCollectionInterface> local_streams();
173 rtc::scoped_refptr<StreamCollectionInterface> remote_streams();
174
Harald Alvestrandbc32c562022-02-09 12:08:47 +0000175 bool initial_offerer() {
176 RTC_DCHECK_RUN_ON(signaling_thread());
177 if (initial_offerer_) {
178 return *initial_offerer_;
179 }
180 return false;
181 }
182
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000183 private:
Tomas Gunnarsson1c7c09b2022-01-12 13:11:04 +0100184 class RemoteDescriptionOperation;
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000185 class ImplicitCreateSessionDescriptionObserver;
Harald Alvestrand1f7eab62020-10-18 16:51:47 +0000186
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000187 friend class ImplicitCreateSessionDescriptionObserver;
188 class SetSessionDescriptionObserverAdapter;
Harald Alvestrand1f7eab62020-10-18 16:51:47 +0000189
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000190 friend class SetSessionDescriptionObserverAdapter;
191
Harald Alvestranda474fbf2020-10-01 16:47:23 +0000192 enum class SessionError {
193 kNone, // No error.
194 kContent, // Error in BaseChannel SetLocalContent/SetRemoteContent.
195 kTransport, // Error from the underlying transport.
196 };
197
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000198 // Represents the [[LocalIceCredentialsToReplace]] internal slot in the spec.
199 // It makes the next CreateOffer() produce new ICE credentials even if
200 // RTCOfferAnswerOptions::ice_restart is false.
201 // https://w3c.github.io/webrtc-pc/#dfn-localufragstoreplace
202 // TODO(hbos): When JsepTransportController/JsepTransport supports rollback,
203 // move this type of logic to JsepTransportController/JsepTransport.
204 class LocalIceCredentialsToReplace;
205
Harald Alvestrand9cd199d2020-10-27 07:10:43 +0000206 // Only called by the Create() function.
Harald Alvestrand66c40362022-01-28 17:41:30 +0000207 explicit SdpOfferAnswerHandler(PeerConnectionSdpMethods* pc,
208 ConnectionContext* context);
Harald Alvestrand9cd199d2020-10-27 07:10:43 +0000209 // Called from the `Create()` function. Can only be called
210 // once. Modifies dependencies.
211 void Initialize(
212 const PeerConnectionInterface::RTCConfiguration& configuration,
Jonas Orelanded99dae2022-03-09 09:28:10 +0100213 PeerConnectionDependencies& dependencies,
214 ConnectionContext* context);
Harald Alvestrand9cd199d2020-10-27 07:10:43 +0000215
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000216 rtc::Thread* signaling_thread() const;
Harald Alvestrandbc32c562022-02-09 12:08:47 +0000217 rtc::Thread* network_thread() const;
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000218 // Non-const versions of local_description()/remote_description(), for use
219 // internally.
220 SessionDescriptionInterface* mutable_local_description()
221 RTC_RUN_ON(signaling_thread()) {
222 return pending_local_description_ ? pending_local_description_.get()
223 : current_local_description_.get();
224 }
225 SessionDescriptionInterface* mutable_remote_description()
226 RTC_RUN_ON(signaling_thread()) {
227 return pending_remote_description_ ? pending_remote_description_.get()
228 : current_remote_description_.get();
229 }
230
231 // Synchronous implementations of SetLocalDescription/SetRemoteDescription
232 // that return an RTCError instead of invoking a callback.
233 RTCError ApplyLocalDescription(
Henrik Boströmf8187e02021-04-26 21:04:26 +0200234 std::unique_ptr<SessionDescriptionInterface> desc,
235 const std::map<std::string, const cricket::ContentGroup*>&
236 bundle_groups_by_mid);
Tomas Gunnarsson0dd75392022-01-17 19:19:56 +0100237 void ApplyRemoteDescription(
238 std::unique_ptr<RemoteDescriptionOperation> operation);
239
240 RTCError ReplaceRemoteDescription(
Henrik Boströmf8187e02021-04-26 21:04:26 +0200241 std::unique_ptr<SessionDescriptionInterface> desc,
Tomas Gunnarsson0dd75392022-01-17 19:19:56 +0100242 SdpType sdp_type,
243 std::unique_ptr<SessionDescriptionInterface>* replaced_description)
244 RTC_RUN_ON(signaling_thread());
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000245
Tomas Gunnarsson651586c2022-01-07 18:33:12 +0000246 // Part of ApplyRemoteDescription steps specific to Unified Plan.
247 void ApplyRemoteDescriptionUpdateTransceiverState(SdpType sdp_type);
248
Tomas Gunnarssonb625edf2022-01-07 16:22:14 +0000249 // Part of ApplyRemoteDescription steps specific to plan b.
250 void PlanBUpdateSendersAndReceivers(
251 const cricket::ContentInfo* audio_content,
252 const cricket::AudioContentDescription* audio_desc,
253 const cricket::ContentInfo* video_content,
254 const cricket::VideoContentDescription* video_desc);
255
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000256 // Implementation of the offer/answer exchange operations. These are chained
Artem Titov880fa812021-07-30 22:30:23 +0200257 // onto the `operations_chain_` when the public CreateOffer(), CreateAnswer(),
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000258 // SetLocalDescription() and SetRemoteDescription() methods are invoked.
259 void DoCreateOffer(
260 const PeerConnectionInterface::RTCOfferAnswerOptions& options,
261 rtc::scoped_refptr<CreateSessionDescriptionObserver> observer);
262 void DoCreateAnswer(
263 const PeerConnectionInterface::RTCOfferAnswerOptions& options,
264 rtc::scoped_refptr<CreateSessionDescriptionObserver> observer);
265 void DoSetLocalDescription(
266 std::unique_ptr<SessionDescriptionInterface> desc,
267 rtc::scoped_refptr<SetLocalDescriptionObserverInterface> observer);
268 void DoSetRemoteDescription(
Tomas Gunnarsson1c7c09b2022-01-12 13:11:04 +0100269 std::unique_ptr<RemoteDescriptionOperation> operation);
270
271 // Called after a DoSetRemoteDescription operation completes.
272 void SetRemoteDescriptionPostProcess(bool was_answer)
273 RTC_RUN_ON(signaling_thread());
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000274
275 // Update the state, signaling if necessary.
276 void ChangeSignalingState(
277 PeerConnectionInterface::SignalingState signaling_state);
278
Henrik Boströmf8187e02021-04-26 21:04:26 +0200279 RTCError UpdateSessionState(
280 SdpType type,
281 cricket::ContentSource source,
282 const cricket::SessionDescription* description,
283 const std::map<std::string, const cricket::ContentGroup*>&
284 bundle_groups_by_mid);
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000285
286 bool IsUnifiedPlan() const RTC_RUN_ON(signaling_thread());
287
Harald Alvestrand763f5a92020-10-22 10:39:40 +0000288 // Signals from MediaStreamObserver.
289 void OnAudioTrackAdded(AudioTrackInterface* track,
290 MediaStreamInterface* stream)
291 RTC_RUN_ON(signaling_thread());
292 void OnAudioTrackRemoved(AudioTrackInterface* track,
293 MediaStreamInterface* stream)
294 RTC_RUN_ON(signaling_thread());
295 void OnVideoTrackAdded(VideoTrackInterface* track,
296 MediaStreamInterface* stream)
297 RTC_RUN_ON(signaling_thread());
298 void OnVideoTrackRemoved(VideoTrackInterface* track,
299 MediaStreamInterface* stream)
300 RTC_RUN_ON(signaling_thread());
301
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000302 // | desc_type | is the type of the description that caused the rollback.
303 RTCError Rollback(SdpType desc_type);
304 void OnOperationsChainEmpty();
305
306 // Runs the algorithm **set the associated remote streams** specified in
307 // https://w3c.github.io/webrtc-pc/#set-associated-remote-streams.
308 void SetAssociatedRemoteStreams(
309 rtc::scoped_refptr<RtpReceiverInternal> receiver,
310 const std::vector<std::string>& stream_ids,
311 std::vector<rtc::scoped_refptr<MediaStreamInterface>>* added_streams,
312 std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams);
313
314 bool CheckIfNegotiationIsNeeded();
315 void GenerateNegotiationNeededEvent();
316 // Helper method which verifies SDP.
Henrik Boströmf8187e02021-04-26 21:04:26 +0200317 RTCError ValidateSessionDescription(
318 const SessionDescriptionInterface* sdesc,
319 cricket::ContentSource source,
320 const std::map<std::string, const cricket::ContentGroup*>&
321 bundle_groups_by_mid) RTC_RUN_ON(signaling_thread());
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000322
323 // Updates the local RtpTransceivers according to the JSEP rules. Called as
324 // part of setting the local/remote description.
325 RTCError UpdateTransceiversAndDataChannels(
326 cricket::ContentSource source,
327 const SessionDescriptionInterface& new_session,
328 const SessionDescriptionInterface* old_local_description,
Henrik Boströmf8187e02021-04-26 21:04:26 +0200329 const SessionDescriptionInterface* old_remote_description,
330 const std::map<std::string, const cricket::ContentGroup*>&
331 bundle_groups_by_mid);
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000332
333 // Associate the given transceiver according to the JSEP rules.
334 RTCErrorOr<
335 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>>
336 AssociateTransceiver(cricket::ContentSource source,
337 SdpType type,
338 size_t mline_index,
339 const cricket::ContentInfo& content,
340 const cricket::ContentInfo* old_local_content,
341 const cricket::ContentInfo* old_remote_content)
342 RTC_RUN_ON(signaling_thread());
343
Harald Alvestrand85466662021-04-19 21:21:36 +0000344 // Returns the media section in the given session description that is
345 // associated with the RtpTransceiver. Returns null if none found or this
346 // RtpTransceiver is not associated. Logic varies depending on the
347 // SdpSemantics specified in the configuration.
348 const cricket::ContentInfo* FindMediaSectionForTransceiver(
349 const RtpTransceiver* transceiver,
350 const SessionDescriptionInterface* sdesc) const;
351
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000352 // Either creates or destroys the transceiver's BaseChannel according to the
353 // given media section.
354 RTCError UpdateTransceiverChannel(
355 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
356 transceiver,
357 const cricket::ContentInfo& content,
358 const cricket::ContentGroup* bundle_group) RTC_RUN_ON(signaling_thread());
359
360 // Either creates or destroys the local data channel according to the given
361 // media section.
362 RTCError UpdateDataChannel(cricket::ContentSource source,
363 const cricket::ContentInfo& content,
364 const cricket::ContentGroup* bundle_group)
365 RTC_RUN_ON(signaling_thread());
Harald Alvestrandc06e3742020-10-01 10:23:33 +0000366 // Check if a call to SetLocalDescription is acceptable with a session
367 // description of the given type.
368 bool ExpectSetLocalDescription(SdpType type);
369 // Check if a call to SetRemoteDescription is acceptable with a session
370 // description of the given type.
371 bool ExpectSetRemoteDescription(SdpType type);
372
373 // The offer/answer machinery assumes the media section MID is present and
374 // unique. To support legacy end points that do not supply a=mid lines, this
375 // method will modify the session description to add MIDs generated according
376 // to the SDP semantics.
377 void FillInMissingRemoteMids(cricket::SessionDescription* remote_description);
378
Niels Möllerbe74b802022-03-18 14:10:15 +0100379 // Returns an RtpTransceiver, if available, that can be used to receive the
Harald Alvestrandc06e3742020-10-01 10:23:33 +0000380 // given media type according to JSEP rules.
381 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
382 FindAvailableTransceiverToReceive(cricket::MediaType media_type) const;
383
Artem Titov880fa812021-07-30 22:30:23 +0200384 // Returns a MediaSessionOptions struct with options decided by `options`,
Harald Alvestrandc06e3742020-10-01 10:23:33 +0000385 // the local MediaStreams and DataChannels.
386 void GetOptionsForOffer(const PeerConnectionInterface::RTCOfferAnswerOptions&
387 offer_answer_options,
388 cricket::MediaSessionOptions* session_options);
389 void GetOptionsForPlanBOffer(
390 const PeerConnectionInterface::RTCOfferAnswerOptions&
391 offer_answer_options,
392 cricket::MediaSessionOptions* session_options)
393 RTC_RUN_ON(signaling_thread());
394 void GetOptionsForUnifiedPlanOffer(
395 const PeerConnectionInterface::RTCOfferAnswerOptions&
396 offer_answer_options,
397 cricket::MediaSessionOptions* session_options)
398 RTC_RUN_ON(signaling_thread());
399
400 // Returns a MediaSessionOptions struct with options decided by
Artem Titov880fa812021-07-30 22:30:23 +0200401 // `constraints`, the local MediaStreams and DataChannels.
Harald Alvestrandc06e3742020-10-01 10:23:33 +0000402 void GetOptionsForAnswer(const PeerConnectionInterface::RTCOfferAnswerOptions&
403 offer_answer_options,
404 cricket::MediaSessionOptions* session_options);
405 void GetOptionsForPlanBAnswer(
406 const PeerConnectionInterface::RTCOfferAnswerOptions&
407 offer_answer_options,
408 cricket::MediaSessionOptions* session_options)
409 RTC_RUN_ON(signaling_thread());
410 void GetOptionsForUnifiedPlanAnswer(
411 const PeerConnectionInterface::RTCOfferAnswerOptions&
412 offer_answer_options,
413 cricket::MediaSessionOptions* session_options)
414 RTC_RUN_ON(signaling_thread());
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000415
Harald Alvestranda474fbf2020-10-01 16:47:23 +0000416 const char* SessionErrorToString(SessionError error) const;
417 std::string GetSessionErrorMsg();
418 // Returns the last error in the session. See the enum above for details.
419 SessionError session_error() const {
420 RTC_DCHECK_RUN_ON(signaling_thread());
421 return session_error_;
422 }
423 const std::string& session_error_desc() const { return session_error_desc_; }
424
425 RTCError HandleLegacyOfferOptions(
426 const PeerConnectionInterface::RTCOfferAnswerOptions& options);
427 void RemoveRecvDirectionFromReceivingTransceiversOfType(
428 cricket::MediaType media_type) RTC_RUN_ON(signaling_thread());
429 void AddUpToOneReceivingTransceiverOfType(cricket::MediaType media_type);
430
431 std::vector<
432 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>>
433 GetReceivingTransceiversOfType(cricket::MediaType media_type)
434 RTC_RUN_ON(signaling_thread());
435
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000436 // Runs the algorithm specified in
437 // https://w3c.github.io/webrtc-pc/#process-remote-track-removal
Harald Alvestranda474fbf2020-10-01 16:47:23 +0000438 // This method will update the following lists:
Artem Titov880fa812021-07-30 22:30:23 +0200439 // `remove_list` is the list of transceivers for which the receiving track is
Harald Alvestranda474fbf2020-10-01 16:47:23 +0000440 // being removed.
Artem Titov880fa812021-07-30 22:30:23 +0200441 // `removed_streams` is the list of streams which no longer have a receiving
Harald Alvestranda474fbf2020-10-01 16:47:23 +0000442 // track so should be removed.
Harald Alvestranda474fbf2020-10-01 16:47:23 +0000443 void ProcessRemovalOfRemoteTrack(
Harald Alvestrand85466662021-04-19 21:21:36 +0000444 const rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
Harald Alvestranda474fbf2020-10-01 16:47:23 +0000445 transceiver,
446 std::vector<rtc::scoped_refptr<RtpTransceiverInterface>>* remove_list,
447 std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams);
448
449 void RemoveRemoteStreamsIfEmpty(
450 const std::vector<rtc::scoped_refptr<MediaStreamInterface>>&
451 remote_streams,
452 std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams);
453
Artem Titov880fa812021-07-30 22:30:23 +0200454 // Remove all local and remote senders of type `media_type`.
Harald Alvestranda474fbf2020-10-01 16:47:23 +0000455 // Called when a media type is rejected (m-line set to port 0).
456 void RemoveSenders(cricket::MediaType media_type);
457
Artem Titov880fa812021-07-30 22:30:23 +0200458 // Loops through the vector of `streams` and finds added and removed
Harald Alvestranda474fbf2020-10-01 16:47:23 +0000459 // StreamParams since last time this method was called.
460 // For each new or removed StreamParam, OnLocalSenderSeen or
461 // OnLocalSenderRemoved is invoked.
462 void UpdateLocalSenders(const std::vector<cricket::StreamParams>& streams,
463 cricket::MediaType media_type);
464
Artem Titov880fa812021-07-30 22:30:23 +0200465 // Makes sure a MediaStreamTrack is created for each StreamParam in `streams`,
Harald Alvestranda474fbf2020-10-01 16:47:23 +0000466 // and existing MediaStreamTracks are removed if there is no corresponding
Artem Titov880fa812021-07-30 22:30:23 +0200467 // StreamParam. If `default_track_needed` is true, a default MediaStreamTrack
Harald Alvestranda474fbf2020-10-01 16:47:23 +0000468 // is created if it doesn't exist; if false, it's removed if it exists.
Artem Titov880fa812021-07-30 22:30:23 +0200469 // `media_type` is the type of the `streams` and can be either audio or video.
470 // If a new MediaStream is created it is added to `new_streams`.
Harald Alvestranda474fbf2020-10-01 16:47:23 +0000471 void UpdateRemoteSendersList(
472 const std::vector<cricket::StreamParams>& streams,
473 bool default_track_needed,
474 cricket::MediaType media_type,
475 StreamCollection* new_streams);
476
Taylor Brandstetterd0acbd82021-01-25 13:44:55 -0800477 // Enables media channels to allow sending of media.
Harald Alvestrand48171ec2021-04-20 15:06:03 +0000478 // This enables media to flow on all configured audio/video channels.
Taylor Brandstetterd0acbd82021-01-25 13:44:55 -0800479 void EnableSending();
Harald Alvestranda474fbf2020-10-01 16:47:23 +0000480 // Push the media parts of the local or remote session description
Harald Alvestrand8101e7b2022-05-23 14:57:47 +0000481 // down to all of the channels, and start SCTP if needed.
Henrik Boströmf8187e02021-04-26 21:04:26 +0200482 RTCError PushdownMediaDescription(
483 SdpType type,
484 cricket::ContentSource source,
485 const std::map<std::string, const cricket::ContentGroup*>&
486 bundle_groups_by_mid);
Harald Alvestranda474fbf2020-10-01 16:47:23 +0000487
488 RTCError PushdownTransportDescription(cricket::ContentSource source,
489 SdpType type);
490 // Helper function to remove stopped transceivers.
491 void RemoveStoppedTransceivers();
Artem Titov880fa812021-07-30 22:30:23 +0200492 // Deletes the corresponding channel of contents that don't exist in `desc`.
493 // `desc` can be null. This means that all channels are deleted.
Harald Alvestranda474fbf2020-10-01 16:47:23 +0000494 void RemoveUnusedChannels(const cricket::SessionDescription* desc);
495
496 // Report inferred negotiated SDP semantics from a local/remote answer to the
497 // UMA observer.
498 void ReportNegotiatedSdpSemantics(const SessionDescriptionInterface& answer);
499
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000500 // Finds remote MediaStreams without any tracks and removes them from
Artem Titov880fa812021-07-30 22:30:23 +0200501 // `remote_streams_` and notifies the observer that the MediaStreams no longer
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000502 // exist.
503 void UpdateEndedRemoteMediaStreams();
504
Tomas Gunnarsson0dd75392022-01-17 19:19:56 +0100505 // Uses all remote candidates in the currently set remote_description().
506 // If no remote description is currently set (nullptr), the return value will
507 // be true. If `UseCandidate()` fails for any candidate in the remote
508 // description, the return value will be false.
509 bool UseCandidatesInRemoteDescription();
Artem Titov880fa812021-07-30 22:30:23 +0200510 // Uses `candidate` in this session.
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000511 bool UseCandidate(const IceCandidateInterface* candidate);
512 // Returns true if we are ready to push down the remote candidate.
Artem Titov880fa812021-07-30 22:30:23 +0200513 // `remote_desc` is the new remote description, or NULL if the current remote
514 // description should be used. Output `valid` is true if the candidate media
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000515 // index is valid.
516 bool ReadyToUseRemoteCandidate(const IceCandidateInterface* candidate,
517 const SessionDescriptionInterface* remote_desc,
518 bool* valid);
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000519
520 RTCErrorOr<const cricket::ContentInfo*> FindContentInfo(
521 const SessionDescriptionInterface* description,
522 const IceCandidateInterface* candidate) RTC_RUN_ON(signaling_thread());
523
524 // Functions for dealing with transports.
525 // Note that cricket code uses the term "channel" for what other code
526 // refers to as "transport".
527
Artem Titov880fa812021-07-30 22:30:23 +0200528 // Allocates media channels based on the `desc`. If `desc` doesn't have
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000529 // the BUNDLE option, this method will disable BUNDLE in PortAllocator.
530 // This method will also delete any existing media channels before creating.
531 RTCError CreateChannels(const cricket::SessionDescription& desc);
532
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000533 bool CreateDataChannel(const std::string& mid);
534
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000535 // Destroys the RTP data channel transport and/or the SCTP data channel
536 // transport and clears it.
Florent Castellidcb9ffc2021-06-29 14:58:23 +0200537 void DestroyDataChannelTransport(RTCError error);
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000538
Artem Titov880fa812021-07-30 22:30:23 +0200539 // Generates MediaDescriptionOptions for the `session_opts` based on existing
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000540 // local description or remote description.
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000541 void GenerateMediaDescriptionOptions(
542 const SessionDescriptionInterface* session_desc,
543 RtpTransceiverDirection audio_direction,
544 RtpTransceiverDirection video_direction,
545 absl::optional<size_t>* audio_index,
546 absl::optional<size_t>* video_index,
547 absl::optional<size_t>* data_index,
548 cricket::MediaSessionOptions* session_options);
549
550 // Generates the active MediaDescriptionOptions for the local data channel
551 // given the specified MID.
552 cricket::MediaDescriptionOptions GetMediaDescriptionOptionsForActiveData(
553 const std::string& mid) const;
554
555 // Generates the rejected MediaDescriptionOptions for the local data channel
556 // given the specified MID.
557 cricket::MediaDescriptionOptions GetMediaDescriptionOptionsForRejectedData(
558 const std::string& mid) const;
559
Taylor Brandstetterd0acbd82021-01-25 13:44:55 -0800560 // Based on number of transceivers per media type, enabled or disable
561 // payload type based demuxing in the affected channels.
Henrik Boströmf8187e02021-04-26 21:04:26 +0200562 bool UpdatePayloadTypeDemuxingState(
563 cricket::ContentSource source,
564 const std::map<std::string, const cricket::ContentGroup*>&
565 bundle_groups_by_mid);
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000566
Harald Alvestrand42b983a2021-10-19 21:50:01 +0000567 // Updates the error state, signaling if necessary.
568 void SetSessionError(SessionError error, const std::string& error_desc);
569
570 // Implements AddIceCandidate without reporting usage, but returns the
571 // particular success/error value that should be reported (and can be utilized
572 // for other purposes).
573 AddIceCandidateResult AddIceCandidateInternal(
574 const IceCandidateInterface* candidate);
575
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000576 // ==================================================================
577 // Access to pc_ variables
578 cricket::ChannelManager* channel_manager() const;
Harald Alvestrand35f4b4c2022-05-16 10:36:43 +0000579 cricket::MediaEngineInterface* media_engine() const;
Harald Alvestrande15fb152020-10-19 13:28:05 +0000580 TransceiverList* transceivers();
581 const TransceiverList* transceivers() const;
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000582 DataChannelController* data_channel_controller();
583 const DataChannelController* data_channel_controller() const;
584 cricket::PortAllocator* port_allocator();
585 const cricket::PortAllocator* port_allocator() const;
Harald Alvestrande15fb152020-10-19 13:28:05 +0000586 RtpTransmissionManager* rtp_manager();
587 const RtpTransmissionManager* rtp_manager() const;
Harald Alvestrandbc32c562022-02-09 12:08:47 +0000588 JsepTransportController* transport_controller_s()
589 RTC_RUN_ON(signaling_thread());
590 const JsepTransportController* transport_controller_s() const
591 RTC_RUN_ON(signaling_thread());
592 JsepTransportController* transport_controller_n()
593 RTC_RUN_ON(network_thread());
594 const JsepTransportController* transport_controller_n() const
595 RTC_RUN_ON(network_thread());
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000596 // ===================================================================
Harald Alvestrand763f5a92020-10-22 10:39:40 +0000597 const cricket::AudioOptions& audio_options() { return audio_options_; }
598 const cricket::VideoOptions& video_options() { return video_options_; }
Harald Alvestrand8101e7b2022-05-23 14:57:47 +0000599 bool ConfiguredForMedia() const;
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000600
Harald Alvestrand5b661302022-01-28 13:08:34 +0000601 PeerConnectionSdpMethods* const pc_;
Harald Alvestrand66c40362022-01-28 17:41:30 +0000602 ConnectionContext* const context_;
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000603
604 std::unique_ptr<WebRtcSessionDescriptionFactory> webrtc_session_desc_factory_
605 RTC_GUARDED_BY(signaling_thread());
606
607 std::unique_ptr<SessionDescriptionInterface> current_local_description_
608 RTC_GUARDED_BY(signaling_thread());
609 std::unique_ptr<SessionDescriptionInterface> pending_local_description_
610 RTC_GUARDED_BY(signaling_thread());
611 std::unique_ptr<SessionDescriptionInterface> current_remote_description_
612 RTC_GUARDED_BY(signaling_thread());
613 std::unique_ptr<SessionDescriptionInterface> pending_remote_description_
614 RTC_GUARDED_BY(signaling_thread());
615
616 PeerConnectionInterface::SignalingState signaling_state_
617 RTC_GUARDED_BY(signaling_thread()) = PeerConnectionInterface::kStable;
618
619 // Whether this peer is the caller. Set when the local description is applied.
620 absl::optional<bool> is_caller_ RTC_GUARDED_BY(signaling_thread());
621
Harald Alvestrand6f04b652020-10-09 11:42:17 +0000622 // Streams added via AddStream.
623 const rtc::scoped_refptr<StreamCollection> local_streams_
624 RTC_GUARDED_BY(signaling_thread());
625 // Streams created as a result of SetRemoteDescription.
626 const rtc::scoped_refptr<StreamCollection> remote_streams_
627 RTC_GUARDED_BY(signaling_thread());
628
629 std::vector<std::unique_ptr<MediaStreamObserver>> stream_observers_
630 RTC_GUARDED_BY(signaling_thread());
631
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000632 // The operations chain is used by the offer/answer exchange methods to ensure
633 // they are executed in the right order. For example, if
634 // SetRemoteDescription() is invoked while CreateOffer() is still pending, the
635 // SRD operation will not start until CreateOffer() has completed. See
636 // https://w3c.github.io/webrtc-pc/#dfn-operations-chain.
637 rtc::scoped_refptr<rtc::OperationsChain> operations_chain_
638 RTC_GUARDED_BY(signaling_thread());
639
Harald Alvestrandbc9ca252020-10-05 13:08:41 +0000640 // One PeerConnection has only one RTCP CNAME.
641 // https://tools.ietf.org/html/draft-ietf-rtcweb-rtp-usage-26#section-4.9
642 const std::string rtcp_cname_;
643
644 // MIDs will be generated using this generator which will keep track of
645 // all the MIDs that have been seen over the life of the PeerConnection.
646 rtc::UniqueStringGenerator mid_generator_ RTC_GUARDED_BY(signaling_thread());
647
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000648 // List of content names for which the remote side triggered an ICE restart.
649 std::set<std::string> pending_ice_restarts_
650 RTC_GUARDED_BY(signaling_thread());
651
652 std::unique_ptr<LocalIceCredentialsToReplace>
653 local_ice_credentials_to_replace_ RTC_GUARDED_BY(signaling_thread());
654
655 bool remote_peer_supports_msid_ RTC_GUARDED_BY(signaling_thread()) = false;
656 bool is_negotiation_needed_ RTC_GUARDED_BY(signaling_thread()) = false;
Harald Alvestrand42b983a2021-10-19 21:50:01 +0000657 uint32_t negotiation_needed_event_id_ RTC_GUARDED_BY(signaling_thread()) = 0;
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000658 bool update_negotiation_needed_on_empty_chain_
659 RTC_GUARDED_BY(signaling_thread()) = false;
Henrik Boström4ea80f32021-06-09 10:29:50 +0200660 // If PT demuxing is successfully negotiated one time we will allow PT
661 // demuxing for the rest of the session so that PT-based apps default to PT
662 // demuxing in follow-up O/A exchanges.
Harald Alvestrand42b983a2021-10-19 21:50:01 +0000663 bool pt_demuxing_has_been_used_audio_ RTC_GUARDED_BY(signaling_thread()) =
664 false;
665 bool pt_demuxing_has_been_used_video_ RTC_GUARDED_BY(signaling_thread()) =
666 false;
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000667
668 // In Unified Plan, if we encounter remote SDP that does not contain an a=msid
669 // line we create and use a stream with a random ID for our receivers. This is
670 // to support legacy endpoints that do not support the a=msid attribute (as
671 // opposed to streamless tracks with "a=msid:-").
672 rtc::scoped_refptr<MediaStreamInterface> missing_msid_default_stream_
673 RTC_GUARDED_BY(signaling_thread());
674
Harald Alvestranda474fbf2020-10-01 16:47:23 +0000675 SessionError session_error_ RTC_GUARDED_BY(signaling_thread()) =
676 SessionError::kNone;
677 std::string session_error_desc_ RTC_GUARDED_BY(signaling_thread());
678
Harald Alvestrand763f5a92020-10-22 10:39:40 +0000679 // Member variables for caching global options.
680 cricket::AudioOptions audio_options_ RTC_GUARDED_BY(signaling_thread());
681 cricket::VideoOptions video_options_ RTC_GUARDED_BY(signaling_thread());
682
Harald Alvestrand763f5a92020-10-22 10:39:40 +0000683 // A video bitrate allocator factory.
684 // This can be injected using the PeerConnectionDependencies,
685 // or else the CreateBuiltinVideoBitrateAllocatorFactory() will be called.
686 // Note that one can still choose to override this in a MediaEngine
687 // if one wants too.
688 std::unique_ptr<webrtc::VideoBitrateAllocatorFactory>
Harald Alvestrand42b983a2021-10-19 21:50:01 +0000689 video_bitrate_allocator_factory_ RTC_GUARDED_BY(signaling_thread());
Harald Alvestrand763f5a92020-10-22 10:39:40 +0000690
Harald Alvestrandbc32c562022-02-09 12:08:47 +0000691 // Whether we are the initial offerer on the association. This
692 // determines the SSL role.
693 absl::optional<bool> initial_offerer_ RTC_GUARDED_BY(signaling_thread());
694
Harald Alvestrandcdcfab02020-09-28 13:02:07 +0000695 rtc::WeakPtrFactory<SdpOfferAnswerHandler> weak_ptr_factory_
696 RTC_GUARDED_BY(signaling_thread());
697};
698
699} // namespace webrtc
700
701#endif // PC_SDP_OFFER_ANSWER_H_