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