2 * Copyright (C) 2017-2018 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
29 #include "LibWebRTCObservers.h"
30 #include "LibWebRTCProvider.h"
31 #include "LibWebRTCRtpSenderBackend.h"
32 #include "RTCRtpReceiver.h"
35 ALLOW_UNUSED_PARAMETERS_BEGIN
37 #include <webrtc/api/jsep.h>
38 #include <webrtc/api/peerconnectioninterface.h>
39 #include <webrtc/pc/peerconnectionfactory.h>
40 #include <webrtc/pc/rtcstatscollector.h>
42 ALLOW_UNUSED_PARAMETERS_END
44 #include <wtf/LoggerHelper.h>
45 #include <wtf/ThreadSafeRefCounted.h>
48 class CreateSessionDescriptionObserver;
49 class DataChannelInterface;
50 class IceCandidateInterface;
51 class MediaStreamInterface;
52 class PeerConnectionObserver;
53 class SessionDescriptionInterface;
54 class SetSessionDescriptionObserver;
58 class LibWebRTCProvider;
59 class LibWebRTCPeerConnectionBackend;
60 class LibWebRTCRtpReceiverBackend;
61 class LibWebRTCRtpTransceiverBackend;
62 class LibWebRTCStatsCollector;
63 class MediaStreamTrack;
64 class RTCSessionDescription;
66 class LibWebRTCMediaEndpoint
67 : public ThreadSafeRefCounted<LibWebRTCMediaEndpoint, WTF::DestructionThread::Main>
68 , private webrtc::PeerConnectionObserver
69 , private webrtc::RTCStatsCollectorCallback
70 #if !RELEASE_LOG_DISABLED
71 , private LoggerHelper
75 static Ref<LibWebRTCMediaEndpoint> create(LibWebRTCPeerConnectionBackend& peerConnection, LibWebRTCProvider& client) { return adoptRef(*new LibWebRTCMediaEndpoint(peerConnection, client)); }
76 virtual ~LibWebRTCMediaEndpoint() = default;
78 bool setConfiguration(LibWebRTCProvider&, webrtc::PeerConnectionInterface::RTCConfiguration&&);
80 webrtc::PeerConnectionInterface& backend() const { ASSERT(m_backend); return *m_backend.get(); }
81 void doSetLocalDescription(RTCSessionDescription&);
82 void doSetRemoteDescription(RTCSessionDescription&);
83 void doCreateOffer(const RTCOfferOptions&);
84 void doCreateAnswer();
85 void getStats(Ref<DeferredPromise>&&);
86 void getStats(webrtc::RtpReceiverInterface&, Ref<DeferredPromise>&&);
87 void getStats(webrtc::RtpSenderInterface&, Ref<DeferredPromise>&&);
88 std::unique_ptr<RTCDataChannelHandler> createDataChannel(const String&, const RTCDataChannelInit&);
89 bool addIceCandidate(webrtc::IceCandidateInterface& candidate) { return m_backend->AddIceCandidate(&candidate); }
92 bool isStopped() const { return !m_backend; }
94 RefPtr<RTCSessionDescription> localDescription() const;
95 RefPtr<RTCSessionDescription> remoteDescription() const;
96 RefPtr<RTCSessionDescription> currentLocalDescription() const;
97 RefPtr<RTCSessionDescription> currentRemoteDescription() const;
98 RefPtr<RTCSessionDescription> pendingLocalDescription() const;
99 RefPtr<RTCSessionDescription> pendingRemoteDescription() const;
101 bool addTrack(LibWebRTCRtpSenderBackend&, MediaStreamTrack&, const Vector<String>&);
102 void removeTrack(LibWebRTCRtpSenderBackend&);
105 std::unique_ptr<LibWebRTCRtpSenderBackend> senderBackend;
106 std::unique_ptr<LibWebRTCRtpReceiverBackend> receiverBackend;
107 std::unique_ptr<LibWebRTCRtpTransceiverBackend> transceiverBackend;
109 std::optional<Backends> addTransceiver(const String& trackKind, const RTCRtpTransceiverInit&);
110 std::optional<Backends> addTransceiver(MediaStreamTrack&, const RTCRtpTransceiverInit&);
111 std::unique_ptr<LibWebRTCRtpTransceiverBackend> transceiverBackendFromSender(LibWebRTCRtpSenderBackend&);
113 void setSenderSourceFromTrack(LibWebRTCRtpSenderBackend&, MediaStreamTrack&);
114 void collectTransceivers();
117 LibWebRTCMediaEndpoint(LibWebRTCPeerConnectionBackend&, LibWebRTCProvider&);
119 // webrtc::PeerConnectionObserver API
120 void OnSignalingChange(webrtc::PeerConnectionInterface::SignalingState) final;
121 void OnAddStream(rtc::scoped_refptr<webrtc::MediaStreamInterface>) final;
122 void OnRemoveStream(rtc::scoped_refptr<webrtc::MediaStreamInterface>) final;
123 void OnDataChannel(rtc::scoped_refptr<webrtc::DataChannelInterface>) final;
124 void OnAddTrack(rtc::scoped_refptr<webrtc::RtpReceiverInterface>, const std::vector<rtc::scoped_refptr<webrtc::MediaStreamInterface>>&) final;
125 void OnTrack(rtc::scoped_refptr<webrtc::RtpTransceiverInterface>) final;
126 void OnRemoveTrack(rtc::scoped_refptr<webrtc::RtpReceiverInterface>) final;
128 void OnRenegotiationNeeded() final;
129 void OnIceConnectionChange(webrtc::PeerConnectionInterface::IceConnectionState) final;
130 void OnIceGatheringChange(webrtc::PeerConnectionInterface::IceGatheringState) final;
131 void OnIceCandidate(const webrtc::IceCandidateInterface*) final;
132 void OnIceCandidatesRemoved(const std::vector<cricket::Candidate>&) final;
134 void createSessionDescriptionSucceeded(std::unique_ptr<webrtc::SessionDescriptionInterface>&&);
135 void createSessionDescriptionFailed(ExceptionCode, const char*);
136 void setLocalSessionDescriptionSucceeded();
137 void setLocalSessionDescriptionFailed(ExceptionCode, const char*);
138 void setRemoteSessionDescriptionSucceeded();
139 void setRemoteSessionDescriptionFailed(ExceptionCode, const char*);
140 void addRemoteStream(webrtc::MediaStreamInterface&);
141 void addRemoteTrack(rtc::scoped_refptr<webrtc::RtpReceiverInterface>&&, const std::vector<rtc::scoped_refptr<webrtc::MediaStreamInterface>>&);
142 void removeRemoteStream(webrtc::MediaStreamInterface&);
143 void newTransceiver(rtc::scoped_refptr<webrtc::RtpTransceiverInterface>&&);
144 void removeRemoteTrack(rtc::scoped_refptr<webrtc::RtpReceiverInterface>&&);
146 void fireTrackEvent(Ref<RTCRtpReceiver>&&, Ref<MediaStreamTrack>&&, const std::vector<rtc::scoped_refptr<webrtc::MediaStreamInterface>>&, RefPtr<RTCRtpTransceiver>&&);
149 std::optional<Backends> createTransceiverBackends(T&&, const RTCRtpTransceiverInit&, LibWebRTCRtpSenderBackend::Source&&);
151 void OnStatsDelivered(const rtc::scoped_refptr<const webrtc::RTCStatsReport>&) final;
152 void gatherStatsForLogging();
153 void startLoggingStats();
154 void stopLoggingStats();
156 void getStats(Ref<DeferredPromise>&&, WTF::Function<void(rtc::scoped_refptr<LibWebRTCStatsCollector>&&)>&&);
158 MediaStream& mediaStreamFromRTCStream(webrtc::MediaStreamInterface&);
160 void AddRef() const { ref(); }
161 rtc::RefCountReleaseStatus Release() const
163 auto result = refCount() - 1;
165 return result ? rtc::RefCountReleaseStatus::kOtherRefsRemained
166 : rtc::RefCountReleaseStatus::kDroppedLastRef;
169 std::pair<LibWebRTCRtpSenderBackend::Source, rtc::scoped_refptr<webrtc::MediaStreamTrackInterface>> createSourceAndRTCTrack(MediaStreamTrack&);
171 #if !RELEASE_LOG_DISABLED
172 const Logger& logger() const final { return m_logger.get(); }
173 const void* logIdentifier() const final { return m_logIdentifier; }
174 const char* logClassName() const final { return "LibWebRTCMediaEndpoint"; }
175 WTFLogChannel& logChannel() const final;
177 Seconds statsLogInterval(int64_t) const;
180 LibWebRTCPeerConnectionBackend& m_peerConnectionBackend;
181 webrtc::PeerConnectionFactoryInterface& m_peerConnectionFactory;
182 rtc::scoped_refptr<webrtc::PeerConnectionInterface> m_backend;
184 friend CreateSessionDescriptionObserver<LibWebRTCMediaEndpoint>;
185 friend SetLocalSessionDescriptionObserver<LibWebRTCMediaEndpoint>;
186 friend SetRemoteSessionDescriptionObserver<LibWebRTCMediaEndpoint>;
188 CreateSessionDescriptionObserver<LibWebRTCMediaEndpoint> m_createSessionDescriptionObserver;
189 SetLocalSessionDescriptionObserver<LibWebRTCMediaEndpoint> m_setLocalSessionDescriptionObserver;
190 SetRemoteSessionDescriptionObserver<LibWebRTCMediaEndpoint> m_setRemoteSessionDescriptionObserver;
192 HashMap<String, RefPtr<MediaStream>> m_remoteStreamsById;
193 HashMap<MediaStreamTrack*, Vector<String>> m_remoteStreamsFromRemoteTrack;
195 bool m_isInitiator { false };
196 Timer m_statsLogTimer;
198 HashMap<String, rtc::scoped_refptr<webrtc::MediaStreamInterface>> m_localStreams;
200 #if !RELEASE_LOG_DISABLED
201 int64_t m_statsFirstDeliveredTimestamp { 0 };
202 Ref<const Logger> m_logger;
203 const void* m_logIdentifier;
207 } // namespace WebCore
209 #endif // USE(LIBWEBRTC)