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