blob: acfffd4d0a7edaf837cca1536057fa7c3a52ce8a [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
henrike@webrtc.org28e20752013-07-10 00:45:36 +000025#include <string>
26#include <vector>
27
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020028#include "rtc_base/refcount.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000029
30namespace cricket {
tommi6f59a4f2016-03-11 14:05:09 -080031class Candidate;
Honghai Zhang7fb69db2016-03-14 11:59:18 -070032class SessionDescription;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000033} // namespace cricket
34
35namespace webrtc {
36
37struct SdpParseError {
38 public:
39 // The sdp line that causes the error.
40 std::string line;
41 // Explains the error.
42 std::string description;
43};
44
45// Class representation of an ICE candidate.
deadbeefb10f32f2017-02-08 01:38:21 -080046//
henrike@webrtc.org28e20752013-07-10 00:45:36 +000047// An instance of this interface is supposed to be owned by one class at
48// a time and is therefore not expected to be thread safe.
deadbeefb10f32f2017-02-08 01:38:21 -080049//
50// An instance can be created by CreateIceCandidate.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000051class IceCandidateInterface {
52 public:
53 virtual ~IceCandidateInterface() {}
deadbeefb10f32f2017-02-08 01:38:21 -080054 // If present, this is the value of the "a=mid" attribute of the candidate's
55 // m= section in SDP, which identifies the m= section.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000056 virtual std::string sdp_mid() const = 0;
deadbeefb10f32f2017-02-08 01:38:21 -080057 // This indicates the index (starting at zero) of m= section this candidate
58 // is assocated with. Needed when an endpoint doesn't support MIDs.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000059 virtual int sdp_mline_index() const = 0;
deadbeefb10f32f2017-02-08 01:38:21 -080060 // Only for use internally.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000061 virtual const cricket::Candidate& candidate() const = 0;
zhihuangd7e771d2017-02-16 11:29:39 -080062 // The URL of the ICE server which this candidate was gathered from.
63 // TODO(zhihuang): Remove the default implementation once the subclasses
64 // implement this method.
65 virtual std::string server_url() const { return ""; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000066 // Creates a SDP-ized form of this candidate.
67 virtual bool ToString(std::string* out) const = 0;
68};
69
70// Creates a IceCandidateInterface based on SDP string.
deadbeef8d60a942017-02-27 14:47:33 -080071// Returns null if the sdp string can't be parsed.
72// |error| may be null.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000073IceCandidateInterface* CreateIceCandidate(const std::string& sdp_mid,
74 int sdp_mline_index,
75 const std::string& sdp,
76 SdpParseError* error);
77
deadbeefb10f32f2017-02-08 01:38:21 -080078// This class represents a collection of candidates for a specific m= section.
79// Used in SessionDescriptionInterface.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000080class IceCandidateCollection {
81 public:
82 virtual ~IceCandidateCollection() {}
83 virtual size_t count() const = 0;
84 // Returns true if an equivalent |candidate| exist in the collection.
85 virtual bool HasCandidate(const IceCandidateInterface* candidate) const = 0;
86 virtual const IceCandidateInterface* at(size_t index) const = 0;
87};
88
deadbeefb10f32f2017-02-08 01:38:21 -080089// Class representation of an SDP session description.
90//
91// An instance of this interface is supposed to be owned by one class at a time
92// and is therefore not expected to be thread safe.
93//
94// An instance can be created by CreateSessionDescription.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000095class SessionDescriptionInterface {
96 public:
97 // Supported types:
98 static const char kOffer[];
99 static const char kPrAnswer[];
100 static const char kAnswer[];
101
102 virtual ~SessionDescriptionInterface() {}
deadbeefb10f32f2017-02-08 01:38:21 -0800103
104 // Only for use internally.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000105 virtual cricket::SessionDescription* description() = 0;
106 virtual const cricket::SessionDescription* description() const = 0;
deadbeefb10f32f2017-02-08 01:38:21 -0800107
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000108 // Get the session id and session version, which are defined based on
109 // RFC 4566 for the SDP o= line.
110 virtual std::string session_id() const = 0;
111 virtual std::string session_version() const = 0;
deadbeefb10f32f2017-02-08 01:38:21 -0800112
113 // kOffer/kPrAnswer/kAnswer
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000114 virtual std::string type() const = 0;
deadbeefb10f32f2017-02-08 01:38:21 -0800115
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000116 // Adds the specified candidate to the description.
deadbeefb10f32f2017-02-08 01:38:21 -0800117 //
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000118 // Ownership is not transferred.
deadbeefb10f32f2017-02-08 01:38:21 -0800119 //
120 // Returns false if the session description does not have a media section
121 // that corresponds to |candidate.sdp_mid()| or
122 // |candidate.sdp_mline_index()|.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000123 virtual bool AddCandidate(const IceCandidateInterface* candidate) = 0;
deadbeefb10f32f2017-02-08 01:38:21 -0800124
125 // Removes the candidates from the description, if found.
126 //
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700127 // Returns the number of candidates removed.
128 virtual size_t RemoveCandidates(
129 const std::vector<cricket::Candidate>& candidates) { return 0; }
130
deadbeefb10f32f2017-02-08 01:38:21 -0800131 // Returns the number of m= sections in the session description.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000132 virtual size_t number_of_mediasections() const = 0;
deadbeefb10f32f2017-02-08 01:38:21 -0800133
134 // Returns a collection of all candidates that belong to a certain m=
135 // section.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000136 virtual const IceCandidateCollection* candidates(
137 size_t mediasection_index) const = 0;
deadbeefb10f32f2017-02-08 01:38:21 -0800138
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000139 // Serializes the description to SDP.
140 virtual bool ToString(std::string* out) const = 0;
141};
142
deadbeefb10f32f2017-02-08 01:38:21 -0800143// Creates a SessionDescriptionInterface based on the SDP string and the type.
deadbeef8d60a942017-02-27 14:47:33 -0800144// Returns null if the sdp string can't be parsed or the type is unsupported.
145// |error| may be null.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000146SessionDescriptionInterface* CreateSessionDescription(const std::string& type,
147 const std::string& sdp,
148 SdpParseError* error);
149
deadbeefb10f32f2017-02-08 01:38:21 -0800150// CreateOffer and CreateAnswer callback interface.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000151class CreateSessionDescriptionObserver : public rtc::RefCountInterface {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000152 public:
deadbeefb10f32f2017-02-08 01:38:21 -0800153 // This callback transfers the ownership of the |desc|.
154 // TODO(deadbeef): Make this take an std::unique_ptr<> to avoid confusion
155 // around ownership.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000156 virtual void OnSuccess(SessionDescriptionInterface* desc) = 0;
157 virtual void OnFailure(const std::string& error) = 0;
158
159 protected:
160 ~CreateSessionDescriptionObserver() {}
161};
162
deadbeefb10f32f2017-02-08 01:38:21 -0800163// SetLocalDescription and SetRemoteDescription callback interface.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000164class SetSessionDescriptionObserver : public rtc::RefCountInterface {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000165 public:
166 virtual void OnSuccess() = 0;
167 virtual void OnFailure(const std::string& error) = 0;
168
169 protected:
170 ~SetSessionDescriptionObserver() {}
171};
172
173} // namespace webrtc
174
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200175#endif // API_JSEP_H_