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