Convert libwebrtc error types to DOM exceptions
[WebKit-https.git] / Source / WebCore / Modules / mediastream / libwebrtc / LibWebRTCMediaEndpoint.cpp
1 /*
2  * Copyright (C) 2017-2018 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1.  Redistributions of source code must retain the above copyright
8  *     notice, this list of conditions and the following disclaimer.
9  * 2.  Redistributions in binary form must reproduce the above copyright
10  *     notice, this list of conditions and the following disclaimer in the
11  *     documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16  * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
17  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  */
24
25 #include "config.h"
26 #include "LibWebRTCMediaEndpoint.h"
27
28 #if USE(LIBWEBRTC)
29
30 #include "EventNames.h"
31 #include "JSRTCStatsReport.h"
32 #include "LibWebRTCDataChannelHandler.h"
33 #include "LibWebRTCPeerConnectionBackend.h"
34 #include "LibWebRTCProvider.h"
35 #include "LibWebRTCRtpReceiverBackend.h"
36 #include "LibWebRTCRtpSenderBackend.h"
37 #include "LibWebRTCRtpTransceiverBackend.h"
38 #include "LibWebRTCStatsCollector.h"
39 #include "LibWebRTCUtils.h"
40 #include "Logging.h"
41 #include "NotImplemented.h"
42 #include "Performance.h"
43 #include "PlatformStrategies.h"
44 #include "RTCDataChannel.h"
45 #include "RTCDataChannelEvent.h"
46 #include "RTCOfferOptions.h"
47 #include "RTCPeerConnection.h"
48 #include "RTCSessionDescription.h"
49 #include "RTCStatsReport.h"
50 #include "RTCTrackEvent.h"
51 #include "RealtimeIncomingAudioSource.h"
52 #include "RealtimeIncomingVideoSource.h"
53 #include "RealtimeOutgoingAudioSource.h"
54 #include "RealtimeOutgoingVideoSource.h"
55 #include "RuntimeEnabledFeatures.h"
56 #include <webrtc/rtc_base/physicalsocketserver.h>
57 #include <webrtc/p2p/base/basicpacketsocketfactory.h>
58 #include <webrtc/p2p/client/basicportallocator.h>
59 #include <webrtc/pc/peerconnectionfactory.h>
60 #include <webrtc/system_wrappers/include/field_trial.h>
61 #include <wtf/MainThread.h>
62
63 namespace WebCore {
64
65 LibWebRTCMediaEndpoint::LibWebRTCMediaEndpoint(LibWebRTCPeerConnectionBackend& peerConnection, LibWebRTCProvider& client)
66     : m_peerConnectionBackend(peerConnection)
67     , m_peerConnectionFactory(*client.factory())
68     , m_createSessionDescriptionObserver(*this)
69     , m_setLocalSessionDescriptionObserver(*this)
70     , m_setRemoteSessionDescriptionObserver(*this)
71     , m_statsLogTimer(*this, &LibWebRTCMediaEndpoint::gatherStatsForLogging)
72 #if !RELEASE_LOG_DISABLED
73     , m_logger(peerConnection.logger())
74     , m_logIdentifier(peerConnection.logIdentifier())
75 #endif
76 {
77     ASSERT(isMainThread());
78     ASSERT(client.factory());
79
80     if (RuntimeEnabledFeatures::sharedFeatures().webRTCH264SimulcastEnabled())
81         webrtc::field_trial::InitFieldTrialsFromString("WebRTC-H264Simulcast/Enabled/");
82 }
83
84 bool LibWebRTCMediaEndpoint::setConfiguration(LibWebRTCProvider& client, webrtc::PeerConnectionInterface::RTCConfiguration&& configuration)
85 {
86     if (RuntimeEnabledFeatures::sharedFeatures().webRTCUnifiedPlanEnabled())
87         configuration.sdp_semantics = webrtc::SdpSemantics::kUnifiedPlan;
88
89     if (!m_backend) {
90         m_backend = client.createPeerConnection(*this, WTFMove(configuration));
91         return !!m_backend;
92     }
93     auto oldConfiguration = m_backend->GetConfiguration();
94     configuration.certificates = oldConfiguration.certificates;
95     return m_backend->SetConfiguration(WTFMove(configuration));
96 }
97
98 static inline const char* sessionDescriptionType(RTCSdpType sdpType)
99 {
100     switch (sdpType) {
101     case RTCSdpType::Offer:
102         return "offer";
103     case RTCSdpType::Pranswer:
104         return "pranswer";
105     case RTCSdpType::Answer:
106         return "answer";
107     case RTCSdpType::Rollback:
108         return "rollback";
109     }
110
111     ASSERT_NOT_REACHED();
112     return "";
113 }
114
115 static inline RTCSdpType fromSessionDescriptionType(const webrtc::SessionDescriptionInterface& description)
116 {
117     auto type = description.type();
118     if (type == webrtc::SessionDescriptionInterface::kOffer)
119         return RTCSdpType::Offer;
120     if (type == webrtc::SessionDescriptionInterface::kAnswer)
121         return RTCSdpType::Answer;
122     ASSERT(type == webrtc::SessionDescriptionInterface::kPrAnswer);
123     return RTCSdpType::Pranswer;
124 }
125
126 static inline RefPtr<RTCSessionDescription> fromSessionDescription(const webrtc::SessionDescriptionInterface* description)
127 {
128     if (!description)
129         return nullptr;
130
131     std::string sdp;
132     description->ToString(&sdp);
133
134     return RTCSessionDescription::create(fromSessionDescriptionType(*description), fromStdString(sdp));
135 }
136
137 // FIXME: We might want to create a new object only if the session actually changed for all description getters.
138 RefPtr<RTCSessionDescription> LibWebRTCMediaEndpoint::currentLocalDescription() const
139 {
140     return m_backend ? fromSessionDescription(m_backend->current_local_description()) : nullptr;
141 }
142
143 RefPtr<RTCSessionDescription> LibWebRTCMediaEndpoint::currentRemoteDescription() const
144 {
145     return m_backend ? fromSessionDescription(m_backend->current_remote_description()) : nullptr;
146 }
147
148 RefPtr<RTCSessionDescription> LibWebRTCMediaEndpoint::pendingLocalDescription() const
149 {
150     return m_backend ? fromSessionDescription(m_backend->pending_local_description()) : nullptr;
151 }
152
153 RefPtr<RTCSessionDescription> LibWebRTCMediaEndpoint::pendingRemoteDescription() const
154 {
155     return m_backend ? fromSessionDescription(m_backend->pending_remote_description()) : nullptr;
156 }
157
158 RefPtr<RTCSessionDescription> LibWebRTCMediaEndpoint::localDescription() const
159 {
160     return m_backend ? fromSessionDescription(m_backend->local_description()) : nullptr;
161 }
162
163 RefPtr<RTCSessionDescription> LibWebRTCMediaEndpoint::remoteDescription() const
164 {
165     return m_backend ? fromSessionDescription(m_backend->remote_description()) : nullptr;
166 }
167
168 void LibWebRTCMediaEndpoint::doSetLocalDescription(RTCSessionDescription& description)
169 {
170     ASSERT(m_backend);
171
172     webrtc::SdpParseError error;
173     std::unique_ptr<webrtc::SessionDescriptionInterface> sessionDescription(webrtc::CreateSessionDescription(sessionDescriptionType(description.type()), description.sdp().utf8().data(), &error));
174
175     if (!sessionDescription) {
176         m_peerConnectionBackend.setLocalDescriptionFailed(Exception { OperationError, fromStdString(error.description) });
177         return;
178     }
179
180     // FIXME: See https://bugs.webkit.org/show_bug.cgi?id=173783. Remove this test once fixed at LibWebRTC level.
181     if (description.type() == RTCSdpType::Answer && !m_backend->pending_remote_description()) {
182         m_peerConnectionBackend.setLocalDescriptionFailed(Exception { InvalidStateError, "Failed to set local answer sdp: no pending remote description."_s });
183         return;
184     }
185
186     m_backend->SetLocalDescription(&m_setLocalSessionDescriptionObserver, sessionDescription.release());
187 }
188
189 void LibWebRTCMediaEndpoint::doSetRemoteDescription(RTCSessionDescription& description)
190 {
191     ASSERT(m_backend);
192
193     webrtc::SdpParseError error;
194     std::unique_ptr<webrtc::SessionDescriptionInterface> sessionDescription(webrtc::CreateSessionDescription(sessionDescriptionType(description.type()), description.sdp().utf8().data(), &error));
195     if (!sessionDescription) {
196         m_peerConnectionBackend.setRemoteDescriptionFailed(Exception { SyntaxError, fromStdString(error.description) });
197         return;
198     }
199     m_backend->SetRemoteDescription(&m_setRemoteSessionDescriptionObserver, sessionDescription.release());
200
201     startLoggingStats();
202 }
203
204 bool LibWebRTCMediaEndpoint::addTrack(LibWebRTCRtpSenderBackend& sender, MediaStreamTrack& track, const Vector<String>& mediaStreamIds)
205 {
206     ASSERT(m_backend);
207
208     if (!RuntimeEnabledFeatures::sharedFeatures().webRTCUnifiedPlanEnabled()) {
209         String mediaStreamId = mediaStreamIds.isEmpty() ? createCanonicalUUIDString() : mediaStreamIds[0];
210         m_localStreams.ensure(mediaStreamId, [&] {
211             auto mediaStream = m_peerConnectionFactory.CreateLocalMediaStream(mediaStreamId.utf8().data());
212             m_backend->AddStream(mediaStream);
213             return mediaStream;
214         });
215     }
216
217     LibWebRTCRtpSenderBackend::Source source;
218     rtc::scoped_refptr<webrtc::MediaStreamTrackInterface> rtcTrack;
219     switch (track.privateTrack().type()) {
220     case RealtimeMediaSource::Type::Audio: {
221         auto audioSource = RealtimeOutgoingAudioSource::create(track.privateTrack());
222         rtcTrack = m_peerConnectionFactory.CreateAudioTrack(track.id().utf8().data(), audioSource.ptr());
223         source = WTFMove(audioSource);
224         break;
225     }
226     case RealtimeMediaSource::Type::Video: {
227         auto videoSource = RealtimeOutgoingVideoSource::create(track.privateTrack());
228         rtcTrack = m_peerConnectionFactory.CreateVideoTrack(track.id().utf8().data(), videoSource.ptr());
229         source = WTFMove(videoSource);
230         break;
231     }
232     case RealtimeMediaSource::Type::None:
233         ASSERT_NOT_REACHED();
234         return false;
235     }
236
237     sender.setSource(WTFMove(source));
238     if (auto rtpSender = sender.rtcSender()) {
239         rtpSender->SetTrack(rtcTrack.get());
240         return true;
241     }
242
243     std::vector<std::string> ids;
244     for (auto& id : mediaStreamIds)
245         ids.push_back(id.utf8().data());
246
247     auto newRTPSender = m_backend->AddTrack(rtcTrack.get(), WTFMove(ids));
248     if (!newRTPSender.ok())
249         return false;
250     sender.setRTCSender(newRTPSender.MoveValue());
251     return true;
252 }
253
254 void LibWebRTCMediaEndpoint::removeTrack(LibWebRTCRtpSenderBackend& sender)
255 {
256     ASSERT(m_backend);
257     m_backend->RemoveTrack(sender.rtcSender());
258 }
259
260 void LibWebRTCMediaEndpoint::doCreateOffer(const RTCOfferOptions& options)
261 {
262     ASSERT(m_backend);
263
264     m_isInitiator = true;
265     webrtc::PeerConnectionInterface::RTCOfferAnswerOptions rtcOptions;
266     rtcOptions.ice_restart = options.iceRestart;
267     rtcOptions.voice_activity_detection = options.voiceActivityDetection;
268
269     if (!RuntimeEnabledFeatures::sharedFeatures().webRTCUnifiedPlanEnabled()) {
270         if (m_peerConnectionBackend.shouldOfferAllowToReceive("audio"_s))
271             rtcOptions.offer_to_receive_audio = webrtc::PeerConnectionInterface::RTCOfferAnswerOptions::kOfferToReceiveMediaTrue;
272         if (m_peerConnectionBackend.shouldOfferAllowToReceive("video"_s))
273             rtcOptions.offer_to_receive_video = webrtc::PeerConnectionInterface::RTCOfferAnswerOptions::kOfferToReceiveMediaTrue;
274     }
275     m_backend->CreateOffer(&m_createSessionDescriptionObserver, rtcOptions);
276 }
277
278 void LibWebRTCMediaEndpoint::doCreateAnswer()
279 {
280     ASSERT(m_backend);
281
282     m_isInitiator = false;
283     m_backend->CreateAnswer(&m_createSessionDescriptionObserver, { });
284 }
285
286 void LibWebRTCMediaEndpoint::getStats(Ref<DeferredPromise>&& promise, WTF::Function<void(rtc::scoped_refptr<LibWebRTCStatsCollector>&&)>&& getStatsFunction)
287 {
288     auto collector = LibWebRTCStatsCollector::create([promise = WTFMove(promise), protectedThis = makeRef(*this)](auto&& report) mutable {
289         ASSERT(isMainThread());
290         if (protectedThis->isStopped() || !report)
291             return false;
292
293         promise->resolve<IDLInterface<RTCStatsReport>>(report.releaseNonNull());
294         return true;
295     });
296     LibWebRTCProvider::callOnWebRTCSignalingThread([getStatsFunction = WTFMove(getStatsFunction), collector = WTFMove(collector)]() mutable {
297         getStatsFunction(WTFMove(collector));
298     });
299 }
300
301 void LibWebRTCMediaEndpoint::getStats(Ref<DeferredPromise>&& promise)
302 {
303     getStats(WTFMove(promise), [this](auto&& collector) {
304         if (m_backend)
305             m_backend->GetStats(WTFMove(collector));
306     });
307 }
308
309 void LibWebRTCMediaEndpoint::getStats(webrtc::RtpReceiverInterface& receiver, Ref<DeferredPromise>&& promise)
310 {
311     getStats(WTFMove(promise), [this, receiver = rtc::scoped_refptr<webrtc::RtpReceiverInterface>(&receiver)](auto&& collector) mutable {
312         if (m_backend)
313             m_backend->GetStats(WTFMove(receiver), WTFMove(collector));
314     });
315 }
316
317 void LibWebRTCMediaEndpoint::getStats(webrtc::RtpSenderInterface& sender, Ref<DeferredPromise>&& promise)
318 {
319     getStats(WTFMove(promise), [this, sender = rtc::scoped_refptr<webrtc::RtpSenderInterface>(&sender)](auto&& collector)  mutable {
320         if (m_backend)
321             m_backend->GetStats(WTFMove(sender), WTFMove(collector));
322     });
323 }
324
325 static RTCSignalingState signalingState(webrtc::PeerConnectionInterface::SignalingState state)
326 {
327     switch (state) {
328     case webrtc::PeerConnectionInterface::kStable:
329         return RTCSignalingState::Stable;
330     case webrtc::PeerConnectionInterface::kHaveLocalOffer:
331         return RTCSignalingState::HaveLocalOffer;
332     case webrtc::PeerConnectionInterface::kHaveLocalPrAnswer:
333         return RTCSignalingState::HaveLocalPranswer;
334     case webrtc::PeerConnectionInterface::kHaveRemoteOffer:
335         return RTCSignalingState::HaveRemoteOffer;
336     case webrtc::PeerConnectionInterface::kHaveRemotePrAnswer:
337         return RTCSignalingState::HaveRemotePranswer;
338     case webrtc::PeerConnectionInterface::kClosed:
339         return RTCSignalingState::Stable;
340     }
341
342     ASSERT_NOT_REACHED();
343     return RTCSignalingState::Stable;
344 }
345
346 void LibWebRTCMediaEndpoint::OnSignalingChange(webrtc::PeerConnectionInterface::SignalingState rtcState)
347 {
348     auto state = signalingState(rtcState);
349     callOnMainThread([protectedThis = makeRef(*this), state] {
350         if (protectedThis->isStopped())
351             return;
352         protectedThis->m_peerConnectionBackend.updateSignalingState(state);
353     });
354 }
355
356 MediaStream& LibWebRTCMediaEndpoint::mediaStreamFromRTCStream(webrtc::MediaStreamInterface& rtcStream)
357 {
358     auto label = fromStdString(rtcStream.id());
359     auto mediaStream = m_remoteStreamsById.ensure(label, [label, this]() mutable {
360         return MediaStream::create(*m_peerConnectionBackend.connection().scriptExecutionContext(), MediaStreamPrivate::create({ }, WTFMove(label)));
361     });
362     return *mediaStream.iterator->value;
363 }
364
365 void LibWebRTCMediaEndpoint::addRemoteStream(webrtc::MediaStreamInterface&)
366 {
367 }
368
369 void LibWebRTCMediaEndpoint::addRemoteTrack(rtc::scoped_refptr<webrtc::RtpReceiverInterface>&& rtcReceiver, const std::vector<rtc::scoped_refptr<webrtc::MediaStreamInterface>>& rtcStreams)
370 {
371     ASSERT(rtcReceiver);
372     RefPtr<RTCRtpReceiver> receiver;
373     RefPtr<RealtimeMediaSource> remoteSource;
374
375     auto* rtcTrack = rtcReceiver->track().get();
376
377     switch (rtcReceiver->media_type()) {
378     case cricket::MEDIA_TYPE_DATA:
379         return;
380     case cricket::MEDIA_TYPE_AUDIO: {
381         rtc::scoped_refptr<webrtc::AudioTrackInterface> audioTrack = static_cast<webrtc::AudioTrackInterface*>(rtcTrack);
382         auto audioReceiver = m_peerConnectionBackend.audioReceiver(fromStdString(rtcTrack->id()));
383
384         receiver = WTFMove(audioReceiver.receiver);
385         audioReceiver.source->setSourceTrack(WTFMove(audioTrack));
386         break;
387     }
388     case cricket::MEDIA_TYPE_VIDEO: {
389         rtc::scoped_refptr<webrtc::VideoTrackInterface> videoTrack = static_cast<webrtc::VideoTrackInterface*>(rtcTrack);
390         auto videoReceiver = m_peerConnectionBackend.videoReceiver(fromStdString(rtcTrack->id()));
391
392         receiver = WTFMove(videoReceiver.receiver);
393         videoReceiver.source->setSourceTrack(WTFMove(videoTrack));
394         break;
395     }
396     }
397
398     receiver->setBackend(std::make_unique<LibWebRTCRtpReceiverBackend>(WTFMove(rtcReceiver)));
399     auto& track = receiver->track();
400     fireTrackEvent(receiver.releaseNonNull(), track, rtcStreams, nullptr);
401 }
402
403 void LibWebRTCMediaEndpoint::fireTrackEvent(Ref<RTCRtpReceiver>&& receiver, Ref<MediaStreamTrack>&& track, const std::vector<rtc::scoped_refptr<webrtc::MediaStreamInterface>>& rtcStreams, RefPtr<RTCRtpTransceiver>&& transceiver)
404 {
405     Vector<RefPtr<MediaStream>> streams;
406     for (auto& rtcStream : rtcStreams) {
407         auto& mediaStream = mediaStreamFromRTCStream(*rtcStream.get());
408         streams.append(&mediaStream);
409         mediaStream.addTrackFromPlatform(track.get());
410     }
411     auto streamIds = WTF::map(streams, [](auto& stream) -> String {
412         return stream->id();
413     });
414     m_remoteStreamsFromRemoteTrack.add(track.ptr(), WTFMove(streamIds));
415
416     m_peerConnectionBackend.connection().fireEvent(RTCTrackEvent::create(eventNames().trackEvent,
417         Event::CanBubble::No, Event::IsCancelable::No, WTFMove(receiver), WTFMove(track), WTFMove(streams), WTFMove(transceiver)));
418 }
419
420 static inline void setExistingReceiverSourceTrack(RealtimeMediaSource& existingSource, webrtc::RtpReceiverInterface& rtcReceiver)
421 {
422     switch (rtcReceiver.media_type()) {
423     case cricket::MEDIA_TYPE_AUDIO: {
424         ASSERT(existingSource.type() == RealtimeMediaSource::Type::Audio);
425         rtc::scoped_refptr<webrtc::AudioTrackInterface> audioTrack = static_cast<webrtc::AudioTrackInterface*>(rtcReceiver.track().get());
426         downcast<RealtimeIncomingAudioSource>(existingSource).setSourceTrack(WTFMove(audioTrack));
427         return;
428     }
429     case cricket::MEDIA_TYPE_VIDEO: {
430         ASSERT(existingSource.type() == RealtimeMediaSource::Type::Video);
431         rtc::scoped_refptr<webrtc::VideoTrackInterface> videoTrack = static_cast<webrtc::VideoTrackInterface*>(rtcReceiver.track().get());
432         downcast<RealtimeIncomingVideoSource>(existingSource).setSourceTrack(WTFMove(videoTrack));
433         return;
434     }
435     case cricket::MEDIA_TYPE_DATA:
436         ASSERT_NOT_REACHED();
437         return;
438     }
439 }
440
441 static inline RefPtr<RealtimeMediaSource> sourceFromNewReceiver(webrtc::RtpReceiverInterface& rtcReceiver)
442 {
443     auto rtcTrack = rtcReceiver.track();
444     switch (rtcReceiver.media_type()) {
445     case cricket::MEDIA_TYPE_DATA:
446         return nullptr;
447     case cricket::MEDIA_TYPE_AUDIO: {
448         rtc::scoped_refptr<webrtc::AudioTrackInterface> audioTrack = static_cast<webrtc::AudioTrackInterface*>(rtcTrack.get());
449         return RealtimeIncomingAudioSource::create(WTFMove(audioTrack), fromStdString(rtcTrack->id()));
450     }
451     case cricket::MEDIA_TYPE_VIDEO: {
452         rtc::scoped_refptr<webrtc::VideoTrackInterface> videoTrack = static_cast<webrtc::VideoTrackInterface*>(rtcTrack.get());
453         return RealtimeIncomingVideoSource::create(WTFMove(videoTrack), fromStdString(rtcTrack->id()));
454     }
455     }
456
457     RELEASE_ASSERT_NOT_REACHED();
458 }
459
460 void LibWebRTCMediaEndpoint::collectTransceivers()
461 {
462     if (!m_backend)
463         return;
464
465     if (!RuntimeEnabledFeatures::sharedFeatures().webRTCUnifiedPlanEnabled())
466         return;
467
468     for (auto& rtcTransceiver : m_backend->GetTransceivers()) {
469         auto* existingTransceiver = m_peerConnectionBackend.existingTransceiver([&](auto& transceiverBackend) {
470             return rtcTransceiver.get() == transceiverBackend.rtcTransceiver();
471         });
472         if (existingTransceiver)
473             continue;
474
475         auto rtcReceiver = rtcTransceiver->receiver();
476         auto source = sourceFromNewReceiver(*rtcReceiver);
477         if (!source)
478             return;
479
480         m_peerConnectionBackend.newRemoteTransceiver(std::make_unique<LibWebRTCRtpTransceiverBackend>(WTFMove(rtcTransceiver)), source.releaseNonNull());
481     }
482 }
483
484 void LibWebRTCMediaEndpoint::newTransceiver(rtc::scoped_refptr<webrtc::RtpTransceiverInterface>&& rtcTransceiver)
485 {
486     auto* transceiver = m_peerConnectionBackend.existingTransceiver([&](auto& transceiverBackend) {
487         return rtcTransceiver.get() == transceiverBackend.rtcTransceiver();
488     });
489     if (transceiver) {
490         auto rtcReceiver = rtcTransceiver->receiver();
491         setExistingReceiverSourceTrack(transceiver->receiver().track().source(), *rtcReceiver);
492         fireTrackEvent(makeRef(transceiver->receiver()), transceiver->receiver().track(), rtcReceiver->streams(), makeRef(*transceiver));
493         return;
494     }
495
496     auto rtcReceiver = rtcTransceiver->receiver();
497     auto source = sourceFromNewReceiver(*rtcReceiver);
498     if (!source)
499         return;
500
501     auto& newTransceiver = m_peerConnectionBackend.newRemoteTransceiver(std::make_unique<LibWebRTCRtpTransceiverBackend>(WTFMove(rtcTransceiver)), source.releaseNonNull());
502
503     fireTrackEvent(makeRef(newTransceiver.receiver()), newTransceiver.receiver().track(), rtcReceiver->streams(), makeRef(newTransceiver));
504 }
505
506 void LibWebRTCMediaEndpoint::removeRemoteTrack(rtc::scoped_refptr<webrtc::RtpReceiverInterface>&& receiver)
507 {
508     // FIXME: Support plan B code path.
509     if (!RuntimeEnabledFeatures::sharedFeatures().webRTCUnifiedPlanEnabled())
510         return;
511
512     auto* transceiver = m_peerConnectionBackend.existingTransceiver([&receiver](auto& transceiverBackend) {
513         auto* rtcTransceiver = transceiverBackend.rtcTransceiver();
514         return rtcTransceiver && receiver.get() == rtcTransceiver->receiver().get();
515     });
516     if (!transceiver)
517         return;
518
519     auto& track = transceiver->receiver().track();
520
521     for (auto& id : m_remoteStreamsFromRemoteTrack.get(&track)) {
522         if (auto stream = m_remoteStreamsById.get(id))
523             stream->privateStream().removeTrack(track.privateTrack(), MediaStreamPrivate::NotifyClientOption::Notify);
524     }
525
526     track.source().setMuted(true);
527 }
528
529 template<typename T>
530 std::optional<LibWebRTCMediaEndpoint::Backends> LibWebRTCMediaEndpoint::createTransceiverBackends(T&& trackOrKind, const RTCRtpTransceiverInit& init, LibWebRTCRtpSenderBackend::Source&& source)
531 {
532     auto result = m_backend->AddTransceiver(WTFMove(trackOrKind), fromRtpTransceiverInit(init));
533     if (!result.ok())
534         return std::nullopt;
535
536     auto transceiver = std::make_unique<LibWebRTCRtpTransceiverBackend>(result.MoveValue());
537     return LibWebRTCMediaEndpoint::Backends { transceiver->createSenderBackend(m_peerConnectionBackend, WTFMove(source)), transceiver->createReceiverBackend(), WTFMove(transceiver) };
538 }
539
540 std::optional<LibWebRTCMediaEndpoint::Backends> LibWebRTCMediaEndpoint::addTransceiver(const String& trackKind, const RTCRtpTransceiverInit& init)
541 {
542     auto type = trackKind == "audio" ? cricket::MediaType::MEDIA_TYPE_AUDIO : cricket::MediaType::MEDIA_TYPE_VIDEO;
543     return createTransceiverBackends(type, init, nullptr);
544 }
545
546 std::pair<LibWebRTCRtpSenderBackend::Source, rtc::scoped_refptr<webrtc::MediaStreamTrackInterface>> LibWebRTCMediaEndpoint::createSourceAndRTCTrack(MediaStreamTrack& track)
547 {
548     LibWebRTCRtpSenderBackend::Source source;
549     rtc::scoped_refptr<webrtc::MediaStreamTrackInterface> rtcTrack;
550     switch (track.privateTrack().type()) {
551     case RealtimeMediaSource::Type::None:
552         ASSERT_NOT_REACHED();
553         break;
554     case RealtimeMediaSource::Type::Audio: {
555         auto audioSource = RealtimeOutgoingAudioSource::create(track.privateTrack());
556         rtcTrack = m_peerConnectionFactory.CreateAudioTrack(track.id().utf8().data(), audioSource.ptr());
557         source = WTFMove(audioSource);
558         break;
559     }
560     case RealtimeMediaSource::Type::Video: {
561         auto videoSource = RealtimeOutgoingVideoSource::create(track.privateTrack());
562         rtcTrack = m_peerConnectionFactory.CreateVideoTrack(track.id().utf8().data(), videoSource.ptr());
563         source = WTFMove(videoSource);
564         break;
565     }
566     }
567     return std::make_pair(WTFMove(source), WTFMove(rtcTrack));
568 }
569
570 std::optional<LibWebRTCMediaEndpoint::Backends> LibWebRTCMediaEndpoint::addTransceiver(MediaStreamTrack& track, const RTCRtpTransceiverInit& init)
571 {
572     auto sourceAndTrack = createSourceAndRTCTrack(track);
573     return createTransceiverBackends(WTFMove(sourceAndTrack.second), init, WTFMove(sourceAndTrack.first));
574 }
575
576 void LibWebRTCMediaEndpoint::setSenderSourceFromTrack(LibWebRTCRtpSenderBackend& sender, MediaStreamTrack& track)
577 {
578     auto sourceAndTrack = createSourceAndRTCTrack(track);
579     sender.setSource(WTFMove(sourceAndTrack.first));
580     sender.rtcSender()->SetTrack(WTFMove(sourceAndTrack.second));
581 }
582
583 std::unique_ptr<LibWebRTCRtpTransceiverBackend> LibWebRTCMediaEndpoint::transceiverBackendFromSender(LibWebRTCRtpSenderBackend& backend)
584 {
585     for (auto& transceiver : m_backend->GetTransceivers()) {
586         if (transceiver->sender().get() == backend.rtcSender())
587             return std::make_unique<LibWebRTCRtpTransceiverBackend>(rtc::scoped_refptr<webrtc::RtpTransceiverInterface>(transceiver));
588     }
589     return nullptr;
590 }
591
592
593 void LibWebRTCMediaEndpoint::removeRemoteStream(webrtc::MediaStreamInterface& rtcStream)
594 {
595     bool removed = m_remoteStreamsById.remove(fromStdString(rtcStream.id()));
596     ASSERT_UNUSED(removed, removed);
597 }
598
599 void LibWebRTCMediaEndpoint::OnAddStream(rtc::scoped_refptr<webrtc::MediaStreamInterface> stream)
600 {
601     callOnMainThread([protectedThis = makeRef(*this), stream = WTFMove(stream)] {
602         if (protectedThis->isStopped())
603             return;
604         ASSERT(stream);
605         protectedThis->addRemoteStream(*stream.get());
606     });
607 }
608
609 void LibWebRTCMediaEndpoint::OnRemoveStream(rtc::scoped_refptr<webrtc::MediaStreamInterface> stream)
610 {
611     callOnMainThread([protectedThis = makeRef(*this), stream = WTFMove(stream)] {
612         if (protectedThis->isStopped())
613             return;
614         ASSERT(stream);
615         protectedThis->removeRemoteStream(*stream.get());
616     });
617 }
618
619 void LibWebRTCMediaEndpoint::OnAddTrack(rtc::scoped_refptr<webrtc::RtpReceiverInterface> receiver, const std::vector<rtc::scoped_refptr<webrtc::MediaStreamInterface>>& streams)
620 {
621     if (RuntimeEnabledFeatures::sharedFeatures().webRTCUnifiedPlanEnabled())
622         return;
623
624     callOnMainThread([protectedThis = makeRef(*this), receiver = WTFMove(receiver), streams]() mutable {
625         if (protectedThis->isStopped())
626             return;
627         protectedThis->addRemoteTrack(WTFMove(receiver), streams);
628     });
629 }
630
631 void LibWebRTCMediaEndpoint::OnTrack(rtc::scoped_refptr<webrtc::RtpTransceiverInterface> transceiver)
632 {
633     if (!RuntimeEnabledFeatures::sharedFeatures().webRTCUnifiedPlanEnabled())
634         return;
635
636     callOnMainThread([protectedThis = makeRef(*this), transceiver = WTFMove(transceiver)]() mutable {
637         if (protectedThis->isStopped())
638             return;
639         protectedThis->newTransceiver(WTFMove(transceiver));
640     });
641 }
642
643 void LibWebRTCMediaEndpoint::OnRemoveTrack(rtc::scoped_refptr<webrtc::RtpReceiverInterface> receiver)
644 {
645     callOnMainThread([protectedThis = makeRef(*this), receiver = WTFMove(receiver)]() mutable {
646         if (protectedThis->isStopped())
647             return;
648         protectedThis->removeRemoteTrack(WTFMove(receiver));
649     });
650 }
651
652 std::unique_ptr<RTCDataChannelHandler> LibWebRTCMediaEndpoint::createDataChannel(const String& label, const RTCDataChannelInit& options)
653 {
654     auto init = LibWebRTCDataChannelHandler::fromRTCDataChannelInit(options);
655     auto channel = m_backend->CreateDataChannel(label.utf8().data(), &init);
656     return channel ? std::make_unique<LibWebRTCDataChannelHandler>(WTFMove(channel)) : nullptr;
657 }
658
659 void LibWebRTCMediaEndpoint::OnDataChannel(rtc::scoped_refptr<webrtc::DataChannelInterface> dataChannel)
660 {
661     callOnMainThread([protectedThis = makeRef(*this), dataChannel = WTFMove(dataChannel)]() mutable {
662         if (protectedThis->isStopped())
663             return;
664         auto& connection = protectedThis->m_peerConnectionBackend.connection();
665         connection.fireEvent(LibWebRTCDataChannelHandler::channelEvent(*connection.scriptExecutionContext(), WTFMove(dataChannel)));
666     });
667 }
668
669 void LibWebRTCMediaEndpoint::stop()
670 {
671     if (!m_backend)
672         return;
673
674     stopLoggingStats();
675
676     m_backend->Close();
677     m_backend = nullptr;
678     m_remoteStreamsById.clear();
679     m_remoteStreamsFromRemoteTrack.clear();
680 }
681
682 void LibWebRTCMediaEndpoint::OnRenegotiationNeeded()
683 {
684     callOnMainThread([protectedThis = makeRef(*this)] {
685         if (protectedThis->isStopped())
686             return;
687         protectedThis->m_peerConnectionBackend.markAsNeedingNegotiation();
688     });
689 }
690
691 static inline RTCIceConnectionState toRTCIceConnectionState(webrtc::PeerConnectionInterface::IceConnectionState state)
692 {
693     switch (state) {
694     case webrtc::PeerConnectionInterface::kIceConnectionNew:
695         return RTCIceConnectionState::New;
696     case webrtc::PeerConnectionInterface::kIceConnectionChecking:
697         return RTCIceConnectionState::Checking;
698     case webrtc::PeerConnectionInterface::kIceConnectionConnected:
699         return RTCIceConnectionState::Connected;
700     case webrtc::PeerConnectionInterface::kIceConnectionCompleted:
701         return RTCIceConnectionState::Completed;
702     case webrtc::PeerConnectionInterface::kIceConnectionFailed:
703         return RTCIceConnectionState::Failed;
704     case webrtc::PeerConnectionInterface::kIceConnectionDisconnected:
705         return RTCIceConnectionState::Disconnected;
706     case webrtc::PeerConnectionInterface::kIceConnectionClosed:
707         return RTCIceConnectionState::Closed;
708     case webrtc::PeerConnectionInterface::kIceConnectionMax:
709         break;
710     }
711
712     ASSERT_NOT_REACHED();
713     return RTCIceConnectionState::New;
714 }
715
716 void LibWebRTCMediaEndpoint::OnIceConnectionChange(webrtc::PeerConnectionInterface::IceConnectionState state)
717 {
718     auto connectionState = toRTCIceConnectionState(state);
719     callOnMainThread([protectedThis = makeRef(*this), connectionState] {
720         if (protectedThis->isStopped())
721             return;
722         if (protectedThis->m_peerConnectionBackend.connection().iceConnectionState() != connectionState)
723             protectedThis->m_peerConnectionBackend.connection().updateIceConnectionState(connectionState);
724     });
725 }
726
727 void LibWebRTCMediaEndpoint::OnIceGatheringChange(webrtc::PeerConnectionInterface::IceGatheringState state)
728 {
729     callOnMainThread([protectedThis = makeRef(*this), state] {
730         if (protectedThis->isStopped())
731             return;
732         if (state == webrtc::PeerConnectionInterface::kIceGatheringComplete)
733             protectedThis->m_peerConnectionBackend.doneGatheringCandidates();
734         else if (state == webrtc::PeerConnectionInterface::kIceGatheringGathering)
735             protectedThis->m_peerConnectionBackend.connection().updateIceGatheringState(RTCIceGatheringState::Gathering);
736     });
737 }
738
739 void LibWebRTCMediaEndpoint::OnIceCandidate(const webrtc::IceCandidateInterface *rtcCandidate)
740 {
741     ASSERT(rtcCandidate);
742
743     std::string sdp;
744     rtcCandidate->ToString(&sdp);
745
746     auto sdpMLineIndex = safeCast<unsigned short>(rtcCandidate->sdp_mline_index());
747
748     callOnMainThread([protectedThis = makeRef(*this), mid = fromStdString(rtcCandidate->sdp_mid()), sdp = fromStdString(sdp), sdpMLineIndex, url = fromStdString(rtcCandidate->server_url())]() mutable {
749         if (protectedThis->isStopped())
750             return;
751         protectedThis->m_peerConnectionBackend.newICECandidate(WTFMove(sdp), WTFMove(mid), sdpMLineIndex, WTFMove(url));
752     });
753 }
754
755 void LibWebRTCMediaEndpoint::OnIceCandidatesRemoved(const std::vector<cricket::Candidate>&)
756 {
757     ASSERT_NOT_REACHED();
758 }
759
760 void LibWebRTCMediaEndpoint::createSessionDescriptionSucceeded(std::unique_ptr<webrtc::SessionDescriptionInterface>&& description)
761 {
762     std::string sdp;
763     description->ToString(&sdp);
764
765     callOnMainThread([protectedThis = makeRef(*this), sdp = fromStdString(sdp)]() mutable {
766         if (protectedThis->isStopped())
767             return;
768         if (protectedThis->m_isInitiator)
769             protectedThis->m_peerConnectionBackend.createOfferSucceeded(WTFMove(sdp));
770         else
771             protectedThis->m_peerConnectionBackend.createAnswerSucceeded(WTFMove(sdp));
772     });
773 }
774
775 void LibWebRTCMediaEndpoint::createSessionDescriptionFailed(ExceptionCode errorCode, const char* errorMessage)
776 {
777     callOnMainThread([protectedThis = makeRef(*this), errorCode, errorMessage = String(errorMessage)] () mutable {
778         if (protectedThis->isStopped())
779             return;
780         if (protectedThis->m_isInitiator)
781             protectedThis->m_peerConnectionBackend.createOfferFailed(Exception { errorCode, WTFMove(errorMessage) });
782         else
783             protectedThis->m_peerConnectionBackend.createAnswerFailed(Exception { errorCode, WTFMove(errorMessage) });
784     });
785 }
786
787 void LibWebRTCMediaEndpoint::setLocalSessionDescriptionSucceeded()
788 {
789     callOnMainThread([protectedThis = makeRef(*this)] {
790         if (protectedThis->isStopped())
791             return;
792         protectedThis->m_peerConnectionBackend.setLocalDescriptionSucceeded();
793     });
794 }
795
796 void LibWebRTCMediaEndpoint::setLocalSessionDescriptionFailed(ExceptionCode errorCode, const char* errorMessage)
797 {
798     callOnMainThread([protectedThis = makeRef(*this), errorCode, errorMessage = String(errorMessage)] () mutable {
799         if (protectedThis->isStopped())
800             return;
801         protectedThis->m_peerConnectionBackend.setLocalDescriptionFailed(Exception { errorCode, WTFMove(errorMessage) });
802     });
803 }
804
805 void LibWebRTCMediaEndpoint::setRemoteSessionDescriptionSucceeded()
806 {
807     callOnMainThread([protectedThis = makeRef(*this)] {
808         if (protectedThis->isStopped())
809             return;
810         protectedThis->m_peerConnectionBackend.setRemoteDescriptionSucceeded();
811     });
812 }
813
814 void LibWebRTCMediaEndpoint::setRemoteSessionDescriptionFailed(ExceptionCode errorCode, const char* errorMessage)
815 {
816     callOnMainThread([protectedThis = makeRef(*this), errorCode, errorMessage = String(errorMessage)] () mutable {
817         if (protectedThis->isStopped())
818             return;
819         protectedThis->m_peerConnectionBackend.setRemoteDescriptionFailed(Exception { errorCode, WTFMove(errorMessage) });
820     });
821 }
822
823 void LibWebRTCMediaEndpoint::gatherStatsForLogging()
824 {
825     LibWebRTCProvider::callOnWebRTCSignalingThread([protectedThis = makeRef(*this)] {
826         if (protectedThis->m_backend)
827             protectedThis->m_backend->GetStats(protectedThis.ptr());
828     });
829 }
830
831 class RTCStatsLogger {
832 public:
833     explicit RTCStatsLogger(const webrtc::RTCStats& stats)
834         : m_stats(stats)
835     {
836     }
837
838     String toJSONString() const { return String(m_stats.ToJson().c_str()); }
839
840 private:
841     const webrtc::RTCStats& m_stats;
842 };
843
844 void LibWebRTCMediaEndpoint::OnStatsDelivered(const rtc::scoped_refptr<const webrtc::RTCStatsReport>& report)
845 {
846 #if !RELEASE_LOG_DISABLED
847     int64_t timestamp = report->timestamp_us();
848     if (!m_statsFirstDeliveredTimestamp)
849         m_statsFirstDeliveredTimestamp = timestamp;
850
851     callOnMainThread([protectedThis = makeRef(*this), this, timestamp, report] {
852         if (m_statsLogTimer.repeatInterval() != statsLogInterval(timestamp)) {
853             m_statsLogTimer.stop();
854             m_statsLogTimer.startRepeating(statsLogInterval(timestamp));
855         }
856
857         for (auto iterator = report->begin(); iterator != report->end(); ++iterator)
858             ALWAYS_LOG(Logger::LogSiteIdentifier("LibWebRTCMediaEndpoint", "OnStatsDelivered", logIdentifier()), RTCStatsLogger { *iterator });
859     });
860 #else
861     UNUSED_PARAM(report);
862 #endif
863 }
864
865 void LibWebRTCMediaEndpoint::startLoggingStats()
866 {
867 #if !RELEASE_LOG_DISABLED
868     if (m_statsLogTimer.isActive())
869         m_statsLogTimer.stop();
870     m_statsLogTimer.startRepeating(statsLogInterval(0));
871 #endif
872 }
873
874 void LibWebRTCMediaEndpoint::stopLoggingStats()
875 {
876     m_statsLogTimer.stop();
877 }
878
879 #if !RELEASE_LOG_DISABLED
880 WTFLogChannel& LibWebRTCMediaEndpoint::logChannel() const
881 {
882     return LogWebRTC;
883 }
884
885 Seconds LibWebRTCMediaEndpoint::statsLogInterval(int64_t reportTimestamp) const
886 {
887     if (logger().willLog(logChannel(), WTFLogLevelInfo))
888         return 2_s;
889
890     if (reportTimestamp - m_statsFirstDeliveredTimestamp > 15000000)
891         return 10_s;
892
893     return 4_s;
894 }
895 #endif
896
897 } // namespace WebCore
898
899 namespace WTF {
900
901 template<typename Type>
902 struct LogArgument;
903
904 template <>
905 struct LogArgument<WebCore::RTCStatsLogger> {
906     static String toString(const WebCore::RTCStatsLogger& logger)
907     {
908         return String(logger.toJSONString());
909     }
910 };
911
912 }; // namespace WTF
913
914
915 #endif // USE(LIBWEBRTC)