blob: 6da782748d4be2175de026e6f9903ac1dd23f873 [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"
31#include "rtc_base/ref_count.h"
Hsin-Yu Chao640d48b2018-11-09 16:51:50 +080032#include "rtc_base/system/rtc_export.h"
Hsin-Yu Chao47ed2092018-03-17 16:50:45 +080033
34namespace cricket {
35class Candidate;
36class SessionDescription;
37} // namespace cricket
38
39namespace webrtc {
40
41struct 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.
50//
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.
53//
54// An instance can be created by CreateIceCandidate.
55class IceCandidateInterface {
56 public:
57 virtual ~IceCandidateInterface() {}
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.
60 virtual std::string sdp_mid() const = 0;
61 // This indicates the index (starting at zero) of m= section this candidate
62 // is associated with. Needed when an endpoint doesn't support MIDs.
63 virtual int sdp_mline_index() const = 0;
64 // Only for use internally.
65 virtual const cricket::Candidate& candidate() const = 0;
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.
69 virtual std::string server_url() const;
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.
75// Returns null if the sdp string can't be parsed.
76// |error| may be null.
Hsin-Yu Chao640d48b2018-11-09 16:51:50 +080077RTC_EXPORT IceCandidateInterface* CreateIceCandidate(const std::string& sdp_mid,
78 int sdp_mline_index,
79 const std::string& sdp,
80 SdpParseError* error);
Hsin-Yu Chao47ed2092018-03-17 16:50:45 +080081
Hsin-Yu Chaoedc7e2a2018-08-24 14:02:41 +080082// Creates an IceCandidateInterface based on a parsed candidate structure.
Hsin-Yu Chao640d48b2018-11-09 16:51:50 +080083RTC_EXPORT std::unique_ptr<IceCandidateInterface> CreateIceCandidate(
Hsin-Yu Chaoedc7e2a2018-08-24 14:02:41 +080084 const std::string& sdp_mid,
85 int sdp_mline_index,
86 const cricket::Candidate& candidate);
87
Hsin-Yu Chao47ed2092018-03-17 16:50:45 +080088// This class represents a collection of candidates for a specific m= section.
89// Used in SessionDescriptionInterface.
90class IceCandidateCollection {
91 public:
92 virtual ~IceCandidateCollection() {}
93 virtual size_t count() const = 0;
94 // Returns true if an equivalent |candidate| exist in the collection.
95 virtual bool HasCandidate(const IceCandidateInterface* candidate) const = 0;
96 virtual const IceCandidateInterface* at(size_t index) const = 0;
97};
98
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
102enum 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.
106 kAnswer // Description must be treated as an SDP final answer, and the offer-
107 // answer exchange must be considered complete after receiving this.
108};
109
110// Returns the string form of the given SDP type. String forms are defined in
111// SessionDescriptionInterface.
112const char* SdpTypeToString(SdpType type);
113
114// Returns the SdpType from its string form. The string form can be one of the
115// constants defined in SessionDescriptionInterface. Passing in any other string
116// results in nullopt.
Hsin-Yu Chaoedc34c82018-08-09 17:53:05 +0800117absl::optional<SdpType> SdpTypeFromString(const std::string& type_str);
Hsin-Yu Chao47ed2092018-03-17 16:50:45 +0800118
119// Class representation of an SDP session description.
120//
121// An instance of this interface is supposed to be owned by one class at a time
122// and is therefore not expected to be thread safe.
123//
124// An instance can be created by CreateSessionDescription.
Hsin-Yu Chao640d48b2018-11-09 16:51:50 +0800125class RTC_EXPORT SessionDescriptionInterface {
Hsin-Yu Chao47ed2092018-03-17 16:50:45 +0800126 public:
127 // String representations of the supported SDP types.
128 static const char kOffer[];
129 static const char kPrAnswer[];
130 static const char kAnswer[];
131
132 virtual ~SessionDescriptionInterface() {}
133
134 // Only for use internally.
135 virtual cricket::SessionDescription* description() = 0;
136 virtual const cricket::SessionDescription* description() const = 0;
137
138 // Get the session id and session version, which are defined based on
139 // RFC 4566 for the SDP o= line.
140 virtual std::string session_id() const = 0;
141 virtual std::string session_version() const = 0;
142
143 // Returns the type of this session description as an SdpType. Descriptions of
144 // the various types are found in the SdpType documentation.
145 // TODO(steveanton): Remove default implementation once Chromium has been
146 // updated.
147 virtual SdpType GetType() const;
148
149 // kOffer/kPrAnswer/kAnswer
150 // TODO(steveanton): Remove this in favor of |GetType| that returns SdpType.
151 virtual std::string type() const = 0;
152
153 // Adds the specified candidate to the description.
154 //
155 // Ownership is not transferred.
156 //
157 // Returns false if the session description does not have a media section
158 // that corresponds to |candidate.sdp_mid()| or
159 // |candidate.sdp_mline_index()|.
160 virtual bool AddCandidate(const IceCandidateInterface* candidate) = 0;
161
162 // Removes the candidates from the description, if found.
163 //
164 // Returns the number of candidates removed.
165 virtual size_t RemoveCandidates(
166 const std::vector<cricket::Candidate>& candidates);
167
168 // Returns the number of m= sections in the session description.
169 virtual size_t number_of_mediasections() const = 0;
170
171 // Returns a collection of all candidates that belong to a certain m=
172 // section.
173 virtual const IceCandidateCollection* candidates(
174 size_t mediasection_index) const = 0;
175
176 // Serializes the description to SDP.
177 virtual bool ToString(std::string* out) const = 0;
178};
179
180// Creates a SessionDescriptionInterface based on the SDP string and the type.
181// Returns null if the sdp string can't be parsed or the type is unsupported.
182// |error| may be null.
183// TODO(steveanton): This function is deprecated. Please use the functions below
184// which take an SdpType enum instead. Remove this once it is no longer used.
Hsin-Yu Chao640d48b2018-11-09 16:51:50 +0800185RTC_EXPORT SessionDescriptionInterface* CreateSessionDescription(
186 const std::string& type,
187 const std::string& sdp,
188 SdpParseError* error);
Hsin-Yu Chao47ed2092018-03-17 16:50:45 +0800189
190// Creates a SessionDescriptionInterface based on the SDP string and the type.
191// Returns null if the SDP string cannot be parsed.
192// If using the signature with |error_out|, details of the parsing error may be
193// written to |error_out| if it is not null.
Hsin-Yu Chao640d48b2018-11-09 16:51:50 +0800194RTC_EXPORT std::unique_ptr<SessionDescriptionInterface>
195CreateSessionDescription(SdpType type, const std::string& sdp);
196RTC_EXPORT std::unique_ptr<SessionDescriptionInterface>
197CreateSessionDescription(SdpType type,
198 const std::string& sdp,
199 SdpParseError* error_out);
Hsin-Yu Chao47ed2092018-03-17 16:50:45 +0800200
Hsin-Yu Chaoedc7e2a2018-08-24 14:02:41 +0800201// Creates a SessionDescriptionInterface based on a parsed SDP structure and the
202// given type, ID and version.
203std::unique_ptr<SessionDescriptionInterface> CreateSessionDescription(
204 SdpType type,
205 const std::string& session_id,
206 const std::string& session_version,
207 std::unique_ptr<cricket::SessionDescription> description);
208
Hsin-Yu Chao47ed2092018-03-17 16:50:45 +0800209// CreateOffer and CreateAnswer callback interface.
Hsin-Yu Chao640d48b2018-11-09 16:51:50 +0800210class RTC_EXPORT CreateSessionDescriptionObserver
211 : public rtc::RefCountInterface {
Hsin-Yu Chao47ed2092018-03-17 16:50:45 +0800212 public:
213 // This callback transfers the ownership of the |desc|.
214 // TODO(deadbeef): Make this take an std::unique_ptr<> to avoid confusion
215 // around ownership.
216 virtual void OnSuccess(SessionDescriptionInterface* desc) = 0;
217 // The OnFailure callback takes an RTCError, which consists of an
218 // error code and a string.
219 // RTCError is non-copyable, so it must be passed using std::move.
220 // Earlier versions of the API used a string argument. This version
221 // is deprecated; in order to let clients remove the old version, it has a
222 // default implementation. If both versions are unimplemented, the
223 // result will be a runtime error (stack overflow). This is intentional.
Hsin-Yu Chaoedc7e2a2018-08-24 14:02:41 +0800224 virtual void OnFailure(RTCError error);
225 virtual void OnFailure(const std::string& error);
Hsin-Yu Chao47ed2092018-03-17 16:50:45 +0800226
227 protected:
228 ~CreateSessionDescriptionObserver() override = default;
229};
230
231// SetLocalDescription and SetRemoteDescription callback interface.
Hsin-Yu Chao640d48b2018-11-09 16:51:50 +0800232class RTC_EXPORT SetSessionDescriptionObserver : public rtc::RefCountInterface {
Hsin-Yu Chao47ed2092018-03-17 16:50:45 +0800233 public:
234 virtual void OnSuccess() = 0;
235 // See description in CreateSessionDescriptionObserver for OnFailure.
Hsin-Yu Chaoedc7e2a2018-08-24 14:02:41 +0800236 virtual void OnFailure(RTCError error);
237
238 virtual void OnFailure(const std::string& error);
Hsin-Yu Chao47ed2092018-03-17 16:50:45 +0800239
240 protected:
241 ~SetSessionDescriptionObserver() override = default;
242};
243
244} // namespace webrtc
245
246#endif // API_JSEP_H_