jsep.h 9.4 KB

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