xref: /aosp_15_r20/external/webrtc/api/jsep.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1*d9f75844SAndroid Build Coastguard Worker /*
2*d9f75844SAndroid Build Coastguard Worker  *  Copyright 2012 The WebRTC project authors. All Rights Reserved.
3*d9f75844SAndroid Build Coastguard Worker  *
4*d9f75844SAndroid Build Coastguard Worker  *  Use of this source code is governed by a BSD-style license
5*d9f75844SAndroid Build Coastguard Worker  *  that can be found in the LICENSE file in the root of the source
6*d9f75844SAndroid Build Coastguard Worker  *  tree. An additional intellectual property rights grant can be found
7*d9f75844SAndroid Build Coastguard Worker  *  in the file PATENTS.  All contributing project authors may
8*d9f75844SAndroid Build Coastguard Worker  *  be found in the AUTHORS file in the root of the source tree.
9*d9f75844SAndroid Build Coastguard Worker  */
10*d9f75844SAndroid Build Coastguard Worker 
11*d9f75844SAndroid Build Coastguard Worker // This file contains declarations of interfaces that wrap SDP-related
12*d9f75844SAndroid Build Coastguard Worker // constructs; session descriptions and ICE candidates. The inner "cricket::"
13*d9f75844SAndroid Build Coastguard Worker // objects shouldn't be accessed directly; the intention is that an application
14*d9f75844SAndroid Build Coastguard Worker // using the PeerConnection API only creates these objects from strings, and
15*d9f75844SAndroid Build Coastguard Worker // them passes them into the PeerConnection.
16*d9f75844SAndroid Build Coastguard Worker //
17*d9f75844SAndroid Build Coastguard Worker // Though in the future, we're planning to provide an SDP parsing API, with a
18*d9f75844SAndroid Build Coastguard Worker // structure more friendly than cricket::SessionDescription.
19*d9f75844SAndroid Build Coastguard Worker 
20*d9f75844SAndroid Build Coastguard Worker #ifndef API_JSEP_H_
21*d9f75844SAndroid Build Coastguard Worker #define API_JSEP_H_
22*d9f75844SAndroid Build Coastguard Worker 
23*d9f75844SAndroid Build Coastguard Worker #include <stddef.h>
24*d9f75844SAndroid Build Coastguard Worker 
25*d9f75844SAndroid Build Coastguard Worker #include <memory>
26*d9f75844SAndroid Build Coastguard Worker #include <string>
27*d9f75844SAndroid Build Coastguard Worker #include <vector>
28*d9f75844SAndroid Build Coastguard Worker 
29*d9f75844SAndroid Build Coastguard Worker #include "absl/types/optional.h"
30*d9f75844SAndroid Build Coastguard Worker #include "api/rtc_error.h"
31*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/ref_count.h"
32*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/system/rtc_export.h"
33*d9f75844SAndroid Build Coastguard Worker 
34*d9f75844SAndroid Build Coastguard Worker namespace cricket {
35*d9f75844SAndroid Build Coastguard Worker class Candidate;
36*d9f75844SAndroid Build Coastguard Worker class SessionDescription;
37*d9f75844SAndroid Build Coastguard Worker }  // namespace cricket
38*d9f75844SAndroid Build Coastguard Worker 
39*d9f75844SAndroid Build Coastguard Worker namespace webrtc {
40*d9f75844SAndroid Build Coastguard Worker 
41*d9f75844SAndroid Build Coastguard Worker struct SdpParseError {
42*d9f75844SAndroid Build Coastguard Worker  public:
43*d9f75844SAndroid Build Coastguard Worker   // The sdp line that causes the error.
44*d9f75844SAndroid Build Coastguard Worker   std::string line;
45*d9f75844SAndroid Build Coastguard Worker   // Explains the error.
46*d9f75844SAndroid Build Coastguard Worker   std::string description;
47*d9f75844SAndroid Build Coastguard Worker };
48*d9f75844SAndroid Build Coastguard Worker 
49*d9f75844SAndroid Build Coastguard Worker // Class representation of an ICE candidate.
50*d9f75844SAndroid Build Coastguard Worker //
51*d9f75844SAndroid Build Coastguard Worker // An instance of this interface is supposed to be owned by one class at
52*d9f75844SAndroid Build Coastguard Worker // a time and is therefore not expected to be thread safe.
53*d9f75844SAndroid Build Coastguard Worker //
54*d9f75844SAndroid Build Coastguard Worker // An instance can be created by CreateIceCandidate.
55*d9f75844SAndroid Build Coastguard Worker class RTC_EXPORT IceCandidateInterface {
56*d9f75844SAndroid Build Coastguard Worker  public:
~IceCandidateInterface()57*d9f75844SAndroid Build Coastguard Worker   virtual ~IceCandidateInterface() {}
58*d9f75844SAndroid Build Coastguard Worker   // If present, this is the value of the "a=mid" attribute of the candidate's
59*d9f75844SAndroid Build Coastguard Worker   // m= section in SDP, which identifies the m= section.
60*d9f75844SAndroid Build Coastguard Worker   virtual std::string sdp_mid() const = 0;
61*d9f75844SAndroid Build Coastguard Worker   // This indicates the index (starting at zero) of m= section this candidate
62*d9f75844SAndroid Build Coastguard Worker   // is associated with. Needed when an endpoint doesn't support MIDs.
63*d9f75844SAndroid Build Coastguard Worker   virtual int sdp_mline_index() const = 0;
64*d9f75844SAndroid Build Coastguard Worker   // Only for use internally.
65*d9f75844SAndroid Build Coastguard Worker   virtual const cricket::Candidate& candidate() const = 0;
66*d9f75844SAndroid Build Coastguard Worker   // The URL of the ICE server which this candidate was gathered from.
67*d9f75844SAndroid Build Coastguard Worker   // TODO(zhihuang): Remove the default implementation once the subclasses
68*d9f75844SAndroid Build Coastguard Worker   // implement this method.
69*d9f75844SAndroid Build Coastguard Worker   virtual std::string server_url() const;
70*d9f75844SAndroid Build Coastguard Worker   // Creates a SDP-ized form of this candidate.
71*d9f75844SAndroid Build Coastguard Worker   virtual bool ToString(std::string* out) const = 0;
72*d9f75844SAndroid Build Coastguard Worker };
73*d9f75844SAndroid Build Coastguard Worker 
74*d9f75844SAndroid Build Coastguard Worker // Creates a IceCandidateInterface based on SDP string.
75*d9f75844SAndroid Build Coastguard Worker // Returns null if the sdp string can't be parsed.
76*d9f75844SAndroid Build Coastguard Worker // `error` may be null.
77*d9f75844SAndroid Build Coastguard Worker RTC_EXPORT IceCandidateInterface* CreateIceCandidate(const std::string& sdp_mid,
78*d9f75844SAndroid Build Coastguard Worker                                                      int sdp_mline_index,
79*d9f75844SAndroid Build Coastguard Worker                                                      const std::string& sdp,
80*d9f75844SAndroid Build Coastguard Worker                                                      SdpParseError* error);
81*d9f75844SAndroid Build Coastguard Worker 
82*d9f75844SAndroid Build Coastguard Worker // Creates an IceCandidateInterface based on a parsed candidate structure.
83*d9f75844SAndroid Build Coastguard Worker RTC_EXPORT std::unique_ptr<IceCandidateInterface> CreateIceCandidate(
84*d9f75844SAndroid Build Coastguard Worker     const std::string& sdp_mid,
85*d9f75844SAndroid Build Coastguard Worker     int sdp_mline_index,
86*d9f75844SAndroid Build Coastguard Worker     const cricket::Candidate& candidate);
87*d9f75844SAndroid Build Coastguard Worker 
88*d9f75844SAndroid Build Coastguard Worker // This class represents a collection of candidates for a specific m= section.
89*d9f75844SAndroid Build Coastguard Worker // Used in SessionDescriptionInterface.
90*d9f75844SAndroid Build Coastguard Worker class IceCandidateCollection {
91*d9f75844SAndroid Build Coastguard Worker  public:
~IceCandidateCollection()92*d9f75844SAndroid Build Coastguard Worker   virtual ~IceCandidateCollection() {}
93*d9f75844SAndroid Build Coastguard Worker   virtual size_t count() const = 0;
94*d9f75844SAndroid Build Coastguard Worker   // Returns true if an equivalent `candidate` exist in the collection.
95*d9f75844SAndroid Build Coastguard Worker   virtual bool HasCandidate(const IceCandidateInterface* candidate) const = 0;
96*d9f75844SAndroid Build Coastguard Worker   virtual const IceCandidateInterface* at(size_t index) const = 0;
97*d9f75844SAndroid Build Coastguard Worker };
98*d9f75844SAndroid Build Coastguard Worker 
99*d9f75844SAndroid Build Coastguard Worker // Enum that describes the type of the SessionDescriptionInterface.
100*d9f75844SAndroid Build Coastguard Worker // Corresponds to RTCSdpType in the WebRTC specification.
101*d9f75844SAndroid Build Coastguard Worker // https://w3c.github.io/webrtc-pc/#dom-rtcsdptype
102*d9f75844SAndroid Build Coastguard Worker enum class SdpType {
103*d9f75844SAndroid Build Coastguard Worker   kOffer,     // Description must be treated as an SDP offer.
104*d9f75844SAndroid Build Coastguard Worker   kPrAnswer,  // Description must be treated as an SDP answer, but not a final
105*d9f75844SAndroid Build Coastguard Worker               // answer.
106*d9f75844SAndroid Build Coastguard Worker   kAnswer,    // Description must be treated as an SDP final answer, and the
107*d9f75844SAndroid Build Coastguard Worker               // offer-answer exchange must be considered complete after
108*d9f75844SAndroid Build Coastguard Worker               // receiving this.
109*d9f75844SAndroid Build Coastguard Worker   kRollback   // Resets any pending offers and sets signaling state back to
110*d9f75844SAndroid Build Coastguard Worker               // stable.
111*d9f75844SAndroid Build Coastguard Worker };
112*d9f75844SAndroid Build Coastguard Worker 
113*d9f75844SAndroid Build Coastguard Worker // Returns the string form of the given SDP type. String forms are defined in
114*d9f75844SAndroid Build Coastguard Worker // SessionDescriptionInterface.
115*d9f75844SAndroid Build Coastguard Worker RTC_EXPORT const char* SdpTypeToString(SdpType type);
116*d9f75844SAndroid Build Coastguard Worker 
117*d9f75844SAndroid Build Coastguard Worker // Returns the SdpType from its string form. The string form can be one of the
118*d9f75844SAndroid Build Coastguard Worker // constants defined in SessionDescriptionInterface. Passing in any other string
119*d9f75844SAndroid Build Coastguard Worker // results in nullopt.
120*d9f75844SAndroid Build Coastguard Worker absl::optional<SdpType> SdpTypeFromString(const std::string& type_str);
121*d9f75844SAndroid Build Coastguard Worker 
122*d9f75844SAndroid Build Coastguard Worker // Class representation of an SDP session description.
123*d9f75844SAndroid Build Coastguard Worker //
124*d9f75844SAndroid Build Coastguard Worker // An instance of this interface is supposed to be owned by one class at a time
125*d9f75844SAndroid Build Coastguard Worker // and is therefore not expected to be thread safe.
126*d9f75844SAndroid Build Coastguard Worker //
127*d9f75844SAndroid Build Coastguard Worker // An instance can be created by CreateSessionDescription.
128*d9f75844SAndroid Build Coastguard Worker class RTC_EXPORT SessionDescriptionInterface {
129*d9f75844SAndroid Build Coastguard Worker  public:
130*d9f75844SAndroid Build Coastguard Worker   // String representations of the supported SDP types.
131*d9f75844SAndroid Build Coastguard Worker   static const char kOffer[];
132*d9f75844SAndroid Build Coastguard Worker   static const char kPrAnswer[];
133*d9f75844SAndroid Build Coastguard Worker   static const char kAnswer[];
134*d9f75844SAndroid Build Coastguard Worker   static const char kRollback[];
135*d9f75844SAndroid Build Coastguard Worker 
~SessionDescriptionInterface()136*d9f75844SAndroid Build Coastguard Worker   virtual ~SessionDescriptionInterface() {}
137*d9f75844SAndroid Build Coastguard Worker 
138*d9f75844SAndroid Build Coastguard Worker   // Create a new SessionDescriptionInterface object
139*d9f75844SAndroid Build Coastguard Worker   // with the same values as the old object.
140*d9f75844SAndroid Build Coastguard Worker   // TODO(bugs.webrtc.org:12215): Remove default implementation
Clone()141*d9f75844SAndroid Build Coastguard Worker   virtual std::unique_ptr<SessionDescriptionInterface> Clone() const {
142*d9f75844SAndroid Build Coastguard Worker     return nullptr;
143*d9f75844SAndroid Build Coastguard Worker   }
144*d9f75844SAndroid Build Coastguard Worker 
145*d9f75844SAndroid Build Coastguard Worker   // Only for use internally.
146*d9f75844SAndroid Build Coastguard Worker   virtual cricket::SessionDescription* description() = 0;
147*d9f75844SAndroid Build Coastguard Worker   virtual const cricket::SessionDescription* description() const = 0;
148*d9f75844SAndroid Build Coastguard Worker 
149*d9f75844SAndroid Build Coastguard Worker   // Get the session id and session version, which are defined based on
150*d9f75844SAndroid Build Coastguard Worker   // RFC 4566 for the SDP o= line.
151*d9f75844SAndroid Build Coastguard Worker   virtual std::string session_id() const = 0;
152*d9f75844SAndroid Build Coastguard Worker   virtual std::string session_version() const = 0;
153*d9f75844SAndroid Build Coastguard Worker 
154*d9f75844SAndroid Build Coastguard Worker   // Returns the type of this session description as an SdpType. Descriptions of
155*d9f75844SAndroid Build Coastguard Worker   // the various types are found in the SdpType documentation.
156*d9f75844SAndroid Build Coastguard Worker   // TODO(steveanton): Remove default implementation once Chromium has been
157*d9f75844SAndroid Build Coastguard Worker   // updated.
158*d9f75844SAndroid Build Coastguard Worker   virtual SdpType GetType() const;
159*d9f75844SAndroid Build Coastguard Worker 
160*d9f75844SAndroid Build Coastguard Worker   // kOffer/kPrAnswer/kAnswer
161*d9f75844SAndroid Build Coastguard Worker   // TODO(steveanton): Remove this in favor of `GetType` that returns SdpType.
162*d9f75844SAndroid Build Coastguard Worker   virtual std::string type() const = 0;
163*d9f75844SAndroid Build Coastguard Worker 
164*d9f75844SAndroid Build Coastguard Worker   // Adds the specified candidate to the description.
165*d9f75844SAndroid Build Coastguard Worker   //
166*d9f75844SAndroid Build Coastguard Worker   // Ownership is not transferred.
167*d9f75844SAndroid Build Coastguard Worker   //
168*d9f75844SAndroid Build Coastguard Worker   // Returns false if the session description does not have a media section
169*d9f75844SAndroid Build Coastguard Worker   // that corresponds to `candidate.sdp_mid()` or
170*d9f75844SAndroid Build Coastguard Worker   // `candidate.sdp_mline_index()`.
171*d9f75844SAndroid Build Coastguard Worker   virtual bool AddCandidate(const IceCandidateInterface* candidate) = 0;
172*d9f75844SAndroid Build Coastguard Worker 
173*d9f75844SAndroid Build Coastguard Worker   // Removes the candidates from the description, if found.
174*d9f75844SAndroid Build Coastguard Worker   //
175*d9f75844SAndroid Build Coastguard Worker   // Returns the number of candidates removed.
176*d9f75844SAndroid Build Coastguard Worker   virtual size_t RemoveCandidates(
177*d9f75844SAndroid Build Coastguard Worker       const std::vector<cricket::Candidate>& candidates);
178*d9f75844SAndroid Build Coastguard Worker 
179*d9f75844SAndroid Build Coastguard Worker   // Returns the number of m= sections in the session description.
180*d9f75844SAndroid Build Coastguard Worker   virtual size_t number_of_mediasections() const = 0;
181*d9f75844SAndroid Build Coastguard Worker 
182*d9f75844SAndroid Build Coastguard Worker   // Returns a collection of all candidates that belong to a certain m=
183*d9f75844SAndroid Build Coastguard Worker   // section.
184*d9f75844SAndroid Build Coastguard Worker   virtual const IceCandidateCollection* candidates(
185*d9f75844SAndroid Build Coastguard Worker       size_t mediasection_index) const = 0;
186*d9f75844SAndroid Build Coastguard Worker 
187*d9f75844SAndroid Build Coastguard Worker   // Serializes the description to SDP.
188*d9f75844SAndroid Build Coastguard Worker   virtual bool ToString(std::string* out) const = 0;
189*d9f75844SAndroid Build Coastguard Worker };
190*d9f75844SAndroid Build Coastguard Worker 
191*d9f75844SAndroid Build Coastguard Worker // Creates a SessionDescriptionInterface based on the SDP string and the type.
192*d9f75844SAndroid Build Coastguard Worker // Returns null if the sdp string can't be parsed or the type is unsupported.
193*d9f75844SAndroid Build Coastguard Worker // `error` may be null.
194*d9f75844SAndroid Build Coastguard Worker // TODO(steveanton): This function is deprecated. Please use the functions below
195*d9f75844SAndroid Build Coastguard Worker // which take an SdpType enum instead. Remove this once it is no longer used.
196*d9f75844SAndroid Build Coastguard Worker RTC_EXPORT SessionDescriptionInterface* CreateSessionDescription(
197*d9f75844SAndroid Build Coastguard Worker     const std::string& type,
198*d9f75844SAndroid Build Coastguard Worker     const std::string& sdp,
199*d9f75844SAndroid Build Coastguard Worker     SdpParseError* error);
200*d9f75844SAndroid Build Coastguard Worker 
201*d9f75844SAndroid Build Coastguard Worker // Creates a SessionDescriptionInterface based on the SDP string and the type.
202*d9f75844SAndroid Build Coastguard Worker // Returns null if the SDP string cannot be parsed.
203*d9f75844SAndroid Build Coastguard Worker // If using the signature with `error_out`, details of the parsing error may be
204*d9f75844SAndroid Build Coastguard Worker // written to `error_out` if it is not null.
205*d9f75844SAndroid Build Coastguard Worker RTC_EXPORT std::unique_ptr<SessionDescriptionInterface>
206*d9f75844SAndroid Build Coastguard Worker CreateSessionDescription(SdpType type, const std::string& sdp);
207*d9f75844SAndroid Build Coastguard Worker RTC_EXPORT std::unique_ptr<SessionDescriptionInterface>
208*d9f75844SAndroid Build Coastguard Worker CreateSessionDescription(SdpType type,
209*d9f75844SAndroid Build Coastguard Worker                          const std::string& sdp,
210*d9f75844SAndroid Build Coastguard Worker                          SdpParseError* error_out);
211*d9f75844SAndroid Build Coastguard Worker 
212*d9f75844SAndroid Build Coastguard Worker // Creates a SessionDescriptionInterface based on a parsed SDP structure and the
213*d9f75844SAndroid Build Coastguard Worker // given type, ID and version.
214*d9f75844SAndroid Build Coastguard Worker std::unique_ptr<SessionDescriptionInterface> CreateSessionDescription(
215*d9f75844SAndroid Build Coastguard Worker     SdpType type,
216*d9f75844SAndroid Build Coastguard Worker     const std::string& session_id,
217*d9f75844SAndroid Build Coastguard Worker     const std::string& session_version,
218*d9f75844SAndroid Build Coastguard Worker     std::unique_ptr<cricket::SessionDescription> description);
219*d9f75844SAndroid Build Coastguard Worker 
220*d9f75844SAndroid Build Coastguard Worker // CreateOffer and CreateAnswer callback interface.
221*d9f75844SAndroid Build Coastguard Worker class RTC_EXPORT CreateSessionDescriptionObserver
222*d9f75844SAndroid Build Coastguard Worker     : public rtc::RefCountInterface {
223*d9f75844SAndroid Build Coastguard Worker  public:
224*d9f75844SAndroid Build Coastguard Worker   // This callback transfers the ownership of the `desc`.
225*d9f75844SAndroid Build Coastguard Worker   // TODO(deadbeef): Make this take an std::unique_ptr<> to avoid confusion
226*d9f75844SAndroid Build Coastguard Worker   // around ownership.
227*d9f75844SAndroid Build Coastguard Worker   virtual void OnSuccess(SessionDescriptionInterface* desc) = 0;
228*d9f75844SAndroid Build Coastguard Worker   // The OnFailure callback takes an RTCError, which consists of an
229*d9f75844SAndroid Build Coastguard Worker   // error code and a string.
230*d9f75844SAndroid Build Coastguard Worker   // RTCError is non-copyable, so it must be passed using std::move.
231*d9f75844SAndroid Build Coastguard Worker   // Earlier versions of the API used a string argument. This version
232*d9f75844SAndroid Build Coastguard Worker   // is removed; its functionality was the same as passing
233*d9f75844SAndroid Build Coastguard Worker   // error.message.
234*d9f75844SAndroid Build Coastguard Worker   virtual void OnFailure(RTCError error) = 0;
235*d9f75844SAndroid Build Coastguard Worker 
236*d9f75844SAndroid Build Coastguard Worker  protected:
237*d9f75844SAndroid Build Coastguard Worker   ~CreateSessionDescriptionObserver() override = default;
238*d9f75844SAndroid Build Coastguard Worker };
239*d9f75844SAndroid Build Coastguard Worker 
240*d9f75844SAndroid Build Coastguard Worker // SetLocalDescription and SetRemoteDescription callback interface.
241*d9f75844SAndroid Build Coastguard Worker class RTC_EXPORT SetSessionDescriptionObserver : public rtc::RefCountInterface {
242*d9f75844SAndroid Build Coastguard Worker  public:
243*d9f75844SAndroid Build Coastguard Worker   virtual void OnSuccess() = 0;
244*d9f75844SAndroid Build Coastguard Worker   // See description in CreateSessionDescriptionObserver for OnFailure.
245*d9f75844SAndroid Build Coastguard Worker   virtual void OnFailure(RTCError error) = 0;
246*d9f75844SAndroid Build Coastguard Worker 
247*d9f75844SAndroid Build Coastguard Worker  protected:
248*d9f75844SAndroid Build Coastguard Worker   ~SetSessionDescriptionObserver() override = default;
249*d9f75844SAndroid Build Coastguard Worker };
250*d9f75844SAndroid Build Coastguard Worker 
251*d9f75844SAndroid Build Coastguard Worker }  // namespace webrtc
252*d9f75844SAndroid Build Coastguard Worker 
253*d9f75844SAndroid Build Coastguard Worker #endif  // API_JSEP_H_
254