blob: 86f4162f84efafeeaf8d62d86cdacd85bedf1dc7 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:361/*
kjellanderb24317b2016-02-10 15:54:432 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:363 *
kjellanderb24317b2016-02-10 15:54:434 * 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.org28e20752013-07-10 00:45:369 */
10
deadbeefb10f32f2017-02-08 09:38:2111// 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.org28e20752013-07-10 00:45:3619
Mirko Bonadei92ea95e2017-09-15 04:47:3120#ifndef API_JSEP_H_
21#define API_JSEP_H_
henrike@webrtc.org28e20752013-07-10 00:45:3622
pbos9baddf22017-01-02 14:44:4123#include <stddef.h>
24
Steve Anton88f2cb92017-12-05 20:47:3225#include <memory>
henrike@webrtc.org28e20752013-07-10 00:45:3626#include <string>
27#include <vector>
28
Danil Chapovalov0bc58cf2018-06-21 11:32:5629#include "absl/types/optional.h"
Steve Anton10542f22019-01-11 17:11:0030#include "api/rtc_error.h"
31#include "rtc_base/ref_count.h"
Mirko Bonadei3b56ee72018-10-15 15:15:1232#include "rtc_base/system/rtc_export.h"
henrike@webrtc.org28e20752013-07-10 00:45:3633
34namespace cricket {
tommi6f59a4f2016-03-11 22:05:0935class Candidate;
Honghai Zhang7fb69db2016-03-14 18:59:1836class SessionDescription;
henrike@webrtc.org28e20752013-07-10 00:45:3637} // 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.
deadbeefb10f32f2017-02-08 09:38:2150//
henrike@webrtc.org28e20752013-07-10 00:45:3651// 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.
deadbeefb10f32f2017-02-08 09:38:2153//
54// An instance can be created by CreateIceCandidate.
Mirko Bonadei35214fc2019-09-23 12:54:2855class RTC_EXPORT IceCandidateInterface {
henrike@webrtc.org28e20752013-07-10 00:45:3656 public:
57 virtual ~IceCandidateInterface() {}
deadbeefb10f32f2017-02-08 09:38:2158 // 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.org28e20752013-07-10 00:45:3660 virtual std::string sdp_mid() const = 0;
deadbeefb10f32f2017-02-08 09:38:2161 // This indicates the index (starting at zero) of m= section this candidate
Steve Anton88f2cb92017-12-05 20:47:3262 // is associated with. Needed when an endpoint doesn't support MIDs.
henrike@webrtc.org28e20752013-07-10 00:45:3663 virtual int sdp_mline_index() const = 0;
deadbeefb10f32f2017-02-08 09:38:2164 // Only for use internally.
henrike@webrtc.org28e20752013-07-10 00:45:3665 virtual const cricket::Candidate& candidate() const = 0;
zhihuangd7e771d2017-02-16 19:29:3966 // 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 Anton845bb732017-12-05 20:50:2669 virtual std::string server_url() const;
henrike@webrtc.org28e20752013-07-10 00:45:3670 // 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.
deadbeef8d60a942017-02-27 22:47:3375// Returns null if the sdp string can't be parsed.
76// |error| may be null.
Mirko Bonadei3b56ee72018-10-15 15:15:1277RTC_EXPORT IceCandidateInterface* CreateIceCandidate(const std::string& sdp_mid,
78 int sdp_mline_index,
79 const std::string& sdp,
80 SdpParseError* error);
henrike@webrtc.org28e20752013-07-10 00:45:3681
Steve Anton27ab0e52018-07-23 22:11:5382// Creates an IceCandidateInterface based on a parsed candidate structure.
Mirko Bonadei3b56ee72018-10-15 15:15:1283RTC_EXPORT std::unique_ptr<IceCandidateInterface> CreateIceCandidate(
Steve Anton27ab0e52018-07-23 22:11:5384 const std::string& sdp_mid,
85 int sdp_mline_index,
86 const cricket::Candidate& candidate);
87
deadbeefb10f32f2017-02-08 09:38:2188// This class represents a collection of candidates for a specific m= section.
89// Used in SessionDescriptionInterface.
henrike@webrtc.org28e20752013-07-10 00:45:3690class 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
Steve Anton88f2cb92017-12-05 20:47:3299// 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.
Eldar Rello5ab79e62019-10-09 15:29:44106 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 Anton88f2cb92017-12-05 20:47:32111};
112
113// Returns the string form of the given SDP type. String forms are defined in
114// SessionDescriptionInterface.
Mirko Bonadei3ac63752019-11-05 08:56:32115RTC_EXPORT const char* SdpTypeToString(SdpType type);
Steve Anton88f2cb92017-12-05 20:47:32116
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 Chapovalov0bc58cf2018-06-21 11:32:56120absl::optional<SdpType> SdpTypeFromString(const std::string& type_str);
Steve Anton88f2cb92017-12-05 20:47:32121
deadbeefb10f32f2017-02-08 09:38:21122// 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 Bonadei3b56ee72018-10-15 15:15:12128class RTC_EXPORT SessionDescriptionInterface {
henrike@webrtc.org28e20752013-07-10 00:45:36129 public:
Steve Anton88f2cb92017-12-05 20:47:32130 // String representations of the supported SDP types.
henrike@webrtc.org28e20752013-07-10 00:45:36131 static const char kOffer[];
132 static const char kPrAnswer[];
133 static const char kAnswer[];
Eldar Rello5ab79e62019-10-09 15:29:44134 static const char kRollback[];
henrike@webrtc.org28e20752013-07-10 00:45:36135
136 virtual ~SessionDescriptionInterface() {}
deadbeefb10f32f2017-02-08 09:38:21137
138 // Only for use internally.
henrike@webrtc.org28e20752013-07-10 00:45:36139 virtual cricket::SessionDescription* description() = 0;
140 virtual const cricket::SessionDescription* description() const = 0;
deadbeefb10f32f2017-02-08 09:38:21141
henrike@webrtc.org28e20752013-07-10 00:45:36142 // Get the session id and session version, which are defined based on
143 // RFC 4566 for the SDP o= line.
144 virtual std::string session_id() const = 0;
145 virtual std::string session_version() const = 0;
deadbeefb10f32f2017-02-08 09:38:21146
Steve Anton88f2cb92017-12-05 20:47:32147 // Returns the type of this session description as an SdpType. Descriptions of
148 // the various types are found in the SdpType documentation.
149 // TODO(steveanton): Remove default implementation once Chromium has been
150 // updated.
151 virtual SdpType GetType() const;
152
deadbeefb10f32f2017-02-08 09:38:21153 // kOffer/kPrAnswer/kAnswer
Steve Anton88f2cb92017-12-05 20:47:32154 // TODO(steveanton): Remove this in favor of |GetType| that returns SdpType.
henrike@webrtc.org28e20752013-07-10 00:45:36155 virtual std::string type() const = 0;
deadbeefb10f32f2017-02-08 09:38:21156
henrike@webrtc.org28e20752013-07-10 00:45:36157 // Adds the specified candidate to the description.
deadbeefb10f32f2017-02-08 09:38:21158 //
henrike@webrtc.org28e20752013-07-10 00:45:36159 // Ownership is not transferred.
deadbeefb10f32f2017-02-08 09:38:21160 //
161 // Returns false if the session description does not have a media section
162 // that corresponds to |candidate.sdp_mid()| or
163 // |candidate.sdp_mline_index()|.
henrike@webrtc.org28e20752013-07-10 00:45:36164 virtual bool AddCandidate(const IceCandidateInterface* candidate) = 0;
deadbeefb10f32f2017-02-08 09:38:21165
166 // Removes the candidates from the description, if found.
167 //
Honghai Zhang7fb69db2016-03-14 18:59:18168 // Returns the number of candidates removed.
169 virtual size_t RemoveCandidates(
Steve Anton845bb732017-12-05 20:50:26170 const std::vector<cricket::Candidate>& candidates);
Honghai Zhang7fb69db2016-03-14 18:59:18171
deadbeefb10f32f2017-02-08 09:38:21172 // Returns the number of m= sections in the session description.
henrike@webrtc.org28e20752013-07-10 00:45:36173 virtual size_t number_of_mediasections() const = 0;
deadbeefb10f32f2017-02-08 09:38:21174
175 // Returns a collection of all candidates that belong to a certain m=
176 // section.
henrike@webrtc.org28e20752013-07-10 00:45:36177 virtual const IceCandidateCollection* candidates(
178 size_t mediasection_index) const = 0;
deadbeefb10f32f2017-02-08 09:38:21179
henrike@webrtc.org28e20752013-07-10 00:45:36180 // Serializes the description to SDP.
181 virtual bool ToString(std::string* out) const = 0;
182};
183
deadbeefb10f32f2017-02-08 09:38:21184// Creates a SessionDescriptionInterface based on the SDP string and the type.
deadbeef8d60a942017-02-27 22:47:33185// Returns null if the sdp string can't be parsed or the type is unsupported.
186// |error| may be null.
Steve Anton88f2cb92017-12-05 20:47:32187// TODO(steveanton): This function is deprecated. Please use the functions below
188// which take an SdpType enum instead. Remove this once it is no longer used.
Mirko Bonadei3b56ee72018-10-15 15:15:12189RTC_EXPORT SessionDescriptionInterface* CreateSessionDescription(
190 const std::string& type,
191 const std::string& sdp,
192 SdpParseError* error);
henrike@webrtc.org28e20752013-07-10 00:45:36193
Steve Anton88f2cb92017-12-05 20:47:32194// Creates a SessionDescriptionInterface based on the SDP string and the type.
195// Returns null if the SDP string cannot be parsed.
196// If using the signature with |error_out|, details of the parsing error may be
197// written to |error_out| if it is not null.
Mirko Bonadei3b56ee72018-10-15 15:15:12198RTC_EXPORT std::unique_ptr<SessionDescriptionInterface>
199CreateSessionDescription(SdpType type, const std::string& sdp);
200RTC_EXPORT std::unique_ptr<SessionDescriptionInterface>
201CreateSessionDescription(SdpType type,
202 const std::string& sdp,
203 SdpParseError* error_out);
Steve Anton88f2cb92017-12-05 20:47:32204
Steve Antond9e4a062018-07-25 01:23:33205// Creates a SessionDescriptionInterface based on a parsed SDP structure and the
206// given type, ID and version.
207std::unique_ptr<SessionDescriptionInterface> CreateSessionDescription(
208 SdpType type,
209 const std::string& session_id,
210 const std::string& session_version,
211 std::unique_ptr<cricket::SessionDescription> description);
212
deadbeefb10f32f2017-02-08 09:38:21213// CreateOffer and CreateAnswer callback interface.
Mirko Bonadeiac194142018-10-22 15:08:37214class RTC_EXPORT CreateSessionDescriptionObserver
215 : public rtc::RefCountInterface {
henrike@webrtc.org28e20752013-07-10 00:45:36216 public:
deadbeefb10f32f2017-02-08 09:38:21217 // This callback transfers the ownership of the |desc|.
218 // TODO(deadbeef): Make this take an std::unique_ptr<> to avoid confusion
219 // around ownership.
henrike@webrtc.org28e20752013-07-10 00:45:36220 virtual void OnSuccess(SessionDescriptionInterface* desc) = 0;
Harald Alvestrand5081c0c2018-03-09 14:18:03221 // The OnFailure callback takes an RTCError, which consists of an
222 // error code and a string.
223 // RTCError is non-copyable, so it must be passed using std::move.
224 // Earlier versions of the API used a string argument. This version
225 // is deprecated; in order to let clients remove the old version, it has a
226 // default implementation. If both versions are unimplemented, the
227 // result will be a runtime error (stack overflow). This is intentional.
Mirko Bonadei79eb4dd2018-07-19 08:39:30228 virtual void OnFailure(RTCError error);
229 virtual void OnFailure(const std::string& error);
henrike@webrtc.org28e20752013-07-10 00:45:36230
231 protected:
Steve Anton845bb732017-12-05 20:50:26232 ~CreateSessionDescriptionObserver() override = default;
henrike@webrtc.org28e20752013-07-10 00:45:36233};
234
deadbeefb10f32f2017-02-08 09:38:21235// SetLocalDescription and SetRemoteDescription callback interface.
Mirko Bonadeiac194142018-10-22 15:08:37236class RTC_EXPORT SetSessionDescriptionObserver : public rtc::RefCountInterface {
henrike@webrtc.org28e20752013-07-10 00:45:36237 public:
238 virtual void OnSuccess() = 0;
Harald Alvestrand5081c0c2018-03-09 14:18:03239 // See description in CreateSessionDescriptionObserver for OnFailure.
Mirko Bonadei79eb4dd2018-07-19 08:39:30240 virtual void OnFailure(RTCError error);
241
242 virtual void OnFailure(const std::string& error);
henrike@webrtc.org28e20752013-07-10 00:45:36243
244 protected:
Steve Anton845bb732017-12-05 20:50:26245 ~SetSessionDescriptionObserver() override = default;
henrike@webrtc.org28e20752013-07-10 00:45:36246};
247
248} // namespace webrtc
249
Mirko Bonadei92ea95e2017-09-15 04:47:31250#endif // API_JSEP_H_