blob: dcf821369eec0b3bc4148f290628e2d1df1bd997 [file] [log] [blame]
Hsin-Yu Chao47ed2092018-03-17 16:50:45 +08001/*
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
Hsin-Yu Chaoedc34c82018-08-09 17:53:05 +080029#include "absl/types/optional.h"
Hsin-Yu Chaof76cafb2019-04-01 13:54:10 +080030#include "api/rtc_error.h"
Hsin-Yu Chao8873da32020-11-30 07:50:42 +000031#include "rtc_base/deprecation.h"
Hsin-Yu Chaof76cafb2019-04-01 13:54:10 +080032#include "rtc_base/ref_count.h"
Hsin-Yu Chao640d48b2018-11-09 16:51:50 +080033#include "rtc_base/system/rtc_export.h"
Hsin-Yu Chao47ed2092018-03-17 16:50:45 +080034
35namespace cricket {
36class Candidate;
37class SessionDescription;
38} // namespace cricket
39
40namespace webrtc {
41
42struct 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.
Hsin-Yu Chao8873da32020-11-30 07:50:42 +000056class RTC_EXPORT IceCandidateInterface {
Hsin-Yu Chao47ed2092018-03-17 16:50:45 +080057 public:
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.
Hsin-Yu Chao640d48b2018-11-09 16:51:50 +080078RTC_EXPORT IceCandidateInterface* CreateIceCandidate(const std::string& sdp_mid,
79 int sdp_mline_index,
80 const std::string& sdp,
81 SdpParseError* error);
Hsin-Yu Chao47ed2092018-03-17 16:50:45 +080082
Hsin-Yu Chaoedc7e2a2018-08-24 14:02:41 +080083// Creates an IceCandidateInterface based on a parsed candidate structure.
Hsin-Yu Chao640d48b2018-11-09 16:51:50 +080084RTC_EXPORT std::unique_ptr<IceCandidateInterface> CreateIceCandidate(
Hsin-Yu Chaoedc7e2a2018-08-24 14:02:41 +080085 const std::string& sdp_mid,
86 int sdp_mline_index,
87 const cricket::Candidate& candidate);
88
Hsin-Yu Chao47ed2092018-03-17 16:50:45 +080089// This class represents a collection of candidates for a specific m= section.
90// Used in SessionDescriptionInterface.
91class IceCandidateCollection {
92 public:
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
103enum 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.
Hsin-Yu Chao8873da32020-11-30 07:50:42 +0000107 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.
Hsin-Yu Chao47ed2092018-03-17 16:50:45 +0800112};
113
114// Returns the string form of the given SDP type. String forms are defined in
115// SessionDescriptionInterface.
Hsin-Yu Chao8873da32020-11-30 07:50:42 +0000116RTC_EXPORT const char* SdpTypeToString(SdpType type);
Hsin-Yu Chao47ed2092018-03-17 16:50:45 +0800117
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.
Hsin-Yu Chaoedc34c82018-08-09 17:53:05 +0800121absl::optional<SdpType> SdpTypeFromString(const std::string& type_str);
Hsin-Yu Chao47ed2092018-03-17 16:50:45 +0800122
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.
Hsin-Yu Chao640d48b2018-11-09 16:51:50 +0800129class RTC_EXPORT SessionDescriptionInterface {
Hsin-Yu Chao47ed2092018-03-17 16:50:45 +0800130 public:
131 // String representations of the supported SDP types.
132 static const char kOffer[];
133 static const char kPrAnswer[];
134 static const char kAnswer[];
Hsin-Yu Chao8873da32020-11-30 07:50:42 +0000135 static const char kRollback[];
Hsin-Yu Chao47ed2092018-03-17 16:50:45 +0800136
137 virtual ~SessionDescriptionInterface() {}
138
Hsin-Yu Chaoe2b05122021-02-03 09:52:37 +0000139 // Create a new SessionDescriptionInterface object
140 // with the same values as the old object.
141 // TODO(bugs.webrtc.org:12215): Remove default implementation
142 virtual std::unique_ptr<SessionDescriptionInterface> Clone() const {
143 return nullptr;
144 }
145
Hsin-Yu Chao47ed2092018-03-17 16:50:45 +0800146 // Only for use internally.
147 virtual cricket::SessionDescription* description() = 0;
148 virtual const cricket::SessionDescription* description() const = 0;
149
150 // Get the session id and session version, which are defined based on
151 // RFC 4566 for the SDP o= line.
152 virtual std::string session_id() const = 0;
153 virtual std::string session_version() const = 0;
154
155 // Returns the type of this session description as an SdpType. Descriptions of
156 // the various types are found in the SdpType documentation.
157 // TODO(steveanton): Remove default implementation once Chromium has been
158 // updated.
159 virtual SdpType GetType() const;
160
161 // kOffer/kPrAnswer/kAnswer
162 // TODO(steveanton): Remove this in favor of |GetType| that returns SdpType.
163 virtual std::string type() const = 0;
164
165 // Adds the specified candidate to the description.
166 //
167 // Ownership is not transferred.
168 //
169 // Returns false if the session description does not have a media section
170 // that corresponds to |candidate.sdp_mid()| or
171 // |candidate.sdp_mline_index()|.
172 virtual bool AddCandidate(const IceCandidateInterface* candidate) = 0;
173
174 // Removes the candidates from the description, if found.
175 //
176 // Returns the number of candidates removed.
177 virtual size_t RemoveCandidates(
178 const std::vector<cricket::Candidate>& candidates);
179
180 // Returns the number of m= sections in the session description.
181 virtual size_t number_of_mediasections() const = 0;
182
183 // Returns a collection of all candidates that belong to a certain m=
184 // section.
185 virtual const IceCandidateCollection* candidates(
186 size_t mediasection_index) const = 0;
187
188 // Serializes the description to SDP.
189 virtual bool ToString(std::string* out) const = 0;
190};
191
192// Creates a SessionDescriptionInterface based on the SDP string and the type.
193// Returns null if the sdp string can't be parsed or the type is unsupported.
194// |error| may be null.
195// TODO(steveanton): This function is deprecated. Please use the functions below
196// which take an SdpType enum instead. Remove this once it is no longer used.
Hsin-Yu Chao640d48b2018-11-09 16:51:50 +0800197RTC_EXPORT SessionDescriptionInterface* CreateSessionDescription(
198 const std::string& type,
199 const std::string& sdp,
200 SdpParseError* error);
Hsin-Yu Chao47ed2092018-03-17 16:50:45 +0800201
202// Creates a SessionDescriptionInterface based on the SDP string and the type.
203// Returns null if the SDP string cannot be parsed.
204// If using the signature with |error_out|, details of the parsing error may be
205// written to |error_out| if it is not null.
Hsin-Yu Chao640d48b2018-11-09 16:51:50 +0800206RTC_EXPORT std::unique_ptr<SessionDescriptionInterface>
207CreateSessionDescription(SdpType type, const std::string& sdp);
208RTC_EXPORT std::unique_ptr<SessionDescriptionInterface>
209CreateSessionDescription(SdpType type,
210 const std::string& sdp,
211 SdpParseError* error_out);
Hsin-Yu Chao47ed2092018-03-17 16:50:45 +0800212
Hsin-Yu Chaoedc7e2a2018-08-24 14:02:41 +0800213// Creates a SessionDescriptionInterface based on a parsed SDP structure and the
214// given type, ID and version.
215std::unique_ptr<SessionDescriptionInterface> CreateSessionDescription(
216 SdpType type,
217 const std::string& session_id,
218 const std::string& session_version,
219 std::unique_ptr<cricket::SessionDescription> description);
220
Hsin-Yu Chao47ed2092018-03-17 16:50:45 +0800221// CreateOffer and CreateAnswer callback interface.
Hsin-Yu Chao640d48b2018-11-09 16:51:50 +0800222class RTC_EXPORT CreateSessionDescriptionObserver
223 : public rtc::RefCountInterface {
Hsin-Yu Chao47ed2092018-03-17 16:50:45 +0800224 public:
225 // This callback transfers the ownership of the |desc|.
226 // TODO(deadbeef): Make this take an std::unique_ptr<> to avoid confusion
227 // around ownership.
228 virtual void OnSuccess(SessionDescriptionInterface* desc) = 0;
229 // The OnFailure callback takes an RTCError, which consists of an
230 // error code and a string.
231 // RTCError is non-copyable, so it must be passed using std::move.
232 // Earlier versions of the API used a string argument. This version
Hsin-Yu Chao8873da32020-11-30 07:50:42 +0000233 // is removed; its functionality was the same as passing
234 // error.message.
235 virtual void OnFailure(RTCError error) = 0;
Hsin-Yu Chao47ed2092018-03-17 16:50:45 +0800236
237 protected:
238 ~CreateSessionDescriptionObserver() override = default;
239};
240
241// SetLocalDescription and SetRemoteDescription callback interface.
Hsin-Yu Chao640d48b2018-11-09 16:51:50 +0800242class RTC_EXPORT SetSessionDescriptionObserver : public rtc::RefCountInterface {
Hsin-Yu Chao47ed2092018-03-17 16:50:45 +0800243 public:
244 virtual void OnSuccess() = 0;
245 // See description in CreateSessionDescriptionObserver for OnFailure.
Hsin-Yu Chao8873da32020-11-30 07:50:42 +0000246 virtual void OnFailure(RTCError error) = 0;
Hsin-Yu Chao47ed2092018-03-17 16:50:45 +0800247
248 protected:
249 ~SetSessionDescriptionObserver() override = default;
250};
251
252} // namespace webrtc
253
254#endif // API_JSEP_H_