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#pragma once
26
27#if USE(LIBWEBRTC)
28
29#include "PeerConnectionBackend.h"
30#include <wtf/HashMap.h>
31
32namespace webrtc {
33class IceCandidateInterface;
34}
35
36namespace WebCore {
37
38class LibWebRTCMediaEndpoint;
39class LibWebRTCProvider;
40class LibWebRTCRtpSenderBackend;
41class LibWebRTCRtpTransceiverBackend;
42class RTCRtpReceiver;
43class RTCRtpReceiverBackend;
44class RTCSessionDescription;
45class RTCStatsReport;
46class RealtimeIncomingAudioSource;
47class RealtimeIncomingVideoSource;
48class RealtimeMediaSource;
49class RealtimeOutgoingAudioSource;
50class RealtimeOutgoingVideoSource;
51
52class LibWebRTCPeerConnectionBackend final : public PeerConnectionBackend {
53 WTF_MAKE_FAST_ALLOCATED;
54public:
55 LibWebRTCPeerConnectionBackend(RTCPeerConnection&, LibWebRTCProvider&);
56 ~LibWebRTCPeerConnectionBackend();
57
58 void replaceTrack(RTCRtpSender&, RefPtr<MediaStreamTrack>&&, DOMPromiseDeferred<void>&&);
59
60 bool shouldOfferAllowToReceive(const char*) const;
61
62private:
63 void doCreateOffer(RTCOfferOptions&&) final;
64 void doCreateAnswer(RTCAnswerOptions&&) final;
65 void doSetLocalDescription(RTCSessionDescription&) final;
66 void doSetRemoteDescription(RTCSessionDescription&) final;
67 void doAddIceCandidate(RTCIceCandidate&) final;
68 void doStop() final;
69 std::unique_ptr<RTCDataChannelHandler> createDataChannelHandler(const String&, const RTCDataChannelInit&) final;
70 bool setConfiguration(MediaEndpointConfiguration&&) final;
71 void getStats(Ref<DeferredPromise>&&) final;
72 void getStats(RTCRtpSender&, Ref<DeferredPromise>&&) final;
73 void getStats(RTCRtpReceiver&, Ref<DeferredPromise>&&) final;
74
75 RefPtr<RTCSessionDescription> localDescription() const final;
76 RefPtr<RTCSessionDescription> currentLocalDescription() const final;
77 RefPtr<RTCSessionDescription> pendingLocalDescription() const final;
78
79 RefPtr<RTCSessionDescription> remoteDescription() const final;
80 RefPtr<RTCSessionDescription> currentRemoteDescription() const final;
81 RefPtr<RTCSessionDescription> pendingRemoteDescription() const final;
82
83 void emulatePlatformEvent(const String&) final { }
84 void applyRotationForOutgoingVideoSources() final;
85
86 friend class LibWebRTCMediaEndpoint;
87 friend class LibWebRTCRtpSenderBackend;
88 RTCPeerConnection& connection() { return m_peerConnection; }
89
90 void getStatsSucceeded(const DeferredPromise&, Ref<RTCStatsReport>&&);
91
92 ExceptionOr<Ref<RTCRtpSender>> addTrack(MediaStreamTrack&, Vector<String>&&) final;
93 void removeTrack(RTCRtpSender&) final;
94
95 ExceptionOr<Ref<RTCRtpTransceiver>> addTransceiver(const String&, const RTCRtpTransceiverInit&) final;
96 ExceptionOr<Ref<RTCRtpTransceiver>> addTransceiver(Ref<MediaStreamTrack>&&, const RTCRtpTransceiverInit&) final;
97 void setSenderSourceFromTrack(LibWebRTCRtpSenderBackend&, MediaStreamTrack&);
98
99 RTCRtpTransceiver* existingTransceiver(WTF::Function<bool(LibWebRTCRtpTransceiverBackend&)>&&);
100 RTCRtpTransceiver& newRemoteTransceiver(std::unique_ptr<LibWebRTCRtpTransceiverBackend>&&, Ref<RealtimeMediaSource>&&);
101
102 void collectTransceivers() final;
103
104 struct VideoReceiver {
105 Ref<RTCRtpReceiver> receiver;
106 Ref<RealtimeIncomingVideoSource> source;
107 };
108 struct AudioReceiver {
109 Ref<RTCRtpReceiver> receiver;
110 Ref<RealtimeIncomingAudioSource> source;
111 };
112 VideoReceiver videoReceiver(String&& trackId);
113 AudioReceiver audioReceiver(String&& trackId);
114
115private:
116 bool isLocalDescriptionSet() const final { return m_isLocalDescriptionSet; }
117
118 Ref<RTCRtpTransceiver> completeAddTransceiver(Ref<RTCRtpSender>&&, const RTCRtpTransceiverInit&, const String& trackId, const String& trackKind);
119
120 Ref<RTCRtpReceiver> createReceiver(const String& trackKind, const String& trackId);
121
122 template<typename T>
123 ExceptionOr<Ref<RTCRtpTransceiver>> addUnifiedPlanTransceiver(T&& trackOrKind, const RTCRtpTransceiverInit&);
124
125 Ref<RTCRtpReceiver> createReceiverForSource(Ref<RealtimeMediaSource>&&, std::unique_ptr<RTCRtpReceiverBackend>&&);
126
127 Ref<LibWebRTCMediaEndpoint> m_endpoint;
128 bool m_isLocalDescriptionSet { false };
129 bool m_isRemoteDescriptionSet { false };
130
131 Vector<std::unique_ptr<webrtc::IceCandidateInterface>> m_pendingCandidates;
132 Vector<Ref<RTCRtpReceiver>> m_pendingReceivers;
133};
134
135} // namespace WebCore
136
137#endif // USE(LIBWEBRTC)
138