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