henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 2 | * Copyright 2012 The WebRTC project authors. All Rights Reserved. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3 | * |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 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. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
deadbeef | b10f32f | 2017-02-08 01:38:21 -0800 | [diff] [blame] | 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. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 19 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 20 | #ifndef API_JSEP_H_ |
| 21 | #define API_JSEP_H_ |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 22 | |
pbos | 9baddf2 | 2017-01-02 06:44:41 -0800 | [diff] [blame] | 23 | #include <stddef.h> |
| 24 | |
Steve Anton | 88f2cb9 | 2017-12-05 12:47:32 -0800 | [diff] [blame] | 25 | #include <memory> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 26 | #include <string> |
| 27 | #include <vector> |
| 28 | |
Danil Chapovalov | 0bc58cf | 2018-06-21 13:32:56 +0200 | [diff] [blame^] | 29 | #include "absl/types/optional.h" |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 30 | #include "api/rtcerror.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 31 | #include "rtc_base/refcount.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 32 | |
| 33 | namespace cricket { |
tommi | 6f59a4f | 2016-03-11 14:05:09 -0800 | [diff] [blame] | 34 | class Candidate; |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 35 | class SessionDescription; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 36 | } // namespace cricket |
| 37 | |
| 38 | namespace webrtc { |
| 39 | |
| 40 | struct SdpParseError { |
| 41 | public: |
| 42 | // The sdp line that causes the error. |
| 43 | std::string line; |
| 44 | // Explains the error. |
| 45 | std::string description; |
| 46 | }; |
| 47 | |
| 48 | // Class representation of an ICE candidate. |
deadbeef | b10f32f | 2017-02-08 01:38:21 -0800 | [diff] [blame] | 49 | // |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 50 | // An instance of this interface is supposed to be owned by one class at |
| 51 | // a time and is therefore not expected to be thread safe. |
deadbeef | b10f32f | 2017-02-08 01:38:21 -0800 | [diff] [blame] | 52 | // |
| 53 | // An instance can be created by CreateIceCandidate. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 54 | class IceCandidateInterface { |
| 55 | public: |
| 56 | virtual ~IceCandidateInterface() {} |
deadbeef | b10f32f | 2017-02-08 01:38:21 -0800 | [diff] [blame] | 57 | // If present, this is the value of the "a=mid" attribute of the candidate's |
| 58 | // m= section in SDP, which identifies the m= section. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 59 | virtual std::string sdp_mid() const = 0; |
deadbeef | b10f32f | 2017-02-08 01:38:21 -0800 | [diff] [blame] | 60 | // This indicates the index (starting at zero) of m= section this candidate |
Steve Anton | 88f2cb9 | 2017-12-05 12:47:32 -0800 | [diff] [blame] | 61 | // is associated with. Needed when an endpoint doesn't support MIDs. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 62 | virtual int sdp_mline_index() const = 0; |
deadbeef | b10f32f | 2017-02-08 01:38:21 -0800 | [diff] [blame] | 63 | // Only for use internally. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 64 | virtual const cricket::Candidate& candidate() const = 0; |
zhihuang | d7e771d | 2017-02-16 11:29:39 -0800 | [diff] [blame] | 65 | // The URL of the ICE server which this candidate was gathered from. |
| 66 | // TODO(zhihuang): Remove the default implementation once the subclasses |
| 67 | // implement this method. |
Steve Anton | 845bb73 | 2017-12-05 12:50:26 -0800 | [diff] [blame] | 68 | virtual std::string server_url() const; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 69 | // Creates a SDP-ized form of this candidate. |
| 70 | virtual bool ToString(std::string* out) const = 0; |
| 71 | }; |
| 72 | |
| 73 | // Creates a IceCandidateInterface based on SDP string. |
deadbeef | 8d60a94 | 2017-02-27 14:47:33 -0800 | [diff] [blame] | 74 | // Returns null if the sdp string can't be parsed. |
| 75 | // |error| may be null. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 76 | IceCandidateInterface* CreateIceCandidate(const std::string& sdp_mid, |
| 77 | int sdp_mline_index, |
| 78 | const std::string& sdp, |
| 79 | SdpParseError* error); |
| 80 | |
deadbeef | b10f32f | 2017-02-08 01:38:21 -0800 | [diff] [blame] | 81 | // This class represents a collection of candidates for a specific m= section. |
| 82 | // Used in SessionDescriptionInterface. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 83 | class IceCandidateCollection { |
| 84 | public: |
| 85 | virtual ~IceCandidateCollection() {} |
| 86 | virtual size_t count() const = 0; |
| 87 | // Returns true if an equivalent |candidate| exist in the collection. |
| 88 | virtual bool HasCandidate(const IceCandidateInterface* candidate) const = 0; |
| 89 | virtual const IceCandidateInterface* at(size_t index) const = 0; |
| 90 | }; |
| 91 | |
Steve Anton | 88f2cb9 | 2017-12-05 12:47:32 -0800 | [diff] [blame] | 92 | // Enum that describes the type of the SessionDescriptionInterface. |
| 93 | // Corresponds to RTCSdpType in the WebRTC specification. |
| 94 | // https://w3c.github.io/webrtc-pc/#dom-rtcsdptype |
| 95 | enum class SdpType { |
| 96 | kOffer, // Description must be treated as an SDP offer. |
| 97 | kPrAnswer, // Description must be treated as an SDP answer, but not a final |
| 98 | // answer. |
| 99 | kAnswer // Description must be treated as an SDP final answer, and the offer- |
| 100 | // answer exchange must be considered complete after receiving this. |
| 101 | }; |
| 102 | |
| 103 | // Returns the string form of the given SDP type. String forms are defined in |
| 104 | // SessionDescriptionInterface. |
| 105 | const char* SdpTypeToString(SdpType type); |
| 106 | |
| 107 | // Returns the SdpType from its string form. The string form can be one of the |
| 108 | // constants defined in SessionDescriptionInterface. Passing in any other string |
| 109 | // results in nullopt. |
Danil Chapovalov | 0bc58cf | 2018-06-21 13:32:56 +0200 | [diff] [blame^] | 110 | absl::optional<SdpType> SdpTypeFromString(const std::string& type_str); |
Steve Anton | 88f2cb9 | 2017-12-05 12:47:32 -0800 | [diff] [blame] | 111 | |
deadbeef | b10f32f | 2017-02-08 01:38:21 -0800 | [diff] [blame] | 112 | // Class representation of an SDP session description. |
| 113 | // |
| 114 | // An instance of this interface is supposed to be owned by one class at a time |
| 115 | // and is therefore not expected to be thread safe. |
| 116 | // |
| 117 | // An instance can be created by CreateSessionDescription. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 118 | class SessionDescriptionInterface { |
| 119 | public: |
Steve Anton | 88f2cb9 | 2017-12-05 12:47:32 -0800 | [diff] [blame] | 120 | // String representations of the supported SDP types. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 121 | static const char kOffer[]; |
| 122 | static const char kPrAnswer[]; |
| 123 | static const char kAnswer[]; |
| 124 | |
| 125 | virtual ~SessionDescriptionInterface() {} |
deadbeef | b10f32f | 2017-02-08 01:38:21 -0800 | [diff] [blame] | 126 | |
| 127 | // Only for use internally. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 128 | virtual cricket::SessionDescription* description() = 0; |
| 129 | virtual const cricket::SessionDescription* description() const = 0; |
deadbeef | b10f32f | 2017-02-08 01:38:21 -0800 | [diff] [blame] | 130 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 131 | // Get the session id and session version, which are defined based on |
| 132 | // RFC 4566 for the SDP o= line. |
| 133 | virtual std::string session_id() const = 0; |
| 134 | virtual std::string session_version() const = 0; |
deadbeef | b10f32f | 2017-02-08 01:38:21 -0800 | [diff] [blame] | 135 | |
Steve Anton | 88f2cb9 | 2017-12-05 12:47:32 -0800 | [diff] [blame] | 136 | // Returns the type of this session description as an SdpType. Descriptions of |
| 137 | // the various types are found in the SdpType documentation. |
| 138 | // TODO(steveanton): Remove default implementation once Chromium has been |
| 139 | // updated. |
| 140 | virtual SdpType GetType() const; |
| 141 | |
deadbeef | b10f32f | 2017-02-08 01:38:21 -0800 | [diff] [blame] | 142 | // kOffer/kPrAnswer/kAnswer |
Steve Anton | 88f2cb9 | 2017-12-05 12:47:32 -0800 | [diff] [blame] | 143 | // TODO(steveanton): Remove this in favor of |GetType| that returns SdpType. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 144 | virtual std::string type() const = 0; |
deadbeef | b10f32f | 2017-02-08 01:38:21 -0800 | [diff] [blame] | 145 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 146 | // Adds the specified candidate to the description. |
deadbeef | b10f32f | 2017-02-08 01:38:21 -0800 | [diff] [blame] | 147 | // |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 148 | // Ownership is not transferred. |
deadbeef | b10f32f | 2017-02-08 01:38:21 -0800 | [diff] [blame] | 149 | // |
| 150 | // Returns false if the session description does not have a media section |
| 151 | // that corresponds to |candidate.sdp_mid()| or |
| 152 | // |candidate.sdp_mline_index()|. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 153 | virtual bool AddCandidate(const IceCandidateInterface* candidate) = 0; |
deadbeef | b10f32f | 2017-02-08 01:38:21 -0800 | [diff] [blame] | 154 | |
| 155 | // Removes the candidates from the description, if found. |
| 156 | // |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 157 | // Returns the number of candidates removed. |
| 158 | virtual size_t RemoveCandidates( |
Steve Anton | 845bb73 | 2017-12-05 12:50:26 -0800 | [diff] [blame] | 159 | const std::vector<cricket::Candidate>& candidates); |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 160 | |
deadbeef | b10f32f | 2017-02-08 01:38:21 -0800 | [diff] [blame] | 161 | // Returns the number of m= sections in the session description. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 162 | virtual size_t number_of_mediasections() const = 0; |
deadbeef | b10f32f | 2017-02-08 01:38:21 -0800 | [diff] [blame] | 163 | |
| 164 | // Returns a collection of all candidates that belong to a certain m= |
| 165 | // section. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 166 | virtual const IceCandidateCollection* candidates( |
| 167 | size_t mediasection_index) const = 0; |
deadbeef | b10f32f | 2017-02-08 01:38:21 -0800 | [diff] [blame] | 168 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 169 | // Serializes the description to SDP. |
| 170 | virtual bool ToString(std::string* out) const = 0; |
| 171 | }; |
| 172 | |
deadbeef | b10f32f | 2017-02-08 01:38:21 -0800 | [diff] [blame] | 173 | // Creates a SessionDescriptionInterface based on the SDP string and the type. |
deadbeef | 8d60a94 | 2017-02-27 14:47:33 -0800 | [diff] [blame] | 174 | // Returns null if the sdp string can't be parsed or the type is unsupported. |
| 175 | // |error| may be null. |
Steve Anton | 88f2cb9 | 2017-12-05 12:47:32 -0800 | [diff] [blame] | 176 | // TODO(steveanton): This function is deprecated. Please use the functions below |
| 177 | // which take an SdpType enum instead. Remove this once it is no longer used. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 178 | SessionDescriptionInterface* CreateSessionDescription(const std::string& type, |
| 179 | const std::string& sdp, |
| 180 | SdpParseError* error); |
| 181 | |
Steve Anton | 88f2cb9 | 2017-12-05 12:47:32 -0800 | [diff] [blame] | 182 | // Creates a SessionDescriptionInterface based on the SDP string and the type. |
| 183 | // Returns null if the SDP string cannot be parsed. |
| 184 | // If using the signature with |error_out|, details of the parsing error may be |
| 185 | // written to |error_out| if it is not null. |
| 186 | std::unique_ptr<SessionDescriptionInterface> CreateSessionDescription( |
| 187 | SdpType type, |
| 188 | const std::string& sdp); |
| 189 | std::unique_ptr<SessionDescriptionInterface> CreateSessionDescription( |
| 190 | SdpType type, |
| 191 | const std::string& sdp, |
| 192 | SdpParseError* error_out); |
| 193 | |
deadbeef | b10f32f | 2017-02-08 01:38:21 -0800 | [diff] [blame] | 194 | // CreateOffer and CreateAnswer callback interface. |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 195 | class CreateSessionDescriptionObserver : public rtc::RefCountInterface { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 196 | public: |
deadbeef | b10f32f | 2017-02-08 01:38:21 -0800 | [diff] [blame] | 197 | // This callback transfers the ownership of the |desc|. |
| 198 | // TODO(deadbeef): Make this take an std::unique_ptr<> to avoid confusion |
| 199 | // around ownership. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 200 | virtual void OnSuccess(SessionDescriptionInterface* desc) = 0; |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 201 | // The OnFailure callback takes an RTCError, which consists of an |
| 202 | // error code and a string. |
| 203 | // RTCError is non-copyable, so it must be passed using std::move. |
| 204 | // Earlier versions of the API used a string argument. This version |
| 205 | // is deprecated; in order to let clients remove the old version, it has a |
| 206 | // default implementation. If both versions are unimplemented, the |
| 207 | // result will be a runtime error (stack overflow). This is intentional. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 208 | virtual void OnFailure(RTCError error) { OnFailure(error.message()); } |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 209 | virtual void OnFailure(const std::string& error) { |
| 210 | OnFailure(RTCError(RTCErrorType::INTERNAL_ERROR, std::string(error))); |
| 211 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 212 | |
| 213 | protected: |
Steve Anton | 845bb73 | 2017-12-05 12:50:26 -0800 | [diff] [blame] | 214 | ~CreateSessionDescriptionObserver() override = default; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 215 | }; |
| 216 | |
deadbeef | b10f32f | 2017-02-08 01:38:21 -0800 | [diff] [blame] | 217 | // SetLocalDescription and SetRemoteDescription callback interface. |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 218 | class SetSessionDescriptionObserver : public rtc::RefCountInterface { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 219 | public: |
| 220 | virtual void OnSuccess() = 0; |
Harald Alvestrand | 5081c0c | 2018-03-09 15:18:03 +0100 | [diff] [blame] | 221 | // See description in CreateSessionDescriptionObserver for OnFailure. |
| 222 | virtual void OnFailure(RTCError error) { |
| 223 | std::string message(error.message()); |
| 224 | OnFailure(message); |
| 225 | } |
| 226 | virtual void OnFailure(const std::string& error) { |
| 227 | OnFailure(RTCError(RTCErrorType::INTERNAL_ERROR, std::string(error))); |
| 228 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 229 | |
| 230 | protected: |
Steve Anton | 845bb73 | 2017-12-05 12:50:26 -0800 | [diff] [blame] | 231 | ~SetSessionDescriptionObserver() override = default; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 232 | }; |
| 233 | |
| 234 | } // namespace webrtc |
| 235 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 236 | #endif // API_JSEP_H_ |