1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
|
/*
* Copyright 2018 The WebRTC Project Authors. All rights reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#ifndef PC_JSEP_TRANSPORT_H_
#define PC_JSEP_TRANSPORT_H_
#include <functional>
#include <memory>
#include <optional>
#include <string>
#include <vector>
#include "api/ice_transport_interface.h"
#include "api/jsep.h"
#include "api/rtc_error.h"
#include "api/scoped_refptr.h"
#include "api/sequence_checker.h"
#include "api/transport/data_channel_transport_interface.h"
#include "call/payload_type_picker.h"
#include "media/sctp/sctp_transport_internal.h"
#include "p2p/base/ice_transport_internal.h"
#include "p2p/base/transport_description.h"
#include "p2p/dtls/dtls_transport_internal.h"
#include "pc/dtls_srtp_transport.h"
#include "pc/dtls_transport.h"
#include "pc/rtcp_mux_filter.h"
#include "pc/rtp_transport.h"
#include "pc/rtp_transport_internal.h"
#include "pc/sctp_transport.h"
#include "pc/session_description.h"
#include "pc/srtp_transport.h"
#include "pc/transport_stats.h"
#include "rtc_base/rtc_certificate.h"
#include "rtc_base/ssl_fingerprint.h"
#include "rtc_base/ssl_stream_adapter.h"
#include "rtc_base/thread.h"
#include "rtc_base/thread_annotations.h"
namespace webrtc {
struct JsepTransportDescription {
public:
JsepTransportDescription();
JsepTransportDescription(
bool rtcp_mux_enabled,
const std::vector<int>& encrypted_header_extension_ids,
int rtp_abs_sendtime_extn_id,
const TransportDescription& transport_description);
JsepTransportDescription(const JsepTransportDescription& from);
~JsepTransportDescription();
JsepTransportDescription& operator=(const JsepTransportDescription& from);
bool rtcp_mux_enabled = true;
std::vector<int> encrypted_header_extension_ids;
int rtp_abs_sendtime_extn_id = -1;
// TODO(zhihuang): Add the ICE and DTLS related variables and methods from
// TransportDescription and remove this extra layer of abstraction.
TransportDescription transport_desc;
};
// Helper class used by JsepTransportController that processes
// TransportDescriptions. A TransportDescription represents the
// transport-specific properties of an SDP m= section, processed according to
// JSEP. Each transport consists of DTLS and ICE transport channels for RTP
// (and possibly RTCP, if rtcp-mux isn't used).
//
// On Threading: JsepTransport performs work solely on the network thread, and
// so its methods should only be called on the network thread.
class JsepTransport {
public:
// `mid` is just used for log statements in order to identify the Transport.
// Note that `local_certificate` is allowed to be null since a remote
// description may be set before a local certificate is generated.
JsepTransport(const std::string& mid,
const scoped_refptr<RTCCertificate>& local_certificate,
scoped_refptr<IceTransportInterface> ice_transport,
scoped_refptr<IceTransportInterface> rtcp_ice_transport,
std::unique_ptr<RtpTransport> unencrypted_rtp_transport,
std::unique_ptr<SrtpTransport> sdes_transport,
std::unique_ptr<DtlsSrtpTransport> dtls_srtp_transport,
std::unique_ptr<DtlsTransportInternal> rtp_dtls_transport,
std::unique_ptr<DtlsTransportInternal> rtcp_dtls_transport,
std::unique_ptr<SctpTransportInternal> sctp_transport,
std::function<void()> rtcp_mux_active_callback,
PayloadTypePicker& suggester);
~JsepTransport();
JsepTransport(const JsepTransport&) = delete;
JsepTransport& operator=(const JsepTransport&) = delete;
// Returns the MID of this transport. This is only used for logging.
const std::string& mid() const { return mid_; }
// Must be called before applying local session description.
// Needed in order to verify the local fingerprint.
void SetLocalCertificate(
const scoped_refptr<RTCCertificate>& local_certificate) {
RTC_DCHECK_RUN_ON(network_thread_);
local_certificate_ = local_certificate;
}
// Return the local certificate provided by SetLocalCertificate.
scoped_refptr<RTCCertificate> GetLocalCertificate() const {
RTC_DCHECK_RUN_ON(network_thread_);
return local_certificate_;
}
RTCError SetLocalJsepTransportDescription(
const JsepTransportDescription& jsep_description,
SdpType type);
// Set the remote TransportDescription to be used by DTLS and ICE channels
// that are part of this Transport.
RTCError SetRemoteJsepTransportDescription(
const JsepTransportDescription& jsep_description,
SdpType type);
RTCError AddRemoteCandidates(const Candidates& candidates);
// Set the "needs-ice-restart" flag as described in JSEP. After the flag is
// set, offers should generate new ufrags/passwords until an ICE restart
// occurs.
//
// This and `needs_ice_restart()` must be called on the network thread.
void SetNeedsIceRestartFlag();
// Returns true if the ICE restart flag above was set, and no ICE restart has
// occurred yet for this transport (by applying a local description with
// changed ufrag/password).
bool needs_ice_restart() const {
RTC_DCHECK_RUN_ON(network_thread_);
return needs_ice_restart_;
}
// Returns role if negotiated, or empty std::optional if it hasn't been
// negotiated yet.
std::optional<SSLRole> GetDtlsRole() const;
bool GetStats(TransportStats* stats) const;
const JsepTransportDescription* local_description() const {
RTC_DCHECK_RUN_ON(network_thread_);
return local_description_.get();
}
const JsepTransportDescription* remote_description() const {
RTC_DCHECK_RUN_ON(network_thread_);
return remote_description_.get();
}
// Returns the rtp transport, if any.
RtpTransportInternal* rtp_transport() const {
if (dtls_srtp_transport_) {
return dtls_srtp_transport_.get();
}
if (sdes_transport_) {
return sdes_transport_.get();
}
if (unencrypted_rtp_transport_) {
return unencrypted_rtp_transport_.get();
}
return nullptr;
}
const DtlsTransportInternal* rtp_dtls_transport() const {
if (rtp_dtls_transport_) {
return rtp_dtls_transport_->internal();
}
return nullptr;
}
DtlsTransportInternal* rtp_dtls_transport() {
if (rtp_dtls_transport_) {
return rtp_dtls_transport_->internal();
}
return nullptr;
}
const DtlsTransportInternal* rtcp_dtls_transport() const {
RTC_DCHECK_RUN_ON(network_thread_);
if (rtcp_dtls_transport_) {
return rtcp_dtls_transport_->internal();
}
return nullptr;
}
DtlsTransportInternal* rtcp_dtls_transport() {
RTC_DCHECK_RUN_ON(network_thread_);
if (rtcp_dtls_transport_) {
return rtcp_dtls_transport_->internal();
}
return nullptr;
}
scoped_refptr<DtlsTransport> RtpDtlsTransport() {
return rtp_dtls_transport_;
}
scoped_refptr<SctpTransport> SctpTransport() const { return sctp_transport_; }
// TODO(bugs.webrtc.org/9719): Delete method, update callers to use
// SctpTransport() instead.
DataChannelTransportInterface* data_channel_transport() const {
return sctp_transport_.get();
}
// TODO(deadbeef): The methods below are only public for testing. Should make
// them utility functions or objects so they can be tested independently from
// this class.
// Returns an error if the certificate's identity does not match the
// fingerprint, or either is NULL.
RTCError VerifyCertificateFingerprint(
const RTCCertificate* certificate,
const SSLFingerprint* fingerprint) const;
void SetActiveResetSrtpParams(bool active_reset_srtp_params);
// Record the PT mappings from a single media section.
// This is used to store info needed when generating subsequent SDP.
RTCError RecordPayloadTypes(bool local,
SdpType type,
const ContentInfo& content);
const PayloadTypeRecorder& remote_payload_types() const {
return remote_payload_types_;
}
const PayloadTypeRecorder& local_payload_types() const {
return local_payload_types_;
}
PayloadTypeRecorder& local_payload_types() { return local_payload_types_; }
void CommitPayloadTypes() {
RTC_DCHECK_RUN_ON(network_thread_);
local_payload_types_.Commit();
remote_payload_types_.Commit();
}
private:
bool SetRtcpMux(bool enable, SdpType type, ContentSource source);
void ActivateRtcpMux() RTC_RUN_ON(network_thread_);
// Negotiates and sets the DTLS parameters based on the current local and
// remote transport description, such as the DTLS role to use, and whether
// DTLS should be activated.
//
// Called when an answer TransportDescription is applied.
RTCError NegotiateAndSetDtlsParameters(SdpType local_description_type);
// Negotiates the DTLS role based off the offer and answer as specified by
// RFC 4145, section-4.1. Returns an RTCError if role cannot be determined
// from the local description and remote description.
RTCError NegotiateDtlsRole(SdpType local_description_type,
ConnectionRole local_connection_role,
ConnectionRole remote_connection_role,
std::optional<SSLRole>* negotiated_dtls_role);
// Pushes down the ICE parameters from the remote description.
void SetRemoteIceParameters(const IceParameters& ice_parameters,
IceTransportInternal* ice);
// Pushes down the DTLS parameters obtained via negotiation.
static RTCError SetNegotiatedDtlsParameters(
DtlsTransportInternal* dtls_transport,
std::optional<SSLRole> dtls_role,
SSLFingerprint* remote_fingerprint);
bool GetTransportStats(DtlsTransportInternal* dtls_transport,
int component,
TransportStats* stats) const;
// Owning thread, for safety checks
const Thread* const network_thread_;
const std::string mid_;
// needs-ice-restart bit as described in JSEP.
bool needs_ice_restart_ RTC_GUARDED_BY(network_thread_) = false;
scoped_refptr<RTCCertificate> local_certificate_
RTC_GUARDED_BY(network_thread_);
std::unique_ptr<JsepTransportDescription> local_description_
RTC_GUARDED_BY(network_thread_);
std::unique_ptr<JsepTransportDescription> remote_description_
RTC_GUARDED_BY(network_thread_);
// Ice transport which may be used by any of upper-layer transports (below).
// Owned by JsepTransport and guaranteed to outlive the transports below.
const scoped_refptr<IceTransportInterface> ice_transport_;
const scoped_refptr<IceTransportInterface> rtcp_ice_transport_;
// To avoid downcasting and make it type safe, keep three unique pointers for
// different SRTP mode and only one of these is non-nullptr.
const std::unique_ptr<RtpTransport> unencrypted_rtp_transport_;
const std::unique_ptr<SrtpTransport> sdes_transport_;
const std::unique_ptr<DtlsSrtpTransport> dtls_srtp_transport_;
const scoped_refptr<DtlsTransport> rtp_dtls_transport_;
// The RTCP transport is const for all usages, except that it is cleared
// when RTCP multiplexing is turned on; this happens on the network thread.
scoped_refptr<DtlsTransport> rtcp_dtls_transport_
RTC_GUARDED_BY(network_thread_);
const scoped_refptr<::webrtc::SctpTransport> sctp_transport_;
RtcpMuxFilter rtcp_mux_negotiator_ RTC_GUARDED_BY(network_thread_);
// Cache the encrypted header extension IDs for SDES negoitation.
std::optional<std::vector<int>> send_extension_ids_
RTC_GUARDED_BY(network_thread_);
std::optional<std::vector<int>> recv_extension_ids_
RTC_GUARDED_BY(network_thread_);
// This is invoked when RTCP-mux becomes active and
// `rtcp_dtls_transport_` is destroyed. The JsepTransportController will
// receive the callback and update the aggregate transport states.
std::function<void()> rtcp_mux_active_callback_;
// Assigned PTs from the remote description, used when sending.
PayloadTypeRecorder remote_payload_types_ RTC_GUARDED_BY(network_thread_);
// Assigned PTs from the local description, used when receiving.
PayloadTypeRecorder local_payload_types_ RTC_GUARDED_BY(network_thread_);
};
} // namespace webrtc
// Re-export symbols from the webrtc namespace for backwards compatibility.
// TODO(bugs.webrtc.org/4222596): Remove once all references are updated.
#ifdef WEBRTC_ALLOW_DEPRECATED_NAMESPACES
namespace cricket {
using ::webrtc::JsepTransport;
using ::webrtc::JsepTransportDescription;
} // namespace cricket
#endif // WEBRTC_ALLOW_DEPRECATED_NAMESPACES
#endif // PC_JSEP_TRANSPORT_H_
|