#ifdef UNSAFE_BUFFERS_BUILD
#pragma allow_unsafe_buffers
#endif
#include "third_party/blink/renderer/modules/peerconnection/rtc_peer_connection_handler.h"
#include <string.h>
#include <functional>
#include <memory>
#include <set>
#include <string>
#include <utility>
#include <vector>
#include "base/containers/contains.h"
#include "base/functional/bind.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/memory/scoped_refptr.h"
#include "base/metrics/histogram_functions.h"
#include "base/metrics/histogram_macros.h"
#include "base/numerics/safe_conversions.h"
#include "base/synchronization/waitable_event.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/single_thread_task_runner.h"
#include "base/threading/thread_checker.h"
#include "base/trace_event/trace_event.h"
#include "build/chromecast_buildflags.h"
#include "media/base/media_switches.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/platform/platform.h"
#include "third_party/blink/public/platform/web_string.h"
#include "third_party/blink/public/platform/web_url.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_rtc_session_description_init.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_union_boolean_constrainbooleanparameters.h"
#include "third_party/blink/renderer/core/frame/deprecation/deprecation.h"
#include "third_party/blink/renderer/core/frame/web_feature.h"
#include "third_party/blink/renderer/modules/mediastream/media_constraints.h"
#include "third_party/blink/renderer/modules/mediastream/media_stream_constraints_util.h"
#include "third_party/blink/renderer/modules/peerconnection/adapters/web_rtc_cross_thread_copier.h"
#include "third_party/blink/renderer/modules/peerconnection/peer_connection_dependency_factory.h"
#include "third_party/blink/renderer/modules/peerconnection/peer_connection_tracker.h"
#include "third_party/blink/renderer/modules/peerconnection/rtc_rtp_receiver_impl.h"
#include "third_party/blink/renderer/modules/peerconnection/speed_limit_uma_listener.h"
#include "third_party/blink/renderer/modules/peerconnection/webrtc_set_description_observer.h"
#include "third_party/blink/renderer/modules/webrtc/webrtc_audio_device_impl.h"
#include "third_party/blink/renderer/platform/heap/garbage_collected.h"
#include "third_party/blink/renderer/platform/mediastream/media_stream_component.h"
#include "third_party/blink/renderer/platform/mediastream/media_stream_track_platform.h"
#include "third_party/blink/renderer/platform/mediastream/webrtc_uma_histograms.h"
#include "third_party/blink/renderer/platform/peerconnection/rtc_answer_options_platform.h"
#include "third_party/blink/renderer/platform/peerconnection/rtc_event_log_output_sink.h"
#include "third_party/blink/renderer/platform/peerconnection/rtc_event_log_output_sink_proxy.h"
#include "third_party/blink/renderer/platform/peerconnection/rtc_ice_candidate_platform.h"
#include "third_party/blink/renderer/platform/peerconnection/rtc_offer_options_platform.h"
#include "third_party/blink/renderer/platform/peerconnection/rtc_rtp_sender_platform.h"
#include "third_party/blink/renderer/platform/peerconnection/rtc_rtp_transceiver_platform.h"
#include "third_party/blink/renderer/platform/peerconnection/rtc_scoped_refptr_cross_thread_copier.h"
#include "third_party/blink/renderer/platform/peerconnection/rtc_session_description_platform.h"
#include "third_party/blink/renderer/platform/peerconnection/rtc_session_description_request.h"
#include "third_party/blink/renderer/platform/peerconnection/rtc_stats.h"
#include "third_party/blink/renderer/platform/peerconnection/rtc_void_request.h"
#include "third_party/blink/renderer/platform/scheduler/public/post_cross_thread_task.h"
#include "third_party/blink/renderer/platform/wtf/cross_thread_copier_base.h"
#include "third_party/blink/renderer/platform/wtf/cross_thread_copier_std.h"
#include "third_party/blink/renderer/platform/wtf/cross_thread_functional.h"
#include "third_party/blink/renderer/platform/wtf/text/string_builder.h"
#include "third_party/blink/renderer/platform/wtf/thread_safe_ref_counted.h"
#include "third_party/webrtc/api/data_channel_interface.h"
#include "third_party/webrtc/api/rtc_event_log_output.h"
#include "third_party/webrtc/api/units/time_delta.h"
#include "third_party/webrtc/pc/session_description.h"
DataChannelInterface;
IceCandidateInterface;
MediaStreamInterface;
PeerConnectionInterface;
PeerConnectionObserver;
StatsReport;
StatsReports;
namespace WTF {
template <>
struct CrossThreadCopier<scoped_refptr<DataChannelInterface>>
: public CrossThreadCopierPassThrough<scoped_refptr<DataChannelInterface>> { … };
template <>
struct CrossThreadCopier<scoped_refptr<PeerConnectionInterface>>
: public CrossThreadCopierPassThrough<
scoped_refptr<PeerConnectionInterface>> { … };
template <>
struct CrossThreadCopier<rtc::scoped_refptr<webrtc::StatsObserver>>
: public CrossThreadCopierPassThrough<
rtc::scoped_refptr<webrtc::StatsObserver>> { … };
}
namespace blink {
namespace {
enum class RtcpMux { … };
RTCSessionDescriptionPlatform* CreateWebKitSessionDescription(
const std::string& sdp,
const std::string& type) { … }
RTCSessionDescriptionPlatform* CreateWebKitSessionDescription(
const webrtc::SessionDescriptionInterface* native_desc) { … }
void RunClosureWithTrace(CrossThreadOnceClosure closure,
const char* trace_event_name) { … }
void RunSynchronousOnceClosure(base::OnceClosure closure,
const char* trace_event_name,
base::WaitableEvent* event) { … }
class CreateSessionDescriptionRequest
: public webrtc::CreateSessionDescriptionObserver { … };
RTCStatsReportCallbackInternal;
void GetRTCStatsOnSignalingThread(
const scoped_refptr<base::SingleThreadTaskRunner>& main_thread,
rtc::scoped_refptr<webrtc::PeerConnectionInterface> native_peer_connection,
RTCStatsReportCallbackInternal callback) { … }
std::set<RTCPeerConnectionHandler*>* GetPeerConnectionHandlers() { … }
size_t GetLocalStreamUsageCount(
const Vector<std::unique_ptr<blink::RTCRtpSenderImpl>>& rtp_senders,
const std::string stream_id) { … }
MediaStreamTrackMetrics::Kind MediaStreamTrackMetricsKind(
const MediaStreamComponent* component) { … }
}
ParsedSessionDescription::ParsedSessionDescription(const String& sdp_type,
const String& sdp)
: … { … }
ParsedSessionDescription ParsedSessionDescription::Parse(
const RTCSessionDescriptionInit* session_description_init) { … }
ParsedSessionDescription ParsedSessionDescription::Parse(
const RTCSessionDescriptionPlatform* session_description_platform) { … }
ParsedSessionDescription ParsedSessionDescription::Parse(const String& sdp_type,
const String& sdp) { … }
void ParsedSessionDescription::DoParse() { … }
class RTCPeerConnectionHandler::WebRtcSetDescriptionObserverImpl
: public WebRtcSetDescriptionObserver { … };
class RTCPeerConnectionHandler::Observer
: public GarbageCollected<RTCPeerConnectionHandler::Observer>,
public PeerConnectionObserver,
public RtcEventLogOutputSink { … };
RTCPeerConnectionHandler::RTCPeerConnectionHandler(
RTCPeerConnectionHandlerClient* client,
blink::PeerConnectionDependencyFactory* dependency_factory,
scoped_refptr<base::SingleThreadTaskRunner> task_runner,
bool encoded_insertable_streams)
: … { … }
RTCPeerConnectionHandler::RTCPeerConnectionHandler(
scoped_refptr<base::SingleThreadTaskRunner> task_runner)
: … { … }
RTCPeerConnectionHandler::~RTCPeerConnectionHandler() { … }
void RTCPeerConnectionHandler::CloseAndUnregister() { … }
bool RTCPeerConnectionHandler::Initialize(
ExecutionContext* context,
const webrtc::PeerConnectionInterface::RTCConfiguration&
server_configuration,
WebLocalFrame* frame,
ExceptionState& exception_state,
RTCRtpTransport* rtp_transport) { … }
bool RTCPeerConnectionHandler::InitializeForTest(
const webrtc::PeerConnectionInterface::RTCConfiguration&
server_configuration,
PeerConnectionTracker* peer_connection_tracker,
ExceptionState& exception_state,
RTCRtpTransport* rtp_transport) { … }
Vector<std::unique_ptr<RTCRtpTransceiverPlatform>>
RTCPeerConnectionHandler::CreateOffer(RTCSessionDescriptionRequest* request,
RTCOfferOptionsPlatform* options) { … }
void RTCPeerConnectionHandler::CreateOfferOnSignalingThread(
webrtc::CreateSessionDescriptionObserver* observer,
webrtc::PeerConnectionInterface::RTCOfferAnswerOptions offer_options,
blink::TransceiverStateSurfacer* transceiver_state_surfacer) { … }
void RTCPeerConnectionHandler::CreateAnswer(
blink::RTCSessionDescriptionRequest* request,
blink::RTCAnswerOptionsPlatform* options) { … }
bool IsOfferOrAnswer(const webrtc::SessionDescriptionInterface* native_desc) { … }
void RTCPeerConnectionHandler::SetLocalDescription(
blink::RTCVoidRequest* request) { … }
void RTCPeerConnectionHandler::SetLocalDescription(
blink::RTCVoidRequest* request,
ParsedSessionDescription parsed_sdp) { … }
void RTCPeerConnectionHandler::SetRemoteDescription(
blink::RTCVoidRequest* request,
ParsedSessionDescription parsed_sdp) { … }
const webrtc::PeerConnectionInterface::RTCConfiguration&
RTCPeerConnectionHandler::GetConfiguration() const { … }
webrtc::RTCErrorType RTCPeerConnectionHandler::SetConfiguration(
const webrtc::PeerConnectionInterface::RTCConfiguration& blink_config) { … }
void RTCPeerConnectionHandler::AddIceCandidate(
RTCVoidRequest* request,
RTCIceCandidatePlatform* candidate) { … }
void RTCPeerConnectionHandler::RestartIce() { … }
void RTCPeerConnectionHandler::GetStandardStatsForTracker(
rtc::scoped_refptr<webrtc::RTCStatsCollectorCallback> observer) { … }
void RTCPeerConnectionHandler::EmitCurrentStateForTracker() { … }
void RTCPeerConnectionHandler::GetStats(RTCStatsReportCallback callback) { … }
webrtc::RTCErrorOr<std::unique_ptr<RTCRtpTransceiverPlatform>>
RTCPeerConnectionHandler::AddTransceiverWithTrack(
MediaStreamComponent* component,
const webrtc::RtpTransceiverInit& init) { … }
void RTCPeerConnectionHandler::AddTransceiverWithTrackOnSignalingThread(
webrtc::MediaStreamTrackInterface* webrtc_track,
webrtc::RtpTransceiverInit init,
blink::TransceiverStateSurfacer* transceiver_state_surfacer,
webrtc::RTCErrorOr<rtc::scoped_refptr<webrtc::RtpTransceiverInterface>>*
error_or_transceiver) { … }
webrtc::RTCErrorOr<std::unique_ptr<RTCRtpTransceiverPlatform>>
RTCPeerConnectionHandler::AddTransceiverWithKind(
const String& kind,
const webrtc::RtpTransceiverInit& init) { … }
void RTCPeerConnectionHandler::AddTransceiverWithMediaTypeOnSignalingThread(
cricket::MediaType media_type,
webrtc::RtpTransceiverInit init,
blink::TransceiverStateSurfacer* transceiver_state_surfacer,
webrtc::RTCErrorOr<rtc::scoped_refptr<webrtc::RtpTransceiverInterface>>*
error_or_transceiver) { … }
webrtc::RTCErrorOr<std::unique_ptr<RTCRtpTransceiverPlatform>>
RTCPeerConnectionHandler::AddTrack(
MediaStreamComponent* component,
const MediaStreamDescriptorVector& descriptors) { … }
void RTCPeerConnectionHandler::AddTrackOnSignalingThread(
webrtc::MediaStreamTrackInterface* track,
std::vector<std::string> stream_ids,
blink::TransceiverStateSurfacer* transceiver_state_surfacer,
webrtc::RTCErrorOr<rtc::scoped_refptr<webrtc::RtpSenderInterface>>*
error_or_sender) { … }
webrtc::RTCErrorOr<std::unique_ptr<RTCRtpTransceiverPlatform>>
RTCPeerConnectionHandler::RemoveTrack(blink::RTCRtpSenderPlatform* web_sender) { … }
void RTCPeerConnectionHandler::RemoveTrackOnSignalingThread(
webrtc::RtpSenderInterface* sender,
blink::TransceiverStateSurfacer* transceiver_state_surfacer,
std::optional<webrtc::RTCError>* result) { … }
Vector<std::unique_ptr<blink::RTCRtpSenderPlatform>>
RTCPeerConnectionHandler::GetPlatformSenders() const { … }
void RTCPeerConnectionHandler::CloseClientPeerConnection() { … }
void RTCPeerConnectionHandler::MaybeCreateThermalUmaListner() { … }
ThermalUmaListener* RTCPeerConnectionHandler::thermal_uma_listener() const { … }
SpeedLimitUmaListener* RTCPeerConnectionHandler::speed_limit_uma_listener()
const { … }
void RTCPeerConnectionHandler::OnThermalStateChange(
mojom::blink::DeviceThermalState thermal_state) { … }
void RTCPeerConnectionHandler::OnSpeedLimitChange(int32_t speed_limit) { … }
void RTCPeerConnectionHandler::StartEventLog(int output_period_ms) { … }
void RTCPeerConnectionHandler::StopEventLog() { … }
void RTCPeerConnectionHandler::OnWebRtcEventLogWrite(
const WTF::Vector<uint8_t>& output) { … }
rtc::scoped_refptr<DataChannelInterface>
RTCPeerConnectionHandler::CreateDataChannel(
const String& label,
const webrtc::DataChannelInit& init) { … }
void RTCPeerConnectionHandler::Close() { … }
webrtc::PeerConnectionInterface*
RTCPeerConnectionHandler::NativePeerConnection() { … }
void RTCPeerConnectionHandler::RunSynchronousOnceClosureOnSignalingThread(
base::OnceClosure closure,
const char* trace_event_name) { … }
void RTCPeerConnectionHandler::OnSessionDescriptionsUpdated(
std::unique_ptr<webrtc::SessionDescriptionInterface>
pending_local_description,
std::unique_ptr<webrtc::SessionDescriptionInterface>
current_local_description,
std::unique_ptr<webrtc::SessionDescriptionInterface>
pending_remote_description,
std::unique_ptr<webrtc::SessionDescriptionInterface>
current_remote_description) { … }
void RTCPeerConnectionHandler::TrackSignalingChange(
webrtc::PeerConnectionInterface::SignalingState new_state) { … }
void RTCPeerConnectionHandler::OnIceConnectionChange(
webrtc::PeerConnectionInterface::IceConnectionState new_state) { … }
void RTCPeerConnectionHandler::TrackIceConnectionStateChange(
webrtc::PeerConnectionInterface::IceConnectionState state) { … }
void RTCPeerConnectionHandler::OnConnectionChange(
webrtc::PeerConnectionInterface::PeerConnectionState new_state) { … }
void RTCPeerConnectionHandler::OnIceGatheringChange(
webrtc::PeerConnectionInterface::IceGatheringState new_state) { … }
void RTCPeerConnectionHandler::OnNegotiationNeededEvent(uint32_t event_id) { … }
void RTCPeerConnectionHandler::OnModifySctpTransport(
blink::WebRTCSctpTransportSnapshot state) { … }
void RTCPeerConnectionHandler::OnModifyTransceivers(
webrtc::PeerConnectionInterface::SignalingState signaling_state,
std::vector<blink::RtpTransceiverState> transceiver_states,
bool is_remote_description,
bool is_rollback) { … }
void RTCPeerConnectionHandler::OnDataChannel(
rtc::scoped_refptr<DataChannelInterface> channel) { … }
void RTCPeerConnectionHandler::OnIceCandidate(const String& sdp,
const String& sdp_mid,
int sdp_mline_index,
int component,
int address_family,
const String& usernameFragment,
const String& url) { … }
void RTCPeerConnectionHandler::OnIceCandidateError(const String& address,
std::optional<uint16_t> port,
const String& host_candidate,
const String& url,
int error_code,
const String& error_text) { … }
void RTCPeerConnectionHandler::OnInterestingUsage(int usage_pattern) { … }
RTCPeerConnectionHandler::FirstSessionDescription::FirstSessionDescription(
const webrtc::SessionDescriptionInterface* sdesc) { … }
void RTCPeerConnectionHandler::ReportFirstSessionDescriptions(
const FirstSessionDescription& local,
const FirstSessionDescription& remote) { … }
Vector<std::unique_ptr<blink::RTCRtpSenderImpl>>::iterator
RTCPeerConnectionHandler::FindSender(uintptr_t id) { … }
Vector<std::unique_ptr<blink::RTCRtpReceiverImpl>>::iterator
RTCPeerConnectionHandler::FindReceiver(uintptr_t id) { … }
Vector<std::unique_ptr<blink::RTCRtpTransceiverImpl>>::iterator
RTCPeerConnectionHandler::FindTransceiver(uintptr_t id) { … }
wtf_size_t RTCPeerConnectionHandler::GetTransceiverIndex(
const RTCRtpTransceiverPlatform& platform_transceiver) { … }
std::unique_ptr<blink::RTCRtpTransceiverImpl>
RTCPeerConnectionHandler::CreateOrUpdateTransceiver(
blink::RtpTransceiverState transceiver_state,
blink::TransceiverStateUpdateMode update_mode) { … }
scoped_refptr<base::SingleThreadTaskRunner>
RTCPeerConnectionHandler::signaling_thread() const { … }
void RTCPeerConnectionHandler::ReportICEState(
webrtc::PeerConnectionInterface::IceConnectionState new_state) { … }
}