[WTF] Import std::optional reference implementation as WTF::Optional
[WebKit-https.git] / Source / WebCore / Modules / mediastream / PeerConnectionBackend.h
index 7cbbc19..ca86191 100644 (file)
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-#ifndef PeerConnectionBackend_h
-#define PeerConnectionBackend_h
+#pragma once
 
 #if ENABLE(WEB_RTC)
 
 #include "JSDOMPromise.h"
 #include "PeerConnectionStates.h"
+#include "RTCDataChannel.h"
 
 namespace WebCore {
 
@@ -43,10 +43,10 @@ class Event;
 class MediaStream;
 class MediaStreamTrack;
 class PeerConnectionBackend;
-class RTCAnswerOptions;
 class RTCConfiguration;
+class RTCDataChannelHandler;
 class RTCIceCandidate;
-class RTCOfferOptions;
+class RTCPeerConnection;
 class RTCRtpReceiver;
 class RTCRtpSender;
 class RTCRtpSenderClient;
@@ -55,74 +55,95 @@ class RTCSessionDescription;
 class RTCStatsResponse;
 class ScriptExecutionContext;
 
+struct RTCAnswerOptions;
+struct RTCOfferOptions;
+
 namespace PeerConnection {
 typedef DOMPromise<RTCSessionDescription> SessionDescriptionPromise;
-typedef DOMPromise<std::nullptr_t> VoidPromise;
+typedef DOMPromise<void> VoidPromise;
 typedef DOMPromise<RTCStatsResponse> StatsPromise;
 }
 
-class PeerConnectionBackendClient {
-public:
-    virtual const Vector<RefPtr<RTCRtpTransceiver>>& getTransceivers() const = 0;
-    virtual RTCRtpSenderClient& senderClient() = 0;
-    virtual void fireEvent(Event&) = 0;
-
-    virtual void addTransceiver(RefPtr<RTCRtpTransceiver>&&) = 0;
-    virtual void setSignalingState(PeerConnectionStates::SignalingState) = 0;
-    virtual void updateIceGatheringState(PeerConnectionStates::IceGatheringState) = 0;
-    virtual void updateIceConnectionState(PeerConnectionStates::IceConnectionState) = 0;
-
-    virtual void scheduleNegotiationNeededEvent() = 0;
-
-    virtual ScriptExecutionContext* scriptExecutionContext() const = 0;
-    virtual PeerConnectionStates::SignalingState internalSignalingState() const = 0;
-    virtual PeerConnectionStates::IceGatheringState internalIceGatheringState() const = 0;
-    virtual PeerConnectionStates::IceConnectionState internalIceConnectionState() const = 0;
-
-    virtual ~PeerConnectionBackendClient() { }
-};
-
-typedef std::unique_ptr<PeerConnectionBackend> (*CreatePeerConnectionBackend)(PeerConnectionBackendClient*);
+typedef std::unique_ptr<PeerConnectionBackend> (*CreatePeerConnectionBackend)(RTCPeerConnection&);
 
 class PeerConnectionBackend {
 public:
     WEBCORE_EXPORT static CreatePeerConnectionBackend create;
+
+    PeerConnectionBackend(RTCPeerConnection& peerConnection) : m_peerConnection(peerConnection) { }
     virtual ~PeerConnectionBackend() { }
 
-    virtual void createOffer(RTCOfferOptions&, PeerConnection::SessionDescriptionPromise&&) = 0;
-    virtual void createAnswer(RTCAnswerOptions&, PeerConnection::SessionDescriptionPromise&&) = 0;
+    void createOffer(RTCOfferOptions&&, PeerConnection::SessionDescriptionPromise&&);
+    void createAnswer(RTCAnswerOptions&&, PeerConnection::SessionDescriptionPromise&&);
+    void setLocalDescription(RTCSessionDescription&, PeerConnection::VoidPromise&&);
+    void setRemoteDescription(RTCSessionDescription&, PeerConnection::VoidPromise&&);
+    void addIceCandidate(RTCIceCandidate&, PeerConnection::VoidPromise&&);
+
+    virtual std::unique_ptr<RTCDataChannelHandler> createDataChannelHandler(const String&, const RTCDataChannelInit&) = 0;
+
+    void stop();
 
-    virtual void setLocalDescription(RTCSessionDescription&, PeerConnection::VoidPromise&&) = 0;
     virtual RefPtr<RTCSessionDescription> localDescription() const = 0;
     virtual RefPtr<RTCSessionDescription> currentLocalDescription() const = 0;
     virtual RefPtr<RTCSessionDescription> pendingLocalDescription() const = 0;
 
-    virtual void setRemoteDescription(RTCSessionDescription&, PeerConnection::VoidPromise&&) = 0;
     virtual RefPtr<RTCSessionDescription> remoteDescription() const = 0;
     virtual RefPtr<RTCSessionDescription> currentRemoteDescription() const = 0;
     virtual RefPtr<RTCSessionDescription> pendingRemoteDescription() const = 0;
 
     virtual void setConfiguration(RTCConfiguration&) = 0;
-    virtual void addIceCandidate(RTCIceCandidate&, PeerConnection::VoidPromise&&) = 0;
 
     virtual void getStats(MediaStreamTrack*, PeerConnection::StatsPromise&&) = 0;
 
     virtual Vector<RefPtr<MediaStream>> getRemoteStreams() const = 0;
 
-    virtual RefPtr<RTCRtpReceiver> createReceiver(const String& transceiverMid, const String& trackKind, const String& trackId) = 0;
+    virtual Ref<RTCRtpReceiver> createReceiver(const String& transceiverMid, const String& trackKind, const String& trackId) = 0;
     virtual void replaceTrack(RTCRtpSender&, RefPtr<MediaStreamTrack>&&, PeerConnection::VoidPromise&&) = 0;
 
-    virtual void stop() = 0;
-
     virtual bool isNegotiationNeeded() const = 0;
     virtual void markAsNeedingNegotiation() = 0;
     virtual void clearNegotiationNeededState() = 0;
 
     virtual void emulatePlatformEvent(const String& action) = 0;
+
+protected:
+    void fireICECandidateEvent(RefPtr<RTCIceCandidate>&&);
+    void doneGatheringCandidates();
+
+    void updateSignalingState(PeerConnectionStates::SignalingState);
+
+    void createOfferSucceeded(String&&);
+    void createOfferFailed(Exception&&);
+
+    void createAnswerSucceeded(String&&);
+    void createAnswerFailed(Exception&&);
+
+    void setLocalDescriptionSucceeded();
+    void setLocalDescriptionFailed(Exception&&);
+
+    void setRemoteDescriptionSucceeded();
+    void setRemoteDescriptionFailed(Exception&&);
+
+    void addIceCandidateSucceeded();
+    void addIceCandidateFailed(Exception&&);
+
+private:
+    virtual void doCreateOffer(RTCOfferOptions&&) = 0;
+    virtual void doCreateAnswer(RTCAnswerOptions&&) = 0;
+    virtual void doSetLocalDescription(RTCSessionDescription&) = 0;
+    virtual void doSetRemoteDescription(RTCSessionDescription&) = 0;
+    virtual void doAddIceCandidate(RTCIceCandidate&) = 0;
+    virtual void doStop() = 0;
+
+protected:
+    RTCPeerConnection& m_peerConnection;
+
+private:
+    std::optional<PeerConnection::SessionDescriptionPromise> m_offerAnswerPromise;
+    std::optional<PeerConnection::VoidPromise> m_setDescriptionPromise;
+    std::optional<PeerConnection::VoidPromise> m_addIceCandidatePromise;
 };
 
 } // namespace WebCore
 
 #endif // ENABLE(WEB_RTC)
-
-#endif // PeerConnectionBackend_h