1/*
2 *  Copyright (C) 2013 Nokia Corporation and/or its subsidiary(-ies).
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 THE COPYRIGHT HOLDERS AND CONTRIBUTORS
14 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
15 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
16 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
17 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
18 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
19 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27
28#if ENABLE(MEDIA_STREAM)
29
30#include "RTCPeerConnectionHandlerMock.h"
31
32#include "MediaConstraintsMock.h"
33#include "NotImplemented.h"
34#include "RTCDTMFSenderHandler.h"
35#include "RTCDTMFSenderHandlerMock.h"
36#include "RTCDataChannelHandler.h"
37#include "RTCDataChannelHandlerMock.h"
38#include "RTCNotifiersMock.h"
39#include "RTCSessionDescriptionRequest.h"
40#include "RTCVoidRequest.h"
41#include <wtf/text/CString.h>
42
43namespace WebCore {
44
45std::unique_ptr<RTCPeerConnectionHandler> RTCPeerConnectionHandlerMock::create(RTCPeerConnectionHandlerClient* client)
46{
47    return std::make_unique<RTCPeerConnectionHandlerMock>(client);
48}
49
50RTCPeerConnectionHandlerMock::RTCPeerConnectionHandlerMock(RTCPeerConnectionHandlerClient* client)
51    : m_client(client)
52{
53}
54
55bool RTCPeerConnectionHandlerMock::initialize(PassRefPtr<RTCConfigurationPrivate>)
56{
57    RefPtr<IceConnectionNotifier> notifier = adoptRef(new IceConnectionNotifier(m_client, RTCPeerConnectionHandlerClient::IceConnectionStateCompleted, RTCPeerConnectionHandlerClient::IceGatheringStateComplete));
58    m_timerEvents.append(adoptRef(new TimerEvent(this, notifier)));
59    return true;
60}
61
62void RTCPeerConnectionHandlerMock::createOffer(PassRefPtr<RTCSessionDescriptionRequest> request, PassRefPtr<RTCOfferOptionsPrivate>)
63{
64    RefPtr<SessionRequestNotifier> notifier = adoptRef(new SessionRequestNotifier(request, RTCSessionDescriptionDescriptor::create("offer", "local")));
65    m_timerEvents.append(adoptRef(new TimerEvent(this, notifier)));
66}
67
68void RTCPeerConnectionHandlerMock::createAnswer(PassRefPtr<RTCSessionDescriptionRequest> request, PassRefPtr<RTCOfferAnswerOptionsPrivate>)
69{
70    RefPtr<SessionRequestNotifier> notifier = adoptRef(new SessionRequestNotifier(request, RTCSessionDescriptionDescriptor::create("answer", "local")));
71    m_timerEvents.append(adoptRef(new TimerEvent(this, notifier)));
72}
73
74RTCPeerConnectionHandlerClient::SignalingState RTCPeerConnectionHandlerMock::signalingStateFromSDP(RTCSessionDescriptionDescriptor* descriptor)
75{
76    if (descriptor->type() == "offer" && descriptor->sdp() == "local")
77        return RTCPeerConnectionHandlerClient::SignalingStateHaveLocalOffer;
78    if (descriptor->type() == "offer" && descriptor->sdp() == "remote")
79        return RTCPeerConnectionHandlerClient::SignalingStateHaveRemoteOffer;
80    if (descriptor->type() == "pranswer" && descriptor->sdp() == "local")
81        return RTCPeerConnectionHandlerClient::SignalingStateHaveLocalPrAnswer;
82    if (descriptor->type() == "pranswer" && descriptor->sdp() == "remote")
83        return RTCPeerConnectionHandlerClient::SignalingStateHaveRemotePrAnswer;
84
85    return RTCPeerConnectionHandlerClient::SignalingStateStable;
86}
87
88void RTCPeerConnectionHandlerMock::setLocalDescription(PassRefPtr<RTCVoidRequest> request, PassRefPtr<RTCSessionDescriptionDescriptor> descriptor)
89{
90    RefPtr<VoidRequestNotifier> voidNotifier;
91    RefPtr<SignalingStateNotifier> signalingNotifier;
92    if (!descriptor.get() || descriptor->sdp() != "local")
93        voidNotifier = adoptRef(new VoidRequestNotifier(request, false, RTCPeerConnectionHandler::internalErrorName()));
94    else {
95        m_localSessionDescription = descriptor;
96        signalingNotifier = adoptRef(new SignalingStateNotifier(m_client, signalingStateFromSDP(m_localSessionDescription.get())));
97        voidNotifier = adoptRef(new VoidRequestNotifier(request, true));
98        m_timerEvents.append(adoptRef(new TimerEvent(this, signalingNotifier)));
99    }
100
101    m_timerEvents.append(adoptRef(new TimerEvent(this, voidNotifier)));
102}
103
104void RTCPeerConnectionHandlerMock::setRemoteDescription(PassRefPtr<RTCVoidRequest> request, PassRefPtr<RTCSessionDescriptionDescriptor> descriptor)
105{
106    RefPtr<VoidRequestNotifier> voidNotifier;
107    RefPtr<SignalingStateNotifier> signalingNotifier;
108    if (!descriptor.get() || descriptor->sdp() != "remote")
109        voidNotifier = adoptRef(new VoidRequestNotifier(request, false, RTCPeerConnectionHandler::internalErrorName()));
110    else {
111        m_remoteSessionDescription = descriptor;
112        signalingNotifier = adoptRef(new SignalingStateNotifier(m_client, signalingStateFromSDP(m_remoteSessionDescription.get())));
113        voidNotifier = adoptRef(new VoidRequestNotifier(request, true));
114        m_timerEvents.append(adoptRef(new TimerEvent(this, signalingNotifier)));
115    }
116
117    m_timerEvents.append(adoptRef(new TimerEvent(this, voidNotifier)));
118}
119
120PassRefPtr<RTCSessionDescriptionDescriptor> RTCPeerConnectionHandlerMock::localDescription()
121{
122    return m_localSessionDescription;
123}
124
125PassRefPtr<RTCSessionDescriptionDescriptor> RTCPeerConnectionHandlerMock::remoteDescription()
126{
127    return m_remoteSessionDescription;
128}
129
130bool RTCPeerConnectionHandlerMock::updateIce(PassRefPtr<RTCConfigurationPrivate>)
131{
132    return true;
133}
134
135bool RTCPeerConnectionHandlerMock::addIceCandidate(PassRefPtr<RTCVoidRequest> request, PassRefPtr<RTCIceCandidateDescriptor>)
136{
137    RefPtr<VoidRequestNotifier> notifier = adoptRef(new VoidRequestNotifier(request, true));
138    m_timerEvents.append(adoptRef(new TimerEvent(this, notifier)));
139    return true;
140}
141
142bool RTCPeerConnectionHandlerMock::addStream(PassRefPtr<MediaStreamPrivate>)
143{
144    // Spec states that every time a stream is added, a negotiationneeded event must be fired.
145    m_client->negotiationNeeded();
146    return true;
147}
148
149void RTCPeerConnectionHandlerMock::removeStream(PassRefPtr<MediaStreamPrivate>)
150{
151    // Spec states that every time a stream is removed, a negotiationneeded event must be fired.
152    m_client->negotiationNeeded();
153}
154
155void RTCPeerConnectionHandlerMock::getStats(PassRefPtr<RTCStatsRequest>)
156{
157    // Current tests does not support getUserMedia and this function requires it.
158    // Once WebKit has test support to getUserMedia, this must be fixed.
159    notImplemented();
160}
161
162std::unique_ptr<RTCDataChannelHandler> RTCPeerConnectionHandlerMock::createDataChannel(const String& label, const RTCDataChannelInit& init)
163{
164    RefPtr<RemoteDataChannelNotifier> notifier = adoptRef(new RemoteDataChannelNotifier(m_client));
165    m_timerEvents.append(adoptRef(new TimerEvent(this, notifier)));
166    return std::make_unique<RTCDataChannelHandlerMock>(label, init);
167}
168
169std::unique_ptr<RTCDTMFSenderHandler> RTCPeerConnectionHandlerMock::createDTMFSender(PassRefPtr<MediaStreamSource>)
170{
171    return std::make_unique<RTCDTMFSenderHandlerMock>();
172}
173
174void RTCPeerConnectionHandlerMock::stop()
175{
176}
177
178} // namespace WebCore
179
180#endif // ENABLE(MEDIA_STREAM)
181