Add media stream release logging
[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     sender.clearSource();
259 }
260
261 void LibWebRTCMediaEndpoint::doCreateOffer(const RTCOfferOptions& options)
262 {
263     ASSERT(m_backend);
264
265     m_isInitiator = true;
266     webrtc::PeerConnectionInterface::RTCOfferAnswerOptions rtcOptions;
267     rtcOptions.ice_restart = options.iceRestart;
268     rtcOptions.voice_activity_detection = options.voiceActivityDetection;
269
270     if (!RuntimeEnabledFeatures::sharedFeatures().webRTCUnifiedPlanEnabled()) {
271         if (m_peerConnectionBackend.shouldOfferAllowToReceive("audio"_s))
272             rtcOptions.offer_to_receive_audio = webrtc::PeerConnectionInterface::RTCOfferAnswerOptions::kOfferToReceiveMediaTrue;
273         if (m_peerConnectionBackend.shouldOfferAllowToReceive("video"_s))
274             rtcOptions.offer_to_receive_video = webrtc::PeerConnectionInterface::RTCOfferAnswerOptions::kOfferToReceiveMediaTrue;
275     }
276     m_backend->CreateOffer(&m_createSessionDescriptionObserver, rtcOptions);
277 }
278
279 void LibWebRTCMediaEndpoint::doCreateAnswer()
280 {
281     ASSERT(m_backend);
282
283     m_isInitiator = false;
284     m_backend->CreateAnswer(&m_createSessionDescriptionObserver, { });
285 }
286
287 void LibWebRTCMediaEndpoint::getStats(Ref<DeferredPromise>&& promise, WTF::Function<void(rtc::scoped_refptr<LibWebRTCStatsCollector>&&)>&& getStatsFunction)
288 {
289     auto collector = LibWebRTCStatsCollector::create([promise = WTFMove(promise), protectedThis = makeRef(*this)]() mutable -> RefPtr<RTCStatsReport> {
290         ASSERT(isMainThread());
291         if (protectedThis->isStopped())
292             return nullptr;
293
294         auto report = RTCStatsReport::create();
295
296         promise->resolve<IDLInterface<RTCStatsReport>>(report.copyRef());
297
298         // The promise resolution might fail in which case no backing map will be created.
299         if (!report->backingMap())
300             return nullptr;
301         return WTFMove(report);
302     });
303     LibWebRTCProvider::callOnWebRTCSignalingThread([getStatsFunction = WTFMove(getStatsFunction), collector = WTFMove(collector)]() mutable {
304         getStatsFunction(WTFMove(collector));
305     });
306 }
307
308 void LibWebRTCMediaEndpoint::getStats(Ref<DeferredPromise>&& promise)
309 {
310     getStats(WTFMove(promise), [this](auto&& collector) {
311         if (m_backend)
312             m_backend->GetStats(WTFMove(collector));
313     });
314 }
315
316 void LibWebRTCMediaEndpoint::getStats(webrtc::RtpReceiverInterface& receiver, Ref<DeferredPromise>&& promise)
317 {
318     getStats(WTFMove(promise), [this, receiver = rtc::scoped_refptr<webrtc::RtpReceiverInterface>(&receiver)](auto&& collector) mutable {
319         if (m_backend)
320             m_backend->GetStats(WTFMove(receiver), WTFMove(collector));
321     });
322 }
323
324 void LibWebRTCMediaEndpoint::getStats(webrtc::RtpSenderInterface& sender, Ref<DeferredPromise>&& promise)
325 {
326     getStats(WTFMove(promise), [this, sender = rtc::scoped_refptr<webrtc::RtpSenderInterface>(&sender)](auto&& collector)  mutable {
327         if (m_backend)
328             m_backend->GetStats(WTFMove(sender), WTFMove(collector));
329     });
330 }
331
332 static RTCSignalingState signalingState(webrtc::PeerConnectionInterface::SignalingState state)
333 {
334     switch (state) {
335     case webrtc::PeerConnectionInterface::kStable:
336         return RTCSignalingState::Stable;
337     case webrtc::PeerConnectionInterface::kHaveLocalOffer:
338         return RTCSignalingState::HaveLocalOffer;
339     case webrtc::PeerConnectionInterface::kHaveLocalPrAnswer:
340         return RTCSignalingState::HaveLocalPranswer;
341     case webrtc::PeerConnectionInterface::kHaveRemoteOffer:
342         return RTCSignalingState::HaveRemoteOffer;
343     case webrtc::PeerConnectionInterface::kHaveRemotePrAnswer:
344         return RTCSignalingState::HaveRemotePranswer;
345     case webrtc::PeerConnectionInterface::kClosed:
346         return RTCSignalingState::Stable;
347     }
348
349     ASSERT_NOT_REACHED();
350     return RTCSignalingState::Stable;
351 }
352
353 void LibWebRTCMediaEndpoint::OnSignalingChange(webrtc::PeerConnectionInterface::SignalingState rtcState)
354 {
355     auto state = signalingState(rtcState);
356     callOnMainThread([protectedThis = makeRef(*this), state] {
357         if (protectedThis->isStopped())
358             return;
359         protectedThis->m_peerConnectionBackend.updateSignalingState(state);
360     });
361 }
362
363 MediaStream& LibWebRTCMediaEndpoint::mediaStreamFromRTCStream(webrtc::MediaStreamInterface& rtcStream)
364 {
365     auto label = fromStdString(rtcStream.id());
366     auto mediaStream = m_remoteStreamsById.ensure(label, [label, this]() mutable {
367         return MediaStream::create(*m_peerConnectionBackend.connection().scriptExecutionContext(), MediaStreamPrivate::create({ }, WTFMove(label)));
368     });
369     return *mediaStream.iterator->value;
370 }
371
372 void LibWebRTCMediaEndpoint::addRemoteStream(webrtc::MediaStreamInterface&)
373 {
374 }
375
376 void LibWebRTCMediaEndpoint::addRemoteTrack(rtc::scoped_refptr<webrtc::RtpReceiverInterface>&& rtcReceiver, const std::vector<rtc::scoped_refptr<webrtc::MediaStreamInterface>>& rtcStreams)
377 {
378     ASSERT(rtcReceiver);
379     RefPtr<RTCRtpReceiver> receiver;
380     RefPtr<RealtimeMediaSource> remoteSource;
381
382     auto* rtcTrack = rtcReceiver->track().get();
383
384     switch (rtcReceiver->media_type()) {
385     case cricket::MEDIA_TYPE_DATA:
386         return;
387     case cricket::MEDIA_TYPE_AUDIO: {
388         rtc::scoped_refptr<webrtc::AudioTrackInterface> audioTrack = static_cast<webrtc::AudioTrackInterface*>(rtcTrack);
389         auto audioReceiver = m_peerConnectionBackend.audioReceiver(fromStdString(rtcTrack->id()));
390
391         receiver = WTFMove(audioReceiver.receiver);
392         audioReceiver.source->setSourceTrack(WTFMove(audioTrack));
393         break;
394     }
395     case cricket::MEDIA_TYPE_VIDEO: {
396         rtc::scoped_refptr<webrtc::VideoTrackInterface> videoTrack = static_cast<webrtc::VideoTrackInterface*>(rtcTrack);
397         auto videoReceiver = m_peerConnectionBackend.videoReceiver(fromStdString(rtcTrack->id()));
398
399         receiver = WTFMove(videoReceiver.receiver);
400         videoReceiver.source->setSourceTrack(WTFMove(videoTrack));
401         break;
402     }
403     }
404
405     receiver->setBackend(std::make_unique<LibWebRTCRtpReceiverBackend>(WTFMove(rtcReceiver)));
406     auto& track = receiver->track();
407     fireTrackEvent(receiver.releaseNonNull(), track, rtcStreams, nullptr);
408 }
409
410 void LibWebRTCMediaEndpoint::fireTrackEvent(Ref<RTCRtpReceiver>&& receiver, MediaStreamTrack& track, const std::vector<rtc::scoped_refptr<webrtc::MediaStreamInterface>>& rtcStreams, RefPtr<RTCRtpTransceiver>&& transceiver)
411 {
412     Vector<RefPtr<MediaStream>> streams;
413     for (auto& rtcStream : rtcStreams) {
414         auto& mediaStream = mediaStreamFromRTCStream(*rtcStream.get());
415         streams.append(&mediaStream);
416         mediaStream.addTrackFromPlatform(track);
417     }
418     auto streamIds = WTF::map(streams, [](auto& stream) -> String {
419         return stream->id();
420     });
421     m_remoteStreamsFromRemoteTrack.add(&track, WTFMove(streamIds));
422
423     m_peerConnectionBackend.connection().fireEvent(RTCTrackEvent::create(eventNames().trackEvent,
424         Event::CanBubble::No, Event::IsCancelable::No, WTFMove(receiver), &track, WTFMove(streams), WTFMove(transceiver)));
425
426     // FIXME: As per spec, we should set muted to 'false' when starting to receive the content from network.
427     track.source().setMuted(false);
428 }
429
430 static inline void setExistingReceiverSourceTrack(RealtimeMediaSource& existingSource, webrtc::RtpReceiverInterface& rtcReceiver)
431 {
432     switch (rtcReceiver.media_type()) {
433     case cricket::MEDIA_TYPE_AUDIO: {
434         ASSERT(existingSource.type() == RealtimeMediaSource::Type::Audio);
435         rtc::scoped_refptr<webrtc::AudioTrackInterface> audioTrack = static_cast<webrtc::AudioTrackInterface*>(rtcReceiver.track().get());
436         downcast<RealtimeIncomingAudioSource>(existingSource).setSourceTrack(WTFMove(audioTrack));
437         return;
438     }
439     case cricket::MEDIA_TYPE_VIDEO: {
440         ASSERT(existingSource.type() == RealtimeMediaSource::Type::Video);
441         rtc::scoped_refptr<webrtc::VideoTrackInterface> videoTrack = static_cast<webrtc::VideoTrackInterface*>(rtcReceiver.track().get());
442         downcast<RealtimeIncomingVideoSource>(existingSource).setSourceTrack(WTFMove(videoTrack));
443         return;
444     }
445     case cricket::MEDIA_TYPE_DATA:
446         ASSERT_NOT_REACHED();
447         return;
448     }
449 }
450
451 RefPtr<RealtimeMediaSource> LibWebRTCMediaEndpoint::sourceFromNewReceiver(webrtc::RtpReceiverInterface& rtcReceiver)
452 {
453     auto rtcTrack = rtcReceiver.track();
454     switch (rtcReceiver.media_type()) {
455     case cricket::MEDIA_TYPE_DATA:
456         return nullptr;
457     case cricket::MEDIA_TYPE_AUDIO: {
458         rtc::scoped_refptr<webrtc::AudioTrackInterface> audioTrack = static_cast<webrtc::AudioTrackInterface*>(rtcTrack.get());
459         return RealtimeIncomingAudioSource::create(WTFMove(audioTrack), fromStdString(rtcTrack->id()));
460     }
461     case cricket::MEDIA_TYPE_VIDEO: {
462         rtc::scoped_refptr<webrtc::VideoTrackInterface> videoTrack = static_cast<webrtc::VideoTrackInterface*>(rtcTrack.get());
463         return RealtimeIncomingVideoSource::create(WTFMove(videoTrack), fromStdString(rtcTrack->id()));
464     }
465     }
466
467     RELEASE_ASSERT_NOT_REACHED();
468 }
469
470 void LibWebRTCMediaEndpoint::collectTransceivers()
471 {
472     if (!m_backend)
473         return;
474
475     if (!RuntimeEnabledFeatures::sharedFeatures().webRTCUnifiedPlanEnabled())
476         return;
477
478     for (auto& rtcTransceiver : m_backend->GetTransceivers()) {
479         auto* existingTransceiver = m_peerConnectionBackend.existingTransceiver([&](auto& transceiverBackend) {
480             return rtcTransceiver.get() == transceiverBackend.rtcTransceiver();
481         });
482         if (existingTransceiver)
483             continue;
484
485         auto rtcReceiver = rtcTransceiver->receiver();
486         auto source = sourceFromNewReceiver(*rtcReceiver);
487         if (!source)
488             return;
489
490         m_peerConnectionBackend.newRemoteTransceiver(std::make_unique<LibWebRTCRtpTransceiverBackend>(WTFMove(rtcTransceiver)), source.releaseNonNull());
491     }
492 }
493
494 void LibWebRTCMediaEndpoint::newTransceiver(rtc::scoped_refptr<webrtc::RtpTransceiverInterface>&& rtcTransceiver)
495 {
496     auto* transceiver = m_peerConnectionBackend.existingTransceiver([&](auto& transceiverBackend) {
497         return rtcTransceiver.get() == transceiverBackend.rtcTransceiver();
498     });
499     if (transceiver) {
500         auto rtcReceiver = rtcTransceiver->receiver();
501         setExistingReceiverSourceTrack(transceiver->receiver().track().source(), *rtcReceiver);
502         fireTrackEvent(makeRef(transceiver->receiver()), transceiver->receiver().track(), rtcReceiver->streams(), makeRef(*transceiver));
503         return;
504     }
505
506     auto rtcReceiver = rtcTransceiver->receiver();
507     auto source = sourceFromNewReceiver(*rtcReceiver);
508     if (!source)
509         return;
510
511     auto& newTransceiver = m_peerConnectionBackend.newRemoteTransceiver(std::make_unique<LibWebRTCRtpTransceiverBackend>(WTFMove(rtcTransceiver)), source.releaseNonNull());
512
513     fireTrackEvent(makeRef(newTransceiver.receiver()), newTransceiver.receiver().track(), rtcReceiver->streams(), makeRef(newTransceiver));
514 }
515
516 void LibWebRTCMediaEndpoint::removeRemoteTrack(rtc::scoped_refptr<webrtc::RtpReceiverInterface>&& receiver)
517 {
518     // FIXME: Support plan B code path.
519     if (!RuntimeEnabledFeatures::sharedFeatures().webRTCUnifiedPlanEnabled())
520         return;
521
522     auto* transceiver = m_peerConnectionBackend.existingTransceiver([&receiver](auto& transceiverBackend) {
523         auto* rtcTransceiver = transceiverBackend.rtcTransceiver();
524         return rtcTransceiver && receiver.get() == rtcTransceiver->receiver().get();
525     });
526     if (!transceiver)
527         return;
528
529     auto& track = transceiver->receiver().track();
530
531     for (auto& id : m_remoteStreamsFromRemoteTrack.get(&track)) {
532         if (auto stream = m_remoteStreamsById.get(id))
533             stream->privateStream().removeTrack(track.privateTrack(), MediaStreamPrivate::NotifyClientOption::Notify);
534     }
535
536     track.source().setMuted(true);
537 }
538
539 template<typename T>
540 Optional<LibWebRTCMediaEndpoint::Backends> LibWebRTCMediaEndpoint::createTransceiverBackends(T&& trackOrKind, const RTCRtpTransceiverInit& init, LibWebRTCRtpSenderBackend::Source&& source)
541 {
542     auto result = m_backend->AddTransceiver(WTFMove(trackOrKind), fromRtpTransceiverInit(init));
543     if (!result.ok())
544         return WTF::nullopt;
545
546     auto transceiver = std::make_unique<LibWebRTCRtpTransceiverBackend>(result.MoveValue());
547     return LibWebRTCMediaEndpoint::Backends { transceiver->createSenderBackend(m_peerConnectionBackend, WTFMove(source)), transceiver->createReceiverBackend(), WTFMove(transceiver) };
548 }
549
550 Optional<LibWebRTCMediaEndpoint::Backends> LibWebRTCMediaEndpoint::addTransceiver(const String& trackKind, const RTCRtpTransceiverInit& init)
551 {
552     auto type = trackKind == "audio" ? cricket::MediaType::MEDIA_TYPE_AUDIO : cricket::MediaType::MEDIA_TYPE_VIDEO;
553     return createTransceiverBackends(type, init, nullptr);
554 }
555
556 std::pair<LibWebRTCRtpSenderBackend::Source, rtc::scoped_refptr<webrtc::MediaStreamTrackInterface>> LibWebRTCMediaEndpoint::createSourceAndRTCTrack(MediaStreamTrack& track)
557 {
558     LibWebRTCRtpSenderBackend::Source source;
559     rtc::scoped_refptr<webrtc::MediaStreamTrackInterface> rtcTrack;
560     switch (track.privateTrack().type()) {
561     case RealtimeMediaSource::Type::None:
562         ASSERT_NOT_REACHED();
563         break;
564     case RealtimeMediaSource::Type::Audio: {
565         auto audioSource = RealtimeOutgoingAudioSource::create(track.privateTrack());
566         rtcTrack = m_peerConnectionFactory.CreateAudioTrack(track.id().utf8().data(), audioSource.ptr());
567         source = WTFMove(audioSource);
568         break;
569     }
570     case RealtimeMediaSource::Type::Video: {
571         auto videoSource = RealtimeOutgoingVideoSource::create(track.privateTrack());
572         rtcTrack = m_peerConnectionFactory.CreateVideoTrack(track.id().utf8().data(), videoSource.ptr());
573         source = WTFMove(videoSource);
574         break;
575     }
576     }
577     return std::make_pair(WTFMove(source), WTFMove(rtcTrack));
578 }
579
580 Optional<LibWebRTCMediaEndpoint::Backends> LibWebRTCMediaEndpoint::addTransceiver(MediaStreamTrack& track, const RTCRtpTransceiverInit& init)
581 {
582     auto sourceAndTrack = createSourceAndRTCTrack(track);
583     return createTransceiverBackends(WTFMove(sourceAndTrack.second), init, WTFMove(sourceAndTrack.first));
584 }
585
586 void LibWebRTCMediaEndpoint::setSenderSourceFromTrack(LibWebRTCRtpSenderBackend& sender, MediaStreamTrack& track)
587 {
588     auto sourceAndTrack = createSourceAndRTCTrack(track);
589     sender.setSource(WTFMove(sourceAndTrack.first));
590     sender.rtcSender()->SetTrack(WTFMove(sourceAndTrack.second));
591 }
592
593 std::unique_ptr<LibWebRTCRtpTransceiverBackend> LibWebRTCMediaEndpoint::transceiverBackendFromSender(LibWebRTCRtpSenderBackend& backend)
594 {
595     for (auto& transceiver : m_backend->GetTransceivers()) {
596         if (transceiver->sender().get() == backend.rtcSender())
597             return std::make_unique<LibWebRTCRtpTransceiverBackend>(rtc::scoped_refptr<webrtc::RtpTransceiverInterface>(transceiver));
598     }
599     return nullptr;
600 }
601
602
603 void LibWebRTCMediaEndpoint::removeRemoteStream(webrtc::MediaStreamInterface& rtcStream)
604 {
605     bool removed = m_remoteStreamsById.remove(fromStdString(rtcStream.id()));
606     ASSERT_UNUSED(removed, removed);
607 }
608
609 void LibWebRTCMediaEndpoint::OnAddStream(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->addRemoteStream(*stream.get());
616     });
617 }
618
619 void LibWebRTCMediaEndpoint::OnRemoveStream(rtc::scoped_refptr<webrtc::MediaStreamInterface> stream)
620 {
621     callOnMainThread([protectedThis = makeRef(*this), stream = WTFMove(stream)] {
622         if (protectedThis->isStopped())
623             return;
624         ASSERT(stream);
625         protectedThis->removeRemoteStream(*stream.get());
626     });
627 }
628
629 void LibWebRTCMediaEndpoint::OnAddTrack(rtc::scoped_refptr<webrtc::RtpReceiverInterface> receiver, const std::vector<rtc::scoped_refptr<webrtc::MediaStreamInterface>>& streams)
630 {
631     if (RuntimeEnabledFeatures::sharedFeatures().webRTCUnifiedPlanEnabled())
632         return;
633
634     callOnMainThread([protectedThis = makeRef(*this), receiver = WTFMove(receiver), streams]() mutable {
635         if (protectedThis->isStopped())
636             return;
637         protectedThis->addRemoteTrack(WTFMove(receiver), streams);
638     });
639 }
640
641 void LibWebRTCMediaEndpoint::OnTrack(rtc::scoped_refptr<webrtc::RtpTransceiverInterface> transceiver)
642 {
643     if (!RuntimeEnabledFeatures::sharedFeatures().webRTCUnifiedPlanEnabled())
644         return;
645
646     callOnMainThread([protectedThis = makeRef(*this), transceiver = WTFMove(transceiver)]() mutable {
647         if (protectedThis->isStopped())
648             return;
649         protectedThis->newTransceiver(WTFMove(transceiver));
650     });
651 }
652
653 void LibWebRTCMediaEndpoint::OnRemoveTrack(rtc::scoped_refptr<webrtc::RtpReceiverInterface> receiver)
654 {
655     callOnMainThread([protectedThis = makeRef(*this), receiver = WTFMove(receiver)]() mutable {
656         if (protectedThis->isStopped())
657             return;
658         protectedThis->removeRemoteTrack(WTFMove(receiver));
659     });
660 }
661
662 std::unique_ptr<RTCDataChannelHandler> LibWebRTCMediaEndpoint::createDataChannel(const String& label, const RTCDataChannelInit& options)
663 {
664     auto init = LibWebRTCDataChannelHandler::fromRTCDataChannelInit(options);
665     auto channel = m_backend->CreateDataChannel(label.utf8().data(), &init);
666     return channel ? std::make_unique<LibWebRTCDataChannelHandler>(WTFMove(channel)) : nullptr;
667 }
668
669 void LibWebRTCMediaEndpoint::OnDataChannel(rtc::scoped_refptr<webrtc::DataChannelInterface> dataChannel)
670 {
671     callOnMainThread([protectedThis = makeRef(*this), dataChannel = WTFMove(dataChannel)]() mutable {
672         if (protectedThis->isStopped())
673             return;
674         auto& connection = protectedThis->m_peerConnectionBackend.connection();
675         connection.fireEvent(LibWebRTCDataChannelHandler::channelEvent(*connection.scriptExecutionContext(), WTFMove(dataChannel)));
676     });
677 }
678
679 void LibWebRTCMediaEndpoint::stop()
680 {
681     if (!m_backend)
682         return;
683
684     stopLoggingStats();
685
686     m_backend->Close();
687     m_backend = nullptr;
688     m_remoteStreamsById.clear();
689     m_remoteStreamsFromRemoteTrack.clear();
690 }
691
692 void LibWebRTCMediaEndpoint::OnRenegotiationNeeded()
693 {
694     callOnMainThread([protectedThis = makeRef(*this)] {
695         if (protectedThis->isStopped())
696             return;
697         protectedThis->m_peerConnectionBackend.markAsNeedingNegotiation();
698     });
699 }
700
701 static inline RTCIceConnectionState toRTCIceConnectionState(webrtc::PeerConnectionInterface::IceConnectionState state)
702 {
703     switch (state) {
704     case webrtc::PeerConnectionInterface::kIceConnectionNew:
705         return RTCIceConnectionState::New;
706     case webrtc::PeerConnectionInterface::kIceConnectionChecking:
707         return RTCIceConnectionState::Checking;
708     case webrtc::PeerConnectionInterface::kIceConnectionConnected:
709         return RTCIceConnectionState::Connected;
710     case webrtc::PeerConnectionInterface::kIceConnectionCompleted:
711         return RTCIceConnectionState::Completed;
712     case webrtc::PeerConnectionInterface::kIceConnectionFailed:
713         return RTCIceConnectionState::Failed;
714     case webrtc::PeerConnectionInterface::kIceConnectionDisconnected:
715         return RTCIceConnectionState::Disconnected;
716     case webrtc::PeerConnectionInterface::kIceConnectionClosed:
717         return RTCIceConnectionState::Closed;
718     case webrtc::PeerConnectionInterface::kIceConnectionMax:
719         break;
720     }
721
722     ASSERT_NOT_REACHED();
723     return RTCIceConnectionState::New;
724 }
725
726 void LibWebRTCMediaEndpoint::OnIceConnectionChange(webrtc::PeerConnectionInterface::IceConnectionState state)
727 {
728     auto connectionState = toRTCIceConnectionState(state);
729     callOnMainThread([protectedThis = makeRef(*this), connectionState] {
730         if (protectedThis->isStopped())
731             return;
732         if (protectedThis->m_peerConnectionBackend.connection().iceConnectionState() != connectionState)
733             protectedThis->m_peerConnectionBackend.connection().updateIceConnectionState(connectionState);
734     });
735 }
736
737 void LibWebRTCMediaEndpoint::OnIceGatheringChange(webrtc::PeerConnectionInterface::IceGatheringState state)
738 {
739     callOnMainThread([protectedThis = makeRef(*this), state] {
740         if (protectedThis->isStopped())
741             return;
742         if (state == webrtc::PeerConnectionInterface::kIceGatheringComplete)
743             protectedThis->m_peerConnectionBackend.doneGatheringCandidates();
744         else if (state == webrtc::PeerConnectionInterface::kIceGatheringGathering)
745             protectedThis->m_peerConnectionBackend.connection().updateIceGatheringState(RTCIceGatheringState::Gathering);
746     });
747 }
748
749 void LibWebRTCMediaEndpoint::OnIceCandidate(const webrtc::IceCandidateInterface *rtcCandidate)
750 {
751     ASSERT(rtcCandidate);
752
753     std::string sdp;
754     rtcCandidate->ToString(&sdp);
755
756     auto sdpMLineIndex = safeCast<unsigned short>(rtcCandidate->sdp_mline_index());
757
758     callOnMainThread([protectedThis = makeRef(*this), mid = fromStdString(rtcCandidate->sdp_mid()), sdp = fromStdString(sdp), sdpMLineIndex, url = fromStdString(rtcCandidate->server_url())]() mutable {
759         if (protectedThis->isStopped())
760             return;
761         protectedThis->m_peerConnectionBackend.newICECandidate(WTFMove(sdp), WTFMove(mid), sdpMLineIndex, WTFMove(url));
762     });
763 }
764
765 void LibWebRTCMediaEndpoint::OnIceCandidatesRemoved(const std::vector<cricket::Candidate>&)
766 {
767     ASSERT_NOT_REACHED();
768 }
769
770 void LibWebRTCMediaEndpoint::createSessionDescriptionSucceeded(std::unique_ptr<webrtc::SessionDescriptionInterface>&& description)
771 {
772     std::string sdp;
773     description->ToString(&sdp);
774
775     callOnMainThread([protectedThis = makeRef(*this), sdp = fromStdString(sdp)]() mutable {
776         if (protectedThis->isStopped())
777             return;
778         if (protectedThis->m_isInitiator)
779             protectedThis->m_peerConnectionBackend.createOfferSucceeded(WTFMove(sdp));
780         else
781             protectedThis->m_peerConnectionBackend.createAnswerSucceeded(WTFMove(sdp));
782     });
783 }
784
785 void LibWebRTCMediaEndpoint::createSessionDescriptionFailed(ExceptionCode errorCode, const char* errorMessage)
786 {
787     callOnMainThread([protectedThis = makeRef(*this), errorCode, errorMessage = String(errorMessage)] () mutable {
788         if (protectedThis->isStopped())
789             return;
790         if (protectedThis->m_isInitiator)
791             protectedThis->m_peerConnectionBackend.createOfferFailed(Exception { errorCode, WTFMove(errorMessage) });
792         else
793             protectedThis->m_peerConnectionBackend.createAnswerFailed(Exception { errorCode, WTFMove(errorMessage) });
794     });
795 }
796
797 void LibWebRTCMediaEndpoint::setLocalSessionDescriptionSucceeded()
798 {
799     callOnMainThread([protectedThis = makeRef(*this)] {
800         if (protectedThis->isStopped())
801             return;
802         protectedThis->m_peerConnectionBackend.setLocalDescriptionSucceeded();
803     });
804 }
805
806 void LibWebRTCMediaEndpoint::setLocalSessionDescriptionFailed(ExceptionCode errorCode, const char* errorMessage)
807 {
808     callOnMainThread([protectedThis = makeRef(*this), errorCode, errorMessage = String(errorMessage)] () mutable {
809         if (protectedThis->isStopped())
810             return;
811         protectedThis->m_peerConnectionBackend.setLocalDescriptionFailed(Exception { errorCode, WTFMove(errorMessage) });
812     });
813 }
814
815 void LibWebRTCMediaEndpoint::setRemoteSessionDescriptionSucceeded()
816 {
817     callOnMainThread([protectedThis = makeRef(*this)] {
818         if (protectedThis->isStopped())
819             return;
820         protectedThis->m_peerConnectionBackend.setRemoteDescriptionSucceeded();
821     });
822 }
823
824 void LibWebRTCMediaEndpoint::setRemoteSessionDescriptionFailed(ExceptionCode errorCode, const char* errorMessage)
825 {
826     callOnMainThread([protectedThis = makeRef(*this), errorCode, errorMessage = String(errorMessage)] () mutable {
827         if (protectedThis->isStopped())
828             return;
829         protectedThis->m_peerConnectionBackend.setRemoteDescriptionFailed(Exception { errorCode, WTFMove(errorMessage) });
830     });
831 }
832
833 void LibWebRTCMediaEndpoint::gatherStatsForLogging()
834 {
835     LibWebRTCProvider::callOnWebRTCSignalingThread([protectedThis = makeRef(*this)] {
836         if (protectedThis->m_backend)
837             protectedThis->m_backend->GetStats(protectedThis.ptr());
838     });
839 }
840
841 class RTCStatsLogger {
842 public:
843     explicit RTCStatsLogger(const webrtc::RTCStats& stats)
844         : m_stats(stats)
845     {
846     }
847
848     String toJSONString() const { return String(m_stats.ToJson().c_str()); }
849
850 private:
851     const webrtc::RTCStats& m_stats;
852 };
853
854 void LibWebRTCMediaEndpoint::OnStatsDelivered(const rtc::scoped_refptr<const webrtc::RTCStatsReport>& report)
855 {
856 #if !RELEASE_LOG_DISABLED
857     int64_t timestamp = report->timestamp_us();
858     if (!m_statsFirstDeliveredTimestamp)
859         m_statsFirstDeliveredTimestamp = timestamp;
860
861     callOnMainThread([protectedThis = makeRef(*this), this, timestamp, report] {
862         if (m_statsLogTimer.repeatInterval() != statsLogInterval(timestamp)) {
863             m_statsLogTimer.stop();
864             m_statsLogTimer.startRepeating(statsLogInterval(timestamp));
865         }
866
867         for (auto iterator = report->begin(); iterator != report->end(); ++iterator) {
868             if (logger().willLog(logChannel(), WTFLogLevelDebug)) {
869                 // Stats are very verbose, let's only display them in inspector console in verbose mode.
870                 logger().debug(LogWebRTC,
871                     Logger::LogSiteIdentifier("LibWebRTCMediaEndpoint", "OnStatsDelivered", logIdentifier()),
872                     RTCStatsLogger { *iterator });
873             } else {
874                 logger().logAlways(LogWebRTCStats,
875                     Logger::LogSiteIdentifier("LibWebRTCMediaEndpoint", "OnStatsDelivered", logIdentifier()),
876                     RTCStatsLogger { *iterator });
877             }
878         }
879     });
880 #else
881     UNUSED_PARAM(report);
882 #endif
883 }
884
885 void LibWebRTCMediaEndpoint::startLoggingStats()
886 {
887 #if !RELEASE_LOG_DISABLED
888     if (m_statsLogTimer.isActive())
889         m_statsLogTimer.stop();
890     m_statsLogTimer.startRepeating(statsLogInterval(0));
891 #endif
892 }
893
894 void LibWebRTCMediaEndpoint::stopLoggingStats()
895 {
896     m_statsLogTimer.stop();
897 }
898
899 #if !RELEASE_LOG_DISABLED
900 WTFLogChannel& LibWebRTCMediaEndpoint::logChannel() const
901 {
902     return LogWebRTC;
903 }
904
905 Seconds LibWebRTCMediaEndpoint::statsLogInterval(int64_t reportTimestamp) const
906 {
907     if (logger().willLog(logChannel(), WTFLogLevelInfo))
908         return 2_s;
909
910     if (reportTimestamp - m_statsFirstDeliveredTimestamp > 15000000)
911         return 10_s;
912
913     return 4_s;
914 }
915 #endif
916
917 } // namespace WebCore
918
919 namespace WTF {
920
921 template<typename Type>
922 struct LogArgument;
923
924 template <>
925 struct LogArgument<WebCore::RTCStatsLogger> {
926     static String toString(const WebCore::RTCStatsLogger& logger)
927     {
928         return String(logger.toJSONString());
929     }
930 };
931
932 }; // namespace WTF
933
934
935 #endif // USE(LIBWEBRTC)