#include "remoting/protocol/webrtc_transport.h"
#include <algorithm>
#include <memory>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
#include "base/base64.h"
#include "base/functional/bind.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "base/memory/raw_ref.h"
#include "base/strings/string_number_conversions.h"
#include "base/task/single_thread_task_runner.h"
#include "base/threading/thread_restrictions.h"
#include "base/time/time.h"
#include "components/webrtc/net_address_utils.h"
#include "components/webrtc/thread_wrapper.h"
#include "remoting/base/constants.h"
#include "remoting/base/logging.h"
#include "remoting/protocol/authenticator.h"
#include "remoting/protocol/errors.h"
#include "remoting/protocol/port_allocator_factory.h"
#include "remoting/protocol/sdp_message.h"
#include "remoting/protocol/transport.h"
#include "remoting/protocol/transport_context.h"
#include "remoting/protocol/webrtc_audio_module.h"
#include "third_party/libjingle_xmpp/xmllite/xmlelement.h"
#include "third_party/webrtc/api/audio_codecs/audio_decoder_factory_template.h"
#include "third_party/webrtc/api/audio_codecs/audio_encoder_factory_template.h"
#include "third_party/webrtc/api/audio_codecs/opus/audio_decoder_opus.h"
#include "third_party/webrtc/api/audio_codecs/opus/audio_encoder_opus.h"
#include "third_party/webrtc/api/enable_media.h"
#include "third_party/webrtc/api/peer_connection_interface.h"
#include "third_party/webrtc/api/rtc_event_log/rtc_event_log_factory.h"
#include "third_party/webrtc/api/video_codecs/builtin_video_decoder_factory.h"
#include "third_party/webrtc/modules/audio_processing/include/audio_processing.h"
#include "third_party/webrtc_overrides/task_queue_factory.h"
#if !defined(NDEBUG)
#include "base/command_line.h"
#endif
QName;
XmlElement;
namespace remoting::protocol {
class ScopedAllowThreadJoinForWebRtcTransport
: public base::ScopedAllowBaseSyncPrimitivesOutsideBlockingScope { … };
class ScopedAllowSyncPrimitivesForWebRtcTransport
: public base::ScopedAllowBaseSyncPrimitivesOutsideBlockingScope { … };
namespace {
DataChannelState;
const int kTransportInfoSendDelayMs = …;
const char kTransportNamespace[] = …;
const int kMaxBitrateBps = …;
constexpr base::TimeDelta kDefaultDataChannelStatePollingInterval = …;
constexpr base::TimeDelta kWaitForDataChannelsClosedTimeout = …;
constexpr base::TimeDelta kCloseAfterDisconnectTimeout = …;
base::TimeDelta data_channel_state_polling_interval = …;
#if !defined(NDEBUG)
const char kDisableAuthenticationSwitchName[] = …;
#endif
bool IsValidSessionDescriptionType(webrtc::SdpType type) { … }
void UpdateCodecParameters(SdpMessage* sdp_message, bool incoming) { … }
std::string GetTransportProtocol(const cricket::CandidatePair& candidate_pair) { … }
std::optional<bool> IsConnectionRelayed(
const cricket::CandidatePair& selected_candidate_pair) { … }
TransportRoute::RouteType CandidateTypeToTransportRouteType(
const cricket::Candidate& candidate) { … }
void SetSenderParameters(webrtc::RtpSenderInterface& sender,
const webrtc::RtpParameters& parameters) { … }
void SetDefaultSenderParameters(
rtc::scoped_refptr<webrtc::RtpSenderInterface> sender) { … }
class CreateSessionDescriptionObserver
: public webrtc::CreateSessionDescriptionObserver { … };
class SetSessionDescriptionObserver
: public webrtc::SetSessionDescriptionObserver { … };
class RtcEventLogOutput : public webrtc::RtcEventLogOutput { … };
}
class WebrtcTransport::PeerConnectionWrapper
: public webrtc::PeerConnectionObserver { … };
WebrtcTransport::WebrtcTransport(
rtc::Thread* worker_thread,
scoped_refptr<TransportContext> transport_context,
std::unique_ptr<webrtc::VideoEncoderFactory> video_encoder_factory,
EventHandler* event_handler)
: … { … }
WebrtcTransport::~WebrtcTransport() { … }
webrtc::PeerConnectionInterface* WebrtcTransport::peer_connection() { … }
webrtc::PeerConnectionFactoryInterface*
WebrtcTransport::peer_connection_factory() { … }
WebrtcAudioModule* WebrtcTransport::audio_module() { … }
std::unique_ptr<MessagePipe> WebrtcTransport::CreateOutgoingChannel(
const std::string& name) { … }
void WebrtcTransport::ApplyNetworkSettings(
const NetworkSettings& network_settings) { … }
void WebrtcTransport::Start(
Authenticator* authenticator,
SendTransportInfoCallback send_transport_info_callback) { … }
bool WebrtcTransport::ProcessTransportInfo(XmlElement* transport_info) { … }
const SessionOptions& WebrtcTransport::session_options() const { … }
void WebrtcTransport::SetPreferredBitrates(std::optional<int> min_bitrate_bps,
std::optional<int> max_bitrate_bps) { … }
void WebrtcTransport::RequestIceRestart() { … }
void WebrtcTransport::RequestSdpRestart() { … }
void WebrtcTransport::SetDataChannelPollingIntervalForTests(
base::TimeDelta new_polling_interval) { … }
void WebrtcTransport::ClosePeerConnection(
rtc::scoped_refptr<webrtc::DataChannelInterface> control_data_channel,
rtc::scoped_refptr<webrtc::DataChannelInterface> event_data_channel,
std::unique_ptr<PeerConnectionWrapper> peer_connection_wrapper,
base::Time start_time = base::Time::Now()) { … }
void WebrtcTransport::Close(ErrorCode error) { … }
void WebrtcTransport::ApplySessionOptions(const SessionOptions& options) { … }
void WebrtcTransport::OnAudioTransceiverCreated(
rtc::scoped_refptr<webrtc::RtpTransceiverInterface> transceiver) { … }
void WebrtcTransport::OnVideoTransceiverCreated(
rtc::scoped_refptr<webrtc::RtpTransceiverInterface> transceiver) { … }
void WebrtcTransport::OnLocalSessionDescriptionCreated(
std::unique_ptr<webrtc::SessionDescriptionInterface> description,
const std::string& error) { … }
void WebrtcTransport::OnLocalDescriptionSet(bool success,
const std::string& error) { … }
void WebrtcTransport::OnRemoteDescriptionSet(bool send_answer,
bool success,
const std::string& error) { … }
void WebrtcTransport::SendAnswer() { … }
void WebrtcTransport::OnCloseAfterDisconnectTimeout() { … }
void WebrtcTransport::OnSignalingChange(
webrtc::PeerConnectionInterface::SignalingState new_state) { … }
void WebrtcTransport::OnAddStream(
rtc::scoped_refptr<webrtc::MediaStreamInterface> stream) { … }
void WebrtcTransport::OnRemoveStream(
rtc::scoped_refptr<webrtc::MediaStreamInterface> stream) { … }
void WebrtcTransport::OnDataChannel(
rtc::scoped_refptr<webrtc::DataChannelInterface> data_channel) { … }
void WebrtcTransport::OnRenegotiationNeeded() { … }
void WebrtcTransport::OnIceConnectionChange(
webrtc::PeerConnectionInterface::IceConnectionState new_state) { … }
void WebrtcTransport::OnIceGatheringChange(
webrtc::PeerConnectionInterface::IceGatheringState new_state) { … }
void WebrtcTransport::OnIceCandidate(
const webrtc::IceCandidateInterface* candidate) { … }
void WebrtcTransport::OnIceSelectedCandidatePairChanged(
const cricket::CandidatePairChangeEvent& event) { … }
std::tuple<int, int> WebrtcTransport::BitratesForConnection() { … }
void WebrtcTransport::UpdateBitrates() { … }
void WebrtcTransport::SetPeerConnectionBitrates(int min_bitrate_bps,
int max_bitrate_bps) { … }
void WebrtcTransport::SetSenderBitrates(
rtc::scoped_refptr<webrtc::RtpSenderInterface> sender,
int min_bitrate_bps,
int max_bitrate_bps) { … }
void WebrtcTransport::RequestNegotiation() { … }
void WebrtcTransport::SendOffer() { … }
void WebrtcTransport::EnsurePendingTransportInfoMessage() { … }
void WebrtcTransport::SendTransportInfo() { … }
void WebrtcTransport::AddPendingCandidatesIfPossible() { … }
void WebrtcTransport::StartRtcEventLogging() { … }
void WebrtcTransport::StopRtcEventLogging() { … }
}