Add very basic PageCache support for RTCPeerConnection
[WebKit-https.git] / Source / WebCore / Modules / mediastream / RTCPeerConnection.h
1 /*
2  * Copyright (C) 2012 Google Inc. All rights reserved.
3  * Copyright (C) 2013 Nokia Corporation and/or its subsidiary(-ies).
4  * Copyright (C) 2015 Ericsson AB. All rights reserved.
5  * Copyright (C) 2017 Apple Inc. All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer
15  *    in the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of Google Inc. nor the names of its contributors
18  *    may be used to endorse or promote products derived from this
19  *    software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #pragma once
35
36 #if ENABLE(WEB_RTC)
37
38 #include "ActiveDOMObject.h"
39 #include "EventTarget.h"
40 #include "MediaEndpointConfiguration.h"
41 #include "MediaStream.h"
42 #include "RTCConfiguration.h"
43 #include "RTCDataChannel.h"
44 #include "RTCIceConnectionState.h"
45 #include "RTCIceGatheringState.h"
46 #include "RTCPeerConnectionState.h"
47 #include "RTCRtpTransceiver.h"
48 #include "RTCSignalingState.h"
49 #include <JavaScriptCore/Uint8Array.h>
50 #include <wtf/LoggerHelper.h>
51
52 namespace WebCore {
53
54 class MediaStream;
55 class MediaStreamTrack;
56 class PeerConnectionBackend;
57 class RTCController;
58 class RTCIceCandidate;
59 class RTCPeerConnectionErrorCallback;
60 class RTCSessionDescription;
61 class RTCStatsCallback;
62
63 struct RTCAnswerOptions;
64 struct RTCOfferOptions;
65 struct RTCRtpParameters;
66
67 struct RTCRtpTransceiverInit {
68     RTCRtpTransceiverDirection direction { RTCRtpTransceiverDirection::Sendrecv };
69     Vector<RefPtr<MediaStream>> streams;
70 };
71
72 class RTCPeerConnection final
73     : public RefCounted<RTCPeerConnection>
74     , public EventTargetWithInlineData
75     , public ActiveDOMObject
76 #if !RELEASE_LOG_DISABLED
77     , private LoggerHelper
78 #endif
79 {
80     WTF_MAKE_ISO_ALLOCATED(RTCPeerConnection);
81 public:
82     static Ref<RTCPeerConnection> create(ScriptExecutionContext&);
83     virtual ~RTCPeerConnection();
84
85     using DataChannelInit = RTCDataChannelInit;
86
87     ExceptionOr<void> initializeWith(Document&, RTCConfiguration&&);
88
89     struct CertificateParameters {
90         String name;
91         String hash;
92         String namedCurve;
93         Optional<uint32_t> modulusLength;
94         RefPtr<Uint8Array> publicExponent;
95         Optional<double> expires;
96     };
97
98     using AlgorithmIdentifier = Variant<JSC::Strong<JSC::JSObject>, String>;
99     static void generateCertificate(JSC::ExecState&, AlgorithmIdentifier&&, DOMPromiseDeferred<IDLInterface<RTCCertificate>>&&);
100
101     // 4.3.2 RTCPeerConnection Interface
102     void queuedCreateOffer(RTCOfferOptions&&, PeerConnection::SessionDescriptionPromise&&);
103     void queuedCreateAnswer(RTCAnswerOptions&&, PeerConnection::SessionDescriptionPromise&&);
104
105     void queuedSetLocalDescription(RTCSessionDescription&, DOMPromiseDeferred<void>&&);
106     RefPtr<RTCSessionDescription> localDescription() const;
107     RefPtr<RTCSessionDescription> currentLocalDescription() const;
108     RefPtr<RTCSessionDescription> pendingLocalDescription() const;
109
110     void queuedSetRemoteDescription(RTCSessionDescription&, DOMPromiseDeferred<void>&&);
111     RefPtr<RTCSessionDescription> remoteDescription() const;
112     RefPtr<RTCSessionDescription> currentRemoteDescription() const;
113     RefPtr<RTCSessionDescription> pendingRemoteDescription() const;
114
115     void queuedAddIceCandidate(RTCIceCandidate*, DOMPromiseDeferred<void>&&);
116
117     RTCSignalingState signalingState() const { return m_signalingState; }
118     RTCIceGatheringState iceGatheringState() const { return m_iceGatheringState; }
119     RTCIceConnectionState iceConnectionState() const { return m_iceConnectionState; }
120     RTCPeerConnectionState connectionState() const { return m_connectionState; }
121
122     const RTCConfiguration& getConfiguration() const { return m_configuration; }
123     ExceptionOr<void> setConfiguration(RTCConfiguration&&);
124     void close();
125
126     bool isClosed() const { return m_connectionState == RTCPeerConnectionState::Closed; }
127     bool isStopped() const { return m_isStopped; }
128
129     void addInternalTransceiver(Ref<RTCRtpTransceiver>&& transceiver) { m_transceiverSet->append(WTFMove(transceiver)); }
130
131     // 5.1 RTCPeerConnection extensions
132     Vector<std::reference_wrapper<RTCRtpSender>> getSenders() const;
133     Vector<std::reference_wrapper<RTCRtpReceiver>> getReceivers() const;
134     const Vector<RefPtr<RTCRtpTransceiver>>& getTransceivers() const;
135
136     const Vector<RefPtr<RTCRtpTransceiver>>& currentTransceivers() const { return m_transceiverSet->list(); }
137
138     ExceptionOr<Ref<RTCRtpSender>> addTrack(Ref<MediaStreamTrack>&&, const Vector<std::reference_wrapper<MediaStream>>&);
139     ExceptionOr<void> removeTrack(RTCRtpSender&);
140
141     using AddTransceiverTrackOrKind = Variant<RefPtr<MediaStreamTrack>, String>;
142     ExceptionOr<Ref<RTCRtpTransceiver>> addTransceiver(AddTransceiverTrackOrKind&&, const RTCRtpTransceiverInit&);
143
144     // 6.1 Peer-to-peer data API
145     ExceptionOr<Ref<RTCDataChannel>> createDataChannel(String&&, RTCDataChannelInit&&);
146
147     // 8.2 Statistics API
148     void getStats(MediaStreamTrack*, Ref<DeferredPromise>&&);
149
150     // EventTarget
151     EventTargetInterface eventTargetInterface() const final { return RTCPeerConnectionEventTargetInterfaceType; }
152     ScriptExecutionContext* scriptExecutionContext() const final { return ActiveDOMObject::scriptExecutionContext(); }
153
154     using RefCounted::ref;
155     using RefCounted::deref;
156
157     // Used for testing with a mock
158     WEBCORE_EXPORT void emulatePlatformEvent(const String& action);
159
160     // API used by PeerConnectionBackend and relatives
161     void addTransceiver(Ref<RTCRtpTransceiver>&&);
162     void setSignalingState(RTCSignalingState);
163     void updateIceGatheringState(RTCIceGatheringState);
164     void updateIceConnectionState(RTCIceConnectionState);
165
166     void scheduleNegotiationNeededEvent();
167
168     void fireEvent(Event&);
169
170     void disableICECandidateFiltering() { m_backend->disableICECandidateFiltering(); }
171     void enableICECandidateFiltering() { m_backend->enableICECandidateFiltering(); }
172
173     void clearController() { m_controller = nullptr; }
174
175     // ActiveDOMObject.
176     bool hasPendingActivity() const final;
177     
178     Document* document();
179
180 #if !RELEASE_LOG_DISABLED
181     const Logger& logger() const final { return m_logger.get(); }
182     const void* logIdentifier() const final { return m_logIdentifier; }
183     const char* logClassName() const final { return "RTCPeerConnection"; }
184     WTFLogChannel& logChannel() const final;
185 #endif
186
187 private:
188     template<typename PromiseType> void addPendingPromise(PromiseType& promise)
189     {
190         promise.whenSettled([pendingActivity = makePendingActivity(*this)] { });
191     }
192
193     RTCPeerConnection(Document&);
194
195     ExceptionOr<void> initializeConfiguration(RTCConfiguration&&);
196     Ref<RTCRtpTransceiver> completeAddTransceiver(Ref<RTCRtpSender>&&, const RTCRtpTransceiverInit&, const String& trackId, const String& trackKind);
197
198     void registerToController(RTCController&);
199     void unregisterFromController();
200
201     friend class Internals;
202     void applyRotationForOutgoingVideoSources() { m_backend->applyRotationForOutgoingVideoSources(); }
203
204     // EventTarget implementation.
205     void refEventTarget() final { ref(); }
206     void derefEventTarget() final { deref(); }
207     void dispatchEvent(Event&) final;
208
209     // ActiveDOMObject
210     WEBCORE_EXPORT void stop() final;
211     const char* activeDOMObjectName() const final;
212     bool canSuspendForDocumentSuspension() const final;
213
214     void updateConnectionState();
215     bool doClose();
216     void doStop();
217
218     ExceptionOr<Vector<MediaEndpointConfiguration::CertificatePEM>> certificatesFromConfiguration(const RTCConfiguration&);
219
220     bool m_isStopped { false };
221     RTCSignalingState m_signalingState { RTCSignalingState::Stable };
222     RTCIceGatheringState m_iceGatheringState { RTCIceGatheringState::New };
223     RTCIceConnectionState m_iceConnectionState { RTCIceConnectionState::New };
224     RTCPeerConnectionState m_connectionState { RTCPeerConnectionState::New };
225
226 #if !RELEASE_LOG_DISABLED
227     Ref<const Logger> m_logger;
228     const void* m_logIdentifier;
229 #endif
230
231     std::unique_ptr<RtpTransceiverSet> m_transceiverSet { std::unique_ptr<RtpTransceiverSet>(new RtpTransceiverSet()) };
232
233     std::unique_ptr<PeerConnectionBackend> m_backend;
234
235     RTCConfiguration m_configuration;
236     RTCController* m_controller { nullptr };
237     Vector<RefPtr<RTCCertificate>> m_certificates;
238 };
239
240 } // namespace WebCore
241
242 #endif // ENABLE(WEB_RTC)