sdp_offer_answer.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /*
  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. #ifndef PC_SDP_OFFER_ANSWER_H_
  11. #define PC_SDP_OFFER_ANSWER_H_
  12. #include <map>
  13. #include <memory>
  14. #include <set>
  15. #include <string>
  16. #include <utility>
  17. #include <vector>
  18. #include "api/jsep_ice_candidate.h"
  19. #include "api/peer_connection_interface.h"
  20. #include "api/transport/data_channel_transport_interface.h"
  21. #include "api/turn_customizer.h"
  22. #include "pc/data_channel_controller.h"
  23. #include "pc/ice_server_parsing.h"
  24. #include "pc/jsep_transport_controller.h"
  25. #include "pc/peer_connection_factory.h"
  26. #include "pc/peer_connection_internal.h"
  27. #include "pc/rtc_stats_collector.h"
  28. #include "pc/rtp_sender.h"
  29. #include "pc/rtp_transceiver.h"
  30. #include "pc/sctp_transport.h"
  31. #include "pc/stats_collector.h"
  32. #include "pc/stream_collection.h"
  33. #include "pc/webrtc_session_description_factory.h"
  34. #include "rtc_base/experiments/field_trial_parser.h"
  35. #include "rtc_base/operations_chain.h"
  36. #include "rtc_base/race_checker.h"
  37. #include "rtc_base/unique_id_generator.h"
  38. #include "rtc_base/weak_ptr.h"
  39. namespace webrtc {
  40. class MediaStreamObserver;
  41. class PeerConnection;
  42. class VideoRtpReceiver;
  43. class RtcEventLog;
  44. // SdpOfferAnswerHandler is a component
  45. // of the PeerConnection object as defined
  46. // by the PeerConnectionInterface API surface.
  47. // The class is responsible for the following:
  48. // - Parsing and interpreting SDP.
  49. // - Generating offers and answers based on the current state.
  50. // This class lives on the signaling thread.
  51. class SdpOfferAnswerHandler {
  52. public:
  53. explicit SdpOfferAnswerHandler(PeerConnection* pc);
  54. ~SdpOfferAnswerHandler();
  55. void SetSessionDescFactory(
  56. std::unique_ptr<WebRtcSessionDescriptionFactory> factory) {
  57. RTC_DCHECK_RUN_ON(signaling_thread());
  58. webrtc_session_desc_factory_ = std::move(factory);
  59. }
  60. void ResetSessionDescFactory() {
  61. RTC_DCHECK_RUN_ON(signaling_thread());
  62. webrtc_session_desc_factory_.reset();
  63. }
  64. const WebRtcSessionDescriptionFactory* webrtc_session_desc_factory() const {
  65. RTC_DCHECK_RUN_ON(signaling_thread());
  66. return webrtc_session_desc_factory_.get();
  67. }
  68. // Change signaling state to Closed, and perform appropriate actions.
  69. void Close();
  70. // Called as part of destroying the owning PeerConnection.
  71. void PrepareForShutdown();
  72. PeerConnectionInterface::SignalingState signaling_state() const;
  73. const SessionDescriptionInterface* local_description() const;
  74. const SessionDescriptionInterface* remote_description() const;
  75. const SessionDescriptionInterface* current_local_description() const;
  76. const SessionDescriptionInterface* current_remote_description() const;
  77. const SessionDescriptionInterface* pending_local_description() const;
  78. const SessionDescriptionInterface* pending_remote_description() const;
  79. void RestartIce();
  80. // JSEP01
  81. void CreateOffer(
  82. CreateSessionDescriptionObserver* observer,
  83. const PeerConnectionInterface::RTCOfferAnswerOptions& options);
  84. void CreateAnswer(
  85. CreateSessionDescriptionObserver* observer,
  86. const PeerConnectionInterface::RTCOfferAnswerOptions& options);
  87. void SetLocalDescription(
  88. std::unique_ptr<SessionDescriptionInterface> desc,
  89. rtc::scoped_refptr<SetLocalDescriptionObserverInterface> observer);
  90. void SetLocalDescription(
  91. rtc::scoped_refptr<SetLocalDescriptionObserverInterface> observer);
  92. void SetLocalDescription(SetSessionDescriptionObserver* observer,
  93. SessionDescriptionInterface* desc);
  94. void SetLocalDescription(SetSessionDescriptionObserver* observer);
  95. void SetRemoteDescription(
  96. std::unique_ptr<SessionDescriptionInterface> desc,
  97. rtc::scoped_refptr<SetRemoteDescriptionObserverInterface> observer);
  98. void SetRemoteDescription(SetSessionDescriptionObserver* observer,
  99. SessionDescriptionInterface* desc);
  100. PeerConnectionInterface::RTCConfiguration GetConfiguration();
  101. RTCError SetConfiguration(
  102. const PeerConnectionInterface::RTCConfiguration& configuration);
  103. bool AddIceCandidate(const IceCandidateInterface* candidate);
  104. void AddIceCandidate(std::unique_ptr<IceCandidateInterface> candidate,
  105. std::function<void(RTCError)> callback);
  106. bool RemoveIceCandidates(const std::vector<cricket::Candidate>& candidates);
  107. // Adds a locally generated candidate to the local description.
  108. void AddLocalIceCandidate(const JsepIceCandidate* candidate);
  109. void RemoveLocalIceCandidates(
  110. const std::vector<cricket::Candidate>& candidates);
  111. bool ShouldFireNegotiationNeededEvent(uint32_t event_id);
  112. absl::optional<bool> is_caller();
  113. bool HasNewIceCredentials();
  114. bool IceRestartPending(const std::string& content_name) const;
  115. void UpdateNegotiationNeeded();
  116. void SetHavePendingRtpDataChannel() {
  117. RTC_DCHECK_RUN_ON(signaling_thread());
  118. have_pending_rtp_data_channel_ = true;
  119. }
  120. // Returns the media section in the given session description that is
  121. // associated with the RtpTransceiver. Returns null if none found or this
  122. // RtpTransceiver is not associated. Logic varies depending on the
  123. // SdpSemantics specified in the configuration.
  124. const cricket::ContentInfo* FindMediaSectionForTransceiver(
  125. rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
  126. transceiver,
  127. const SessionDescriptionInterface* sdesc) const;
  128. private:
  129. class ImplicitCreateSessionDescriptionObserver;
  130. friend class ImplicitCreateSessionDescriptionObserver;
  131. class SetSessionDescriptionObserverAdapter;
  132. friend class SetSessionDescriptionObserverAdapter;
  133. // Represents the [[LocalIceCredentialsToReplace]] internal slot in the spec.
  134. // It makes the next CreateOffer() produce new ICE credentials even if
  135. // RTCOfferAnswerOptions::ice_restart is false.
  136. // https://w3c.github.io/webrtc-pc/#dfn-localufragstoreplace
  137. // TODO(hbos): When JsepTransportController/JsepTransport supports rollback,
  138. // move this type of logic to JsepTransportController/JsepTransport.
  139. class LocalIceCredentialsToReplace;
  140. rtc::Thread* signaling_thread() const;
  141. // Non-const versions of local_description()/remote_description(), for use
  142. // internally.
  143. SessionDescriptionInterface* mutable_local_description()
  144. RTC_RUN_ON(signaling_thread()) {
  145. return pending_local_description_ ? pending_local_description_.get()
  146. : current_local_description_.get();
  147. }
  148. SessionDescriptionInterface* mutable_remote_description()
  149. RTC_RUN_ON(signaling_thread()) {
  150. return pending_remote_description_ ? pending_remote_description_.get()
  151. : current_remote_description_.get();
  152. }
  153. // Synchronous implementations of SetLocalDescription/SetRemoteDescription
  154. // that return an RTCError instead of invoking a callback.
  155. RTCError ApplyLocalDescription(
  156. std::unique_ptr<SessionDescriptionInterface> desc);
  157. RTCError ApplyRemoteDescription(
  158. std::unique_ptr<SessionDescriptionInterface> desc);
  159. // Implementation of the offer/answer exchange operations. These are chained
  160. // onto the |operations_chain_| when the public CreateOffer(), CreateAnswer(),
  161. // SetLocalDescription() and SetRemoteDescription() methods are invoked.
  162. void DoCreateOffer(
  163. const PeerConnectionInterface::RTCOfferAnswerOptions& options,
  164. rtc::scoped_refptr<CreateSessionDescriptionObserver> observer);
  165. void DoCreateAnswer(
  166. const PeerConnectionInterface::RTCOfferAnswerOptions& options,
  167. rtc::scoped_refptr<CreateSessionDescriptionObserver> observer);
  168. void DoSetLocalDescription(
  169. std::unique_ptr<SessionDescriptionInterface> desc,
  170. rtc::scoped_refptr<SetLocalDescriptionObserverInterface> observer);
  171. void DoSetRemoteDescription(
  172. std::unique_ptr<SessionDescriptionInterface> desc,
  173. rtc::scoped_refptr<SetRemoteDescriptionObserverInterface> observer);
  174. // Update the state, signaling if necessary.
  175. void ChangeSignalingState(
  176. PeerConnectionInterface::SignalingState signaling_state);
  177. RTCError UpdateSessionState(SdpType type,
  178. cricket::ContentSource source,
  179. const cricket::SessionDescription* description);
  180. bool IsUnifiedPlan() const RTC_RUN_ON(signaling_thread());
  181. // | desc_type | is the type of the description that caused the rollback.
  182. RTCError Rollback(SdpType desc_type);
  183. void OnOperationsChainEmpty();
  184. // Runs the algorithm **set the associated remote streams** specified in
  185. // https://w3c.github.io/webrtc-pc/#set-associated-remote-streams.
  186. void SetAssociatedRemoteStreams(
  187. rtc::scoped_refptr<RtpReceiverInternal> receiver,
  188. const std::vector<std::string>& stream_ids,
  189. std::vector<rtc::scoped_refptr<MediaStreamInterface>>* added_streams,
  190. std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams);
  191. bool CheckIfNegotiationIsNeeded();
  192. void GenerateNegotiationNeededEvent();
  193. // Helper method which verifies SDP.
  194. RTCError ValidateSessionDescription(const SessionDescriptionInterface* sdesc,
  195. cricket::ContentSource source)
  196. RTC_RUN_ON(signaling_thread());
  197. // Updates the local RtpTransceivers according to the JSEP rules. Called as
  198. // part of setting the local/remote description.
  199. RTCError UpdateTransceiversAndDataChannels(
  200. cricket::ContentSource source,
  201. const SessionDescriptionInterface& new_session,
  202. const SessionDescriptionInterface* old_local_description,
  203. const SessionDescriptionInterface* old_remote_description);
  204. // Associate the given transceiver according to the JSEP rules.
  205. RTCErrorOr<
  206. rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>>
  207. AssociateTransceiver(cricket::ContentSource source,
  208. SdpType type,
  209. size_t mline_index,
  210. const cricket::ContentInfo& content,
  211. const cricket::ContentInfo* old_local_content,
  212. const cricket::ContentInfo* old_remote_content)
  213. RTC_RUN_ON(signaling_thread());
  214. // If the BUNDLE policy is max-bundle, then we know for sure that all
  215. // transports will be bundled from the start. This method returns the BUNDLE
  216. // group if that's the case, or null if BUNDLE will be negotiated later. An
  217. // error is returned if max-bundle is specified but the session description
  218. // does not have a BUNDLE group.
  219. RTCErrorOr<const cricket::ContentGroup*> GetEarlyBundleGroup(
  220. const cricket::SessionDescription& desc) const
  221. RTC_RUN_ON(signaling_thread());
  222. // Either creates or destroys the transceiver's BaseChannel according to the
  223. // given media section.
  224. RTCError UpdateTransceiverChannel(
  225. rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
  226. transceiver,
  227. const cricket::ContentInfo& content,
  228. const cricket::ContentGroup* bundle_group) RTC_RUN_ON(signaling_thread());
  229. // Either creates or destroys the local data channel according to the given
  230. // media section.
  231. RTCError UpdateDataChannel(cricket::ContentSource source,
  232. const cricket::ContentInfo& content,
  233. const cricket::ContentGroup* bundle_group)
  234. RTC_RUN_ON(signaling_thread());
  235. // Check if a call to SetLocalDescription is acceptable with a session
  236. // description of the given type.
  237. bool ExpectSetLocalDescription(SdpType type);
  238. // Check if a call to SetRemoteDescription is acceptable with a session
  239. // description of the given type.
  240. bool ExpectSetRemoteDescription(SdpType type);
  241. // The offer/answer machinery assumes the media section MID is present and
  242. // unique. To support legacy end points that do not supply a=mid lines, this
  243. // method will modify the session description to add MIDs generated according
  244. // to the SDP semantics.
  245. void FillInMissingRemoteMids(cricket::SessionDescription* remote_description);
  246. // Returns an RtpTransciever, if available, that can be used to receive the
  247. // given media type according to JSEP rules.
  248. rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
  249. FindAvailableTransceiverToReceive(cricket::MediaType media_type) const;
  250. // Returns a MediaSessionOptions struct with options decided by |options|,
  251. // the local MediaStreams and DataChannels.
  252. void GetOptionsForOffer(const PeerConnectionInterface::RTCOfferAnswerOptions&
  253. offer_answer_options,
  254. cricket::MediaSessionOptions* session_options);
  255. void GetOptionsForPlanBOffer(
  256. const PeerConnectionInterface::RTCOfferAnswerOptions&
  257. offer_answer_options,
  258. cricket::MediaSessionOptions* session_options)
  259. RTC_RUN_ON(signaling_thread());
  260. void GetOptionsForUnifiedPlanOffer(
  261. const PeerConnectionInterface::RTCOfferAnswerOptions&
  262. offer_answer_options,
  263. cricket::MediaSessionOptions* session_options)
  264. RTC_RUN_ON(signaling_thread());
  265. // Returns a MediaSessionOptions struct with options decided by
  266. // |constraints|, the local MediaStreams and DataChannels.
  267. void GetOptionsForAnswer(const PeerConnectionInterface::RTCOfferAnswerOptions&
  268. offer_answer_options,
  269. cricket::MediaSessionOptions* session_options);
  270. void GetOptionsForPlanBAnswer(
  271. const PeerConnectionInterface::RTCOfferAnswerOptions&
  272. offer_answer_options,
  273. cricket::MediaSessionOptions* session_options)
  274. RTC_RUN_ON(signaling_thread());
  275. void GetOptionsForUnifiedPlanAnswer(
  276. const PeerConnectionInterface::RTCOfferAnswerOptions&
  277. offer_answer_options,
  278. cricket::MediaSessionOptions* session_options)
  279. RTC_RUN_ON(signaling_thread());
  280. // ===================================================================
  281. PeerConnection* const pc_;
  282. std::unique_ptr<WebRtcSessionDescriptionFactory> webrtc_session_desc_factory_
  283. RTC_GUARDED_BY(signaling_thread());
  284. std::unique_ptr<SessionDescriptionInterface> current_local_description_
  285. RTC_GUARDED_BY(signaling_thread());
  286. std::unique_ptr<SessionDescriptionInterface> pending_local_description_
  287. RTC_GUARDED_BY(signaling_thread());
  288. std::unique_ptr<SessionDescriptionInterface> current_remote_description_
  289. RTC_GUARDED_BY(signaling_thread());
  290. std::unique_ptr<SessionDescriptionInterface> pending_remote_description_
  291. RTC_GUARDED_BY(signaling_thread());
  292. PeerConnectionInterface::SignalingState signaling_state_
  293. RTC_GUARDED_BY(signaling_thread()) = PeerConnectionInterface::kStable;
  294. // Whether this peer is the caller. Set when the local description is applied.
  295. absl::optional<bool> is_caller_ RTC_GUARDED_BY(signaling_thread());
  296. // The operations chain is used by the offer/answer exchange methods to ensure
  297. // they are executed in the right order. For example, if
  298. // SetRemoteDescription() is invoked while CreateOffer() is still pending, the
  299. // SRD operation will not start until CreateOffer() has completed. See
  300. // https://w3c.github.io/webrtc-pc/#dfn-operations-chain.
  301. rtc::scoped_refptr<rtc::OperationsChain> operations_chain_
  302. RTC_GUARDED_BY(signaling_thread());
  303. // List of content names for which the remote side triggered an ICE restart.
  304. std::set<std::string> pending_ice_restarts_
  305. RTC_GUARDED_BY(signaling_thread());
  306. std::unique_ptr<LocalIceCredentialsToReplace>
  307. local_ice_credentials_to_replace_ RTC_GUARDED_BY(signaling_thread());
  308. bool remote_peer_supports_msid_ RTC_GUARDED_BY(signaling_thread()) = false;
  309. bool is_negotiation_needed_ RTC_GUARDED_BY(signaling_thread()) = false;
  310. uint32_t negotiation_needed_event_id_ = 0;
  311. bool update_negotiation_needed_on_empty_chain_
  312. RTC_GUARDED_BY(signaling_thread()) = false;
  313. // In Unified Plan, if we encounter remote SDP that does not contain an a=msid
  314. // line we create and use a stream with a random ID for our receivers. This is
  315. // to support legacy endpoints that do not support the a=msid attribute (as
  316. // opposed to streamless tracks with "a=msid:-").
  317. rtc::scoped_refptr<MediaStreamInterface> missing_msid_default_stream_
  318. RTC_GUARDED_BY(signaling_thread());
  319. // Used when rolling back RTP data channels.
  320. bool have_pending_rtp_data_channel_ RTC_GUARDED_BY(signaling_thread()) =
  321. false;
  322. rtc::WeakPtrFactory<SdpOfferAnswerHandler> weak_ptr_factory_
  323. RTC_GUARDED_BY(signaling_thread());
  324. };
  325. } // namespace webrtc
  326. #endif // PC_SDP_OFFER_ANSWER_H_