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