1/*
2 * Copyright (C) 2012 Google 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 *
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer
12 *    in the documentation and/or other materials provided with the
13 *    distribution.
14 * 3. Neither the name of Google Inc. nor the names of its contributors
15 *    may be used to endorse or promote products derived from this
16 *    software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#ifndef RTCPeerConnection_h
32#define RTCPeerConnection_h
33
34#include "bindings/v8/Dictionary.h"
35#include "bindings/v8/ScriptWrappable.h"
36#include "core/dom/ActiveDOMObject.h"
37#include "core/dom/EventTarget.h"
38#include "core/platform/Timer.h"
39#include "core/platform/mediastream/RTCPeerConnectionHandler.h"
40#include "core/platform/mediastream/RTCPeerConnectionHandlerClient.h"
41#include "modules/mediastream/MediaStream.h"
42#include "modules/mediastream/RTCIceCandidate.h"
43#include "wtf/RefCounted.h"
44
45namespace WebCore {
46
47class ExceptionState;
48class MediaConstraints;
49class MediaStreamTrack;
50class RTCConfiguration;
51class RTCDTMFSender;
52class RTCDataChannel;
53class RTCErrorCallback;
54class RTCSessionDescription;
55class RTCSessionDescriptionCallback;
56class RTCStatsCallback;
57class VoidCallback;
58
59class RTCPeerConnection : public RefCounted<RTCPeerConnection>, public ScriptWrappable, public RTCPeerConnectionHandlerClient, public EventTarget, public ActiveDOMObject {
60public:
61    static PassRefPtr<RTCPeerConnection> create(ScriptExecutionContext*, const Dictionary& rtcConfiguration, const Dictionary& mediaConstraints, ExceptionState&);
62    ~RTCPeerConnection();
63
64    void createOffer(PassRefPtr<RTCSessionDescriptionCallback>, PassRefPtr<RTCErrorCallback>, const Dictionary& mediaConstraints, ExceptionState&);
65
66    void createAnswer(PassRefPtr<RTCSessionDescriptionCallback>, PassRefPtr<RTCErrorCallback>, const Dictionary& mediaConstraints, ExceptionState&);
67
68    void setLocalDescription(PassRefPtr<RTCSessionDescription>, PassRefPtr<VoidCallback>, PassRefPtr<RTCErrorCallback>, ExceptionState&);
69    PassRefPtr<RTCSessionDescription> localDescription(ExceptionState&);
70
71    void setRemoteDescription(PassRefPtr<RTCSessionDescription>, PassRefPtr<VoidCallback>, PassRefPtr<RTCErrorCallback>, ExceptionState&);
72    PassRefPtr<RTCSessionDescription> remoteDescription(ExceptionState&);
73
74    String signalingState() const;
75
76    void updateIce(const Dictionary& rtcConfiguration, const Dictionary& mediaConstraints, ExceptionState&);
77
78    void addIceCandidate(RTCIceCandidate*, ExceptionState&);
79
80    String iceGatheringState() const;
81
82    String iceConnectionState() const;
83
84    MediaStreamVector getLocalStreams() const;
85
86    MediaStreamVector getRemoteStreams() const;
87
88    MediaStream* getStreamById(const String& streamId);
89
90    void addStream(PassRefPtr<MediaStream>, const Dictionary& mediaConstraints, ExceptionState&);
91
92    void removeStream(PassRefPtr<MediaStream>, ExceptionState&);
93
94    void getStats(PassRefPtr<RTCStatsCallback> successCallback, PassRefPtr<MediaStreamTrack> selector);
95
96    PassRefPtr<RTCDataChannel> createDataChannel(String label, const Dictionary& dataChannelDict, ExceptionState&);
97
98    PassRefPtr<RTCDTMFSender> createDTMFSender(PassRefPtr<MediaStreamTrack>, ExceptionState&);
99
100    void close(ExceptionState&);
101
102    DEFINE_ATTRIBUTE_EVENT_LISTENER(negotiationneeded);
103    DEFINE_ATTRIBUTE_EVENT_LISTENER(icecandidate);
104    DEFINE_ATTRIBUTE_EVENT_LISTENER(signalingstatechange);
105    DEFINE_ATTRIBUTE_EVENT_LISTENER(addstream);
106    DEFINE_ATTRIBUTE_EVENT_LISTENER(removestream);
107    DEFINE_ATTRIBUTE_EVENT_LISTENER(iceconnectionstatechange);
108    DEFINE_ATTRIBUTE_EVENT_LISTENER(datachannel);
109
110    // RTCPeerConnectionHandlerClient
111    virtual void negotiationNeeded() OVERRIDE;
112    virtual void didGenerateIceCandidate(WebKit::WebRTCICECandidate) OVERRIDE;
113    virtual void didChangeSignalingState(SignalingState) OVERRIDE;
114    virtual void didChangeIceGatheringState(IceGatheringState) OVERRIDE;
115    virtual void didChangeIceConnectionState(IceConnectionState) OVERRIDE;
116    virtual void didAddRemoteStream(PassRefPtr<MediaStreamDescriptor>) OVERRIDE;
117    virtual void didRemoveRemoteStream(MediaStreamDescriptor*) OVERRIDE;
118    virtual void didAddRemoteDataChannel(PassOwnPtr<RTCDataChannelHandler>) OVERRIDE;
119
120    // EventTarget
121    virtual const AtomicString& interfaceName() const OVERRIDE;
122    virtual ScriptExecutionContext* scriptExecutionContext() const OVERRIDE;
123
124    // ActiveDOMObject
125    virtual void stop() OVERRIDE;
126    virtual bool hasPendingActivity() const OVERRIDE { return !m_stopped; }
127
128    using RefCounted<RTCPeerConnection>::ref;
129    using RefCounted<RTCPeerConnection>::deref;
130
131private:
132    RTCPeerConnection(ScriptExecutionContext*, PassRefPtr<RTCConfiguration>, PassRefPtr<MediaConstraints>, ExceptionState&);
133
134    static PassRefPtr<RTCConfiguration> parseConfiguration(const Dictionary& configuration, ExceptionState&);
135    void scheduleDispatchEvent(PassRefPtr<Event>);
136    void scheduledEventTimerFired(Timer<RTCPeerConnection>*);
137    bool hasLocalStreamWithTrackId(const String& trackId);
138
139    // EventTarget implementation.
140    virtual EventTargetData* eventTargetData();
141    virtual EventTargetData* ensureEventTargetData();
142    virtual void refEventTarget() { ref(); }
143    virtual void derefEventTarget() { deref(); }
144    EventTargetData m_eventTargetData;
145
146    void changeSignalingState(SignalingState);
147    void changeIceGatheringState(IceGatheringState);
148    void changeIceConnectionState(IceConnectionState);
149
150    SignalingState m_signalingState;
151    IceGatheringState m_iceGatheringState;
152    IceConnectionState m_iceConnectionState;
153
154    MediaStreamVector m_localStreams;
155    MediaStreamVector m_remoteStreams;
156
157    Vector<RefPtr<RTCDataChannel> > m_dataChannels;
158
159    OwnPtr<RTCPeerConnectionHandler> m_peerHandler;
160
161    Timer<RTCPeerConnection> m_scheduledEventTimer;
162    Vector<RefPtr<Event> > m_scheduledEvents;
163
164    bool m_stopped;
165};
166
167} // namespace WebCore
168
169#endif // RTCPeerConnection_h
170