You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

250 lines
9.6 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright 2012 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. // This file contains declarations of interfaces that wrap SDP-related
  11. // constructs; session descriptions and ICE candidates. The inner "cricket::"
  12. // objects shouldn't be accessed directly; the intention is that an application
  13. // using the PeerConnection API only creates these objects from strings, and
  14. // them passes them into the PeerConnection.
  15. //
  16. // Though in the future, we're planning to provide an SDP parsing API, with a
  17. // structure more friendly than cricket::SessionDescription.
  18. #ifndef API_JSEP_H_
  19. #define API_JSEP_H_
  20. #include <stddef.h>
  21. #include <memory>
  22. #include <string>
  23. #include <vector>
  24. #include "absl/types/optional.h"
  25. #include "api/rtc_error.h"
  26. #include "rtc_base/ref_count.h"
  27. #include "rtc_base/system/rtc_export.h"
  28. namespace cricket {
  29. class Candidate;
  30. class SessionDescription;
  31. } // namespace cricket
  32. namespace webrtc {
  33. struct SdpParseError {
  34. public:
  35. // The sdp line that causes the error.
  36. std::string line;
  37. // Explains the error.
  38. std::string description;
  39. };
  40. // Class representation of an ICE candidate.
  41. //
  42. // An instance of this interface is supposed to be owned by one class at
  43. // a time and is therefore not expected to be thread safe.
  44. //
  45. // An instance can be created by CreateIceCandidate.
  46. class RTC_EXPORT IceCandidateInterface {
  47. public:
  48. virtual ~IceCandidateInterface() {}
  49. // If present, this is the value of the "a=mid" attribute of the candidate's
  50. // m= section in SDP, which identifies the m= section.
  51. virtual std::string sdp_mid() const = 0;
  52. // This indicates the index (starting at zero) of m= section this candidate
  53. // is associated with. Needed when an endpoint doesn't support MIDs.
  54. virtual int sdp_mline_index() const = 0;
  55. // Only for use internally.
  56. virtual const cricket::Candidate& candidate() const = 0;
  57. // The URL of the ICE server which this candidate was gathered from.
  58. // TODO(zhihuang): Remove the default implementation once the subclasses
  59. // implement this method.
  60. virtual std::string server_url() const;
  61. // Creates a SDP-ized form of this candidate.
  62. virtual bool ToString(std::string* out) const = 0;
  63. };
  64. // Creates a IceCandidateInterface based on SDP string.
  65. // Returns null if the sdp string can't be parsed.
  66. // |error| may be null.
  67. RTC_EXPORT IceCandidateInterface* CreateIceCandidate(const std::string& sdp_mid,
  68. int sdp_mline_index,
  69. const std::string& sdp,
  70. SdpParseError* error);
  71. // Creates an IceCandidateInterface based on a parsed candidate structure.
  72. RTC_EXPORT std::unique_ptr<IceCandidateInterface> CreateIceCandidate(
  73. const std::string& sdp_mid,
  74. int sdp_mline_index,
  75. const cricket::Candidate& candidate);
  76. // This class represents a collection of candidates for a specific m= section.
  77. // Used in SessionDescriptionInterface.
  78. class IceCandidateCollection {
  79. public:
  80. virtual ~IceCandidateCollection() {}
  81. virtual size_t count() const = 0;
  82. // Returns true if an equivalent |candidate| exist in the collection.
  83. virtual bool HasCandidate(const IceCandidateInterface* candidate) const = 0;
  84. virtual const IceCandidateInterface* at(size_t index) const = 0;
  85. };
  86. // Enum that describes the type of the SessionDescriptionInterface.
  87. // Corresponds to RTCSdpType in the WebRTC specification.
  88. // https://w3c.github.io/webrtc-pc/#dom-rtcsdptype
  89. enum class SdpType {
  90. kOffer, // Description must be treated as an SDP offer.
  91. kPrAnswer, // Description must be treated as an SDP answer, but not a final
  92. // answer.
  93. kAnswer, // Description must be treated as an SDP final answer, and the
  94. // offer-answer exchange must be considered complete after
  95. // receiving this.
  96. kRollback // Resets any pending offers and sets signaling state back to
  97. // stable.
  98. };
  99. // Returns the string form of the given SDP type. String forms are defined in
  100. // SessionDescriptionInterface.
  101. const char* SdpTypeToString(SdpType type);
  102. // Returns the SdpType from its string form. The string form can be one of the
  103. // constants defined in SessionDescriptionInterface. Passing in any other string
  104. // results in nullopt.
  105. absl::optional<SdpType> SdpTypeFromString(const std::string& type_str);
  106. // Class representation of an SDP session description.
  107. //
  108. // An instance of this interface is supposed to be owned by one class at a time
  109. // and is therefore not expected to be thread safe.
  110. //
  111. // An instance can be created by CreateSessionDescription.
  112. class RTC_EXPORT SessionDescriptionInterface {
  113. public:
  114. // String representations of the supported SDP types.
  115. static const char kOffer[];
  116. static const char kPrAnswer[];
  117. static const char kAnswer[];
  118. static const char kRollback[];
  119. virtual ~SessionDescriptionInterface() {}
  120. // Only for use internally.
  121. virtual cricket::SessionDescription* description() = 0;
  122. virtual const cricket::SessionDescription* description() const = 0;
  123. // Get the session id and session version, which are defined based on
  124. // RFC 4566 for the SDP o= line.
  125. virtual std::string session_id() const = 0;
  126. virtual std::string session_version() const = 0;
  127. // Returns the type of this session description as an SdpType. Descriptions of
  128. // the various types are found in the SdpType documentation.
  129. // TODO(steveanton): Remove default implementation once Chromium has been
  130. // updated.
  131. virtual SdpType GetType() const;
  132. // kOffer/kPrAnswer/kAnswer
  133. // TODO(steveanton): Remove this in favor of |GetType| that returns SdpType.
  134. virtual std::string type() const = 0;
  135. // Adds the specified candidate to the description.
  136. //
  137. // Ownership is not transferred.
  138. //
  139. // Returns false if the session description does not have a media section
  140. // that corresponds to |candidate.sdp_mid()| or
  141. // |candidate.sdp_mline_index()|.
  142. virtual bool AddCandidate(const IceCandidateInterface* candidate) = 0;
  143. // Removes the candidates from the description, if found.
  144. //
  145. // Returns the number of candidates removed.
  146. virtual size_t RemoveCandidates(
  147. const std::vector<cricket::Candidate>& candidates);
  148. // Returns the number of m= sections in the session description.
  149. virtual size_t number_of_mediasections() const = 0;
  150. // Returns a collection of all candidates that belong to a certain m=
  151. // section.
  152. virtual const IceCandidateCollection* candidates(
  153. size_t mediasection_index) const = 0;
  154. // Serializes the description to SDP.
  155. virtual bool ToString(std::string* out) const = 0;
  156. };
  157. // Creates a SessionDescriptionInterface based on the SDP string and the type.
  158. // Returns null if the sdp string can't be parsed or the type is unsupported.
  159. // |error| may be null.
  160. // TODO(steveanton): This function is deprecated. Please use the functions below
  161. // which take an SdpType enum instead. Remove this once it is no longer used.
  162. RTC_EXPORT SessionDescriptionInterface* CreateSessionDescription(
  163. const std::string& type,
  164. const std::string& sdp,
  165. SdpParseError* error);
  166. // Creates a SessionDescriptionInterface based on the SDP string and the type.
  167. // Returns null if the SDP string cannot be parsed.
  168. // If using the signature with |error_out|, details of the parsing error may be
  169. // written to |error_out| if it is not null.
  170. RTC_EXPORT std::unique_ptr<SessionDescriptionInterface>
  171. CreateSessionDescription(SdpType type, const std::string& sdp);
  172. RTC_EXPORT std::unique_ptr<SessionDescriptionInterface>
  173. CreateSessionDescription(SdpType type,
  174. const std::string& sdp,
  175. SdpParseError* error_out);
  176. // Creates a SessionDescriptionInterface based on a parsed SDP structure and the
  177. // given type, ID and version.
  178. std::unique_ptr<SessionDescriptionInterface> CreateSessionDescription(
  179. SdpType type,
  180. const std::string& session_id,
  181. const std::string& session_version,
  182. std::unique_ptr<cricket::SessionDescription> description);
  183. // CreateOffer and CreateAnswer callback interface.
  184. class RTC_EXPORT CreateSessionDescriptionObserver
  185. : public rtc::RefCountInterface {
  186. public:
  187. // This callback transfers the ownership of the |desc|.
  188. // TODO(deadbeef): Make this take an std::unique_ptr<> to avoid confusion
  189. // around ownership.
  190. virtual void OnSuccess(SessionDescriptionInterface* desc) = 0;
  191. // The OnFailure callback takes an RTCError, which consists of an
  192. // error code and a string.
  193. // RTCError is non-copyable, so it must be passed using std::move.
  194. // Earlier versions of the API used a string argument. This version
  195. // is deprecated; in order to let clients remove the old version, it has a
  196. // default implementation. If both versions are unimplemented, the
  197. // result will be a runtime error (stack overflow). This is intentional.
  198. virtual void OnFailure(RTCError error);
  199. virtual void OnFailure(const std::string& error);
  200. protected:
  201. ~CreateSessionDescriptionObserver() override = default;
  202. };
  203. // SetLocalDescription and SetRemoteDescription callback interface.
  204. class RTC_EXPORT SetSessionDescriptionObserver : public rtc::RefCountInterface {
  205. public:
  206. virtual void OnSuccess() = 0;
  207. // See description in CreateSessionDescriptionObserver for OnFailure.
  208. virtual void OnFailure(RTCError error);
  209. virtual void OnFailure(const std::string& error);
  210. protected:
  211. ~SetSessionDescriptionObserver() override = default;
  212. };
  213. } // namespace webrtc
  214. #endif // API_JSEP_H_