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/core/v8/Dictionary.h"
35#include "core/dom/ActiveDOMObject.h"
36#include "modules/EventTargetModules.h"
37#include "modules/mediastream/MediaStream.h"
38#include "modules/mediastream/RTCIceCandidate.h"
39#include "platform/AsyncMethodRunner.h"
40#include "public/platform/WebMediaConstraints.h"
41#include "public/platform/WebRTCPeerConnectionHandler.h"
42#include "public/platform/WebRTCPeerConnectionHandlerClient.h"
43
44namespace blink {
45
46class ExceptionState;
47class MediaStreamTrack;
48class RTCConfiguration;
49class RTCDTMFSender;
50class RTCDataChannel;
51class RTCErrorCallback;
52class RTCOfferOptions;
53class RTCSessionDescription;
54class RTCSessionDescriptionCallback;
55class RTCStatsCallback;
56class VoidCallback;
57
58class RTCPeerConnection FINAL
59    : public RefCountedGarbageCollectedWillBeGarbageCollectedFinalized<RTCPeerConnection>
60    , public WebRTCPeerConnectionHandlerClient
61    , public EventTargetWithInlineData
62    , public ActiveDOMObject {
63    DEFINE_EVENT_TARGET_REFCOUNTING_WILL_BE_REMOVED(RefCountedGarbageCollectedWillBeGarbageCollectedFinalized<RTCPeerConnection>);
64    DEFINE_WRAPPERTYPEINFO();
65    WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(RTCPeerConnection);
66public:
67    static RTCPeerConnection* create(ExecutionContext*, const Dictionary&, const Dictionary&, ExceptionState&);
68    virtual ~RTCPeerConnection();
69
70    void createOffer(RTCSessionDescriptionCallback*, RTCErrorCallback*, const Dictionary&, ExceptionState&);
71
72    void createAnswer(RTCSessionDescriptionCallback*, RTCErrorCallback*, const Dictionary&, ExceptionState&);
73
74    void setLocalDescription(RTCSessionDescription*, VoidCallback*, RTCErrorCallback*, ExceptionState&);
75    RTCSessionDescription* localDescription(ExceptionState&);
76
77    void setRemoteDescription(RTCSessionDescription*, VoidCallback*, RTCErrorCallback*, ExceptionState&);
78    RTCSessionDescription* remoteDescription(ExceptionState&);
79
80    String signalingState() const;
81
82    void updateIce(const Dictionary& rtcConfiguration, const Dictionary& mediaConstraints, ExceptionState&);
83
84    // DEPRECATED
85    void addIceCandidate(RTCIceCandidate*, ExceptionState&);
86
87    void addIceCandidate(RTCIceCandidate*, VoidCallback*, RTCErrorCallback*, ExceptionState&);
88
89    String iceGatheringState() const;
90
91    String iceConnectionState() const;
92
93    MediaStreamVector getLocalStreams() const;
94
95    MediaStreamVector getRemoteStreams() const;
96
97    MediaStream* getStreamById(const String& streamId);
98
99    void addStream(MediaStream*, const Dictionary& mediaConstraints, ExceptionState&);
100
101    void removeStream(MediaStream*, ExceptionState&);
102
103    void getStats(RTCStatsCallback* successCallback, MediaStreamTrack* selector);
104
105    RTCDataChannel* createDataChannel(String label, const Dictionary& dataChannelDict, ExceptionState&);
106
107    RTCDTMFSender* createDTMFSender(MediaStreamTrack*, ExceptionState&);
108
109    void close(ExceptionState&);
110
111    // We allow getStats after close, but not other calls or callbacks.
112    bool shouldFireDefaultCallbacks() { return !m_closed && !m_stopped; }
113    bool shouldFireGetStatsCallback() { return !m_stopped; }
114
115    DEFINE_ATTRIBUTE_EVENT_LISTENER(negotiationneeded);
116    DEFINE_ATTRIBUTE_EVENT_LISTENER(icecandidate);
117    DEFINE_ATTRIBUTE_EVENT_LISTENER(signalingstatechange);
118    DEFINE_ATTRIBUTE_EVENT_LISTENER(addstream);
119    DEFINE_ATTRIBUTE_EVENT_LISTENER(removestream);
120    DEFINE_ATTRIBUTE_EVENT_LISTENER(iceconnectionstatechange);
121    DEFINE_ATTRIBUTE_EVENT_LISTENER(datachannel);
122
123    // WebRTCPeerConnectionHandlerClient
124    virtual void negotiationNeeded() OVERRIDE;
125    virtual void didGenerateICECandidate(const WebRTCICECandidate&) OVERRIDE;
126    virtual void didChangeSignalingState(SignalingState) OVERRIDE;
127    virtual void didChangeICEGatheringState(ICEGatheringState) OVERRIDE;
128    virtual void didChangeICEConnectionState(ICEConnectionState) OVERRIDE;
129    virtual void didAddRemoteStream(const WebMediaStream&) OVERRIDE;
130    virtual void didRemoveRemoteStream(const WebMediaStream&) OVERRIDE;
131    virtual void didAddRemoteDataChannel(WebRTCDataChannelHandler*) OVERRIDE;
132    virtual void releasePeerConnectionHandler() OVERRIDE;
133    virtual void closePeerConnection() OVERRIDE;
134
135    // EventTarget
136    virtual const AtomicString& interfaceName() const OVERRIDE;
137    virtual ExecutionContext* executionContext() const OVERRIDE;
138
139    // ActiveDOMObject
140    virtual void suspend() OVERRIDE;
141    virtual void resume() OVERRIDE;
142    virtual void stop() OVERRIDE;
143    // We keep the this object alive until either stopped or closed.
144    virtual bool hasPendingActivity() const OVERRIDE
145    {
146        return !m_closed && !m_stopped;
147    }
148
149    virtual void trace(Visitor*) OVERRIDE;
150
151private:
152    RTCPeerConnection(ExecutionContext*, RTCConfiguration*, WebMediaConstraints, ExceptionState&);
153
154
155    static RTCConfiguration* parseConfiguration(const Dictionary&, ExceptionState&);
156    static RTCOfferOptions* parseOfferOptions(const Dictionary&, ExceptionState&);
157
158    void scheduleDispatchEvent(PassRefPtrWillBeRawPtr<Event>);
159    void dispatchScheduledEvent();
160    bool hasLocalStreamWithTrackId(const String& trackId);
161
162    void changeSignalingState(WebRTCPeerConnectionHandlerClient::SignalingState);
163    void changeIceGatheringState(WebRTCPeerConnectionHandlerClient::ICEGatheringState);
164    void changeIceConnectionState(WebRTCPeerConnectionHandlerClient::ICEConnectionState);
165
166    void closeInternal();
167
168    SignalingState m_signalingState;
169    ICEGatheringState m_iceGatheringState;
170    ICEConnectionState m_iceConnectionState;
171
172    MediaStreamVector m_localStreams;
173    MediaStreamVector m_remoteStreams;
174
175    HeapVector<Member<RTCDataChannel> > m_dataChannels;
176
177    OwnPtr<WebRTCPeerConnectionHandler> m_peerHandler;
178
179    AsyncMethodRunner<RTCPeerConnection> m_dispatchScheduledEventRunner;
180    WillBeHeapVector<RefPtrWillBeMember<Event> > m_scheduledEvents;
181
182    bool m_stopped;
183    bool m_closed;
184};
185
186} // namespace blink
187
188#endif // RTCPeerConnection_h
189