blob: cbc0a6c6bf5614a56dc2910c3a0b8599b073a73d [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * 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:36 +00009 */
10
deadbeefb10f32f2017-02-08 01:38:21 -080011// 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:36 +000019
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#ifndef API_JSEP_H_
21#define API_JSEP_H_
henrike@webrtc.org28e20752013-07-10 00:45:36 +000022
pbos9baddf22017-01-02 06:44:41 -080023#include <stddef.h>
24
Steve Anton88f2cb92017-12-05 12:47:32 -080025#include <memory>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000026#include <string>
27#include <vector>
28
Danil Chapovalov0bc58cf2018-06-21 13:32:56 +020029#include "absl/types/optional.h"
Harald Alvestrand5081c0c2018-03-09 15:18:03 +010030#include "api/rtcerror.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020031#include "rtc_base/refcount.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000032
33namespace cricket {
tommi6f59a4f2016-03-11 14:05:09 -080034class Candidate;
Honghai Zhang7fb69db2016-03-14 11:59:18 -070035class SessionDescription;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000036} // namespace cricket
37
38namespace webrtc {
39
40struct SdpParseError {
41 public:
42 // The sdp line that causes the error.
43 std::string line;
44 // Explains the error.
45 std::string description;
46};
47
48// Class representation of an ICE candidate.
deadbeefb10f32f2017-02-08 01:38:21 -080049//
henrike@webrtc.org28e20752013-07-10 00:45:36 +000050// An instance of this interface is supposed to be owned by one class at
51// a time and is therefore not expected to be thread safe.
deadbeefb10f32f2017-02-08 01:38:21 -080052//
53// An instance can be created by CreateIceCandidate.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000054class IceCandidateInterface {
55 public:
56 virtual ~IceCandidateInterface() {}
deadbeefb10f32f2017-02-08 01:38:21 -080057 // If present, this is the value of the "a=mid" attribute of the candidate's
58 // m= section in SDP, which identifies the m= section.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000059 virtual std::string sdp_mid() const = 0;
deadbeefb10f32f2017-02-08 01:38:21 -080060 // This indicates the index (starting at zero) of m= section this candidate
Steve Anton88f2cb92017-12-05 12:47:32 -080061 // is associated with. Needed when an endpoint doesn't support MIDs.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000062 virtual int sdp_mline_index() const = 0;
deadbeefb10f32f2017-02-08 01:38:21 -080063 // Only for use internally.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000064 virtual const cricket::Candidate& candidate() const = 0;
zhihuangd7e771d2017-02-16 11:29:39 -080065 // The URL of the ICE server which this candidate was gathered from.
66 // TODO(zhihuang): Remove the default implementation once the subclasses
67 // implement this method.
Steve Anton845bb732017-12-05 12:50:26 -080068 virtual std::string server_url() const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000069 // Creates a SDP-ized form of this candidate.
70 virtual bool ToString(std::string* out) const = 0;
71};
72
73// Creates a IceCandidateInterface based on SDP string.
deadbeef8d60a942017-02-27 14:47:33 -080074// Returns null if the sdp string can't be parsed.
75// |error| may be null.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000076IceCandidateInterface* CreateIceCandidate(const std::string& sdp_mid,
77 int sdp_mline_index,
78 const std::string& sdp,
79 SdpParseError* error);
80
Steve Anton27ab0e52018-07-23 15:11:53 -070081// Creates an IceCandidateInterface based on a parsed candidate structure.
82std::unique_ptr<IceCandidateInterface> CreateIceCandidate(
83 const std::string& sdp_mid,
84 int sdp_mline_index,
85 const cricket::Candidate& candidate);
86
deadbeefb10f32f2017-02-08 01:38:21 -080087// This class represents a collection of candidates for a specific m= section.
88// Used in SessionDescriptionInterface.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000089class IceCandidateCollection {
90 public:
91 virtual ~IceCandidateCollection() {}
92 virtual size_t count() const = 0;
93 // Returns true if an equivalent |candidate| exist in the collection.
94 virtual bool HasCandidate(const IceCandidateInterface* candidate) const = 0;
95 virtual const IceCandidateInterface* at(size_t index) const = 0;
96};
97
Steve Anton88f2cb92017-12-05 12:47:32 -080098// Enum that describes the type of the SessionDescriptionInterface.
99// Corresponds to RTCSdpType in the WebRTC specification.
100// https://w3c.github.io/webrtc-pc/#dom-rtcsdptype
101enum class SdpType {
102 kOffer, // Description must be treated as an SDP offer.
103 kPrAnswer, // Description must be treated as an SDP answer, but not a final
104 // answer.
105 kAnswer // Description must be treated as an SDP final answer, and the offer-
106 // answer exchange must be considered complete after receiving this.
107};
108
109// Returns the string form of the given SDP type. String forms are defined in
110// SessionDescriptionInterface.
111const char* SdpTypeToString(SdpType type);
112
113// Returns the SdpType from its string form. The string form can be one of the
114// constants defined in SessionDescriptionInterface. Passing in any other string
115// results in nullopt.
Danil Chapovalov0bc58cf2018-06-21 13:32:56 +0200116absl::optional<SdpType> SdpTypeFromString(const std::string& type_str);
Steve Anton88f2cb92017-12-05 12:47:32 -0800117
deadbeefb10f32f2017-02-08 01:38:21 -0800118// Class representation of an SDP session description.
119//
120// An instance of this interface is supposed to be owned by one class at a time
121// and is therefore not expected to be thread safe.
122//
123// An instance can be created by CreateSessionDescription.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000124class SessionDescriptionInterface {
125 public:
Steve Anton88f2cb92017-12-05 12:47:32 -0800126 // String representations of the supported SDP types.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000127 static const char kOffer[];
128 static const char kPrAnswer[];
129 static const char kAnswer[];
130
131 virtual ~SessionDescriptionInterface() {}
deadbeefb10f32f2017-02-08 01:38:21 -0800132
133 // Only for use internally.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000134 virtual cricket::SessionDescription* description() = 0;
135 virtual const cricket::SessionDescription* description() const = 0;
deadbeefb10f32f2017-02-08 01:38:21 -0800136
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000137 // Get the session id and session version, which are defined based on
138 // RFC 4566 for the SDP o= line.
139 virtual std::string session_id() const = 0;
140 virtual std::string session_version() const = 0;
deadbeefb10f32f2017-02-08 01:38:21 -0800141
Steve Anton88f2cb92017-12-05 12:47:32 -0800142 // Returns the type of this session description as an SdpType. Descriptions of
143 // the various types are found in the SdpType documentation.
144 // TODO(steveanton): Remove default implementation once Chromium has been
145 // updated.
146 virtual SdpType GetType() const;
147
deadbeefb10f32f2017-02-08 01:38:21 -0800148 // kOffer/kPrAnswer/kAnswer
Steve Anton88f2cb92017-12-05 12:47:32 -0800149 // TODO(steveanton): Remove this in favor of |GetType| that returns SdpType.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000150 virtual std::string type() const = 0;
deadbeefb10f32f2017-02-08 01:38:21 -0800151
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000152 // Adds the specified candidate to the description.
deadbeefb10f32f2017-02-08 01:38:21 -0800153 //
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000154 // Ownership is not transferred.
deadbeefb10f32f2017-02-08 01:38:21 -0800155 //
156 // Returns false if the session description does not have a media section
157 // that corresponds to |candidate.sdp_mid()| or
158 // |candidate.sdp_mline_index()|.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000159 virtual bool AddCandidate(const IceCandidateInterface* candidate) = 0;
deadbeefb10f32f2017-02-08 01:38:21 -0800160
161 // Removes the candidates from the description, if found.
162 //
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700163 // Returns the number of candidates removed.
164 virtual size_t RemoveCandidates(
Steve Anton845bb732017-12-05 12:50:26 -0800165 const std::vector<cricket::Candidate>& candidates);
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700166
deadbeefb10f32f2017-02-08 01:38:21 -0800167 // Returns the number of m= sections in the session description.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000168 virtual size_t number_of_mediasections() const = 0;
deadbeefb10f32f2017-02-08 01:38:21 -0800169
170 // Returns a collection of all candidates that belong to a certain m=
171 // section.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000172 virtual const IceCandidateCollection* candidates(
173 size_t mediasection_index) const = 0;
deadbeefb10f32f2017-02-08 01:38:21 -0800174
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000175 // Serializes the description to SDP.
176 virtual bool ToString(std::string* out) const = 0;
177};
178
deadbeefb10f32f2017-02-08 01:38:21 -0800179// Creates a SessionDescriptionInterface based on the SDP string and the type.
deadbeef8d60a942017-02-27 14:47:33 -0800180// Returns null if the sdp string can't be parsed or the type is unsupported.
181// |error| may be null.
Steve Anton88f2cb92017-12-05 12:47:32 -0800182// TODO(steveanton): This function is deprecated. Please use the functions below
183// which take an SdpType enum instead. Remove this once it is no longer used.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000184SessionDescriptionInterface* CreateSessionDescription(const std::string& type,
185 const std::string& sdp,
186 SdpParseError* error);
187
Steve Anton88f2cb92017-12-05 12:47:32 -0800188// Creates a SessionDescriptionInterface based on the SDP string and the type.
189// Returns null if the SDP string cannot be parsed.
190// If using the signature with |error_out|, details of the parsing error may be
191// written to |error_out| if it is not null.
192std::unique_ptr<SessionDescriptionInterface> CreateSessionDescription(
193 SdpType type,
194 const std::string& sdp);
195std::unique_ptr<SessionDescriptionInterface> CreateSessionDescription(
196 SdpType type,
197 const std::string& sdp,
198 SdpParseError* error_out);
199
deadbeefb10f32f2017-02-08 01:38:21 -0800200// CreateOffer and CreateAnswer callback interface.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000201class CreateSessionDescriptionObserver : public rtc::RefCountInterface {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000202 public:
deadbeefb10f32f2017-02-08 01:38:21 -0800203 // This callback transfers the ownership of the |desc|.
204 // TODO(deadbeef): Make this take an std::unique_ptr<> to avoid confusion
205 // around ownership.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000206 virtual void OnSuccess(SessionDescriptionInterface* desc) = 0;
Harald Alvestrand5081c0c2018-03-09 15:18:03 +0100207 // The OnFailure callback takes an RTCError, which consists of an
208 // error code and a string.
209 // RTCError is non-copyable, so it must be passed using std::move.
210 // Earlier versions of the API used a string argument. This version
211 // is deprecated; in order to let clients remove the old version, it has a
212 // default implementation. If both versions are unimplemented, the
213 // result will be a runtime error (stack overflow). This is intentional.
Mirko Bonadei79eb4dd2018-07-19 10:39:30 +0200214 virtual void OnFailure(RTCError error);
215 virtual void OnFailure(const std::string& error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000216
217 protected:
Steve Anton845bb732017-12-05 12:50:26 -0800218 ~CreateSessionDescriptionObserver() override = default;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000219};
220
deadbeefb10f32f2017-02-08 01:38:21 -0800221// SetLocalDescription and SetRemoteDescription callback interface.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000222class SetSessionDescriptionObserver : public rtc::RefCountInterface {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000223 public:
224 virtual void OnSuccess() = 0;
Harald Alvestrand5081c0c2018-03-09 15:18:03 +0100225 // See description in CreateSessionDescriptionObserver for OnFailure.
Mirko Bonadei79eb4dd2018-07-19 10:39:30 +0200226 virtual void OnFailure(RTCError error);
227
228 virtual void OnFailure(const std::string& error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000229
230 protected:
Steve Anton845bb732017-12-05 12:50:26 -0800231 ~SetSessionDescriptionObserver() override = default;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000232};
233
234} // namespace webrtc
235
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200236#endif // API_JSEP_H_