2 * Copyright (C) 2012 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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
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.
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.
31 #ifndef RTCPeerConnection_h
32 #define RTCPeerConnection_h
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"
47 class MediaStreamTrack;
48 class RTCConfiguration;
51 class RTCErrorCallback;
52 class RTCOfferOptions;
53 class RTCSessionDescription;
54 class RTCSessionDescriptionCallback;
55 class RTCStatsCallback;
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);
66 static RTCPeerConnection* create(ExecutionContext*, const Dictionary&, const Dictionary&, ExceptionState&);
67 virtual ~RTCPeerConnection();
69 void createOffer(PassOwnPtr<RTCSessionDescriptionCallback>, PassOwnPtr<RTCErrorCallback>, const Dictionary&, ExceptionState&);
71 void createAnswer(PassOwnPtr<RTCSessionDescriptionCallback>, PassOwnPtr<RTCErrorCallback>, const Dictionary&, ExceptionState&);
73 void setLocalDescription(RTCSessionDescription*, PassOwnPtr<VoidCallback>, PassOwnPtr<RTCErrorCallback>, ExceptionState&);
74 RTCSessionDescription* localDescription(ExceptionState&);
76 void setRemoteDescription(RTCSessionDescription*, PassOwnPtr<VoidCallback>, PassOwnPtr<RTCErrorCallback>, ExceptionState&);
77 RTCSessionDescription* remoteDescription(ExceptionState&);
79 String signalingState() const;
81 void updateIce(const Dictionary& rtcConfiguration, const Dictionary& mediaConstraints, ExceptionState&);
84 void addIceCandidate(RTCIceCandidate*, ExceptionState&);
86 void addIceCandidate(RTCIceCandidate*, PassOwnPtr<VoidCallback>, PassOwnPtr<RTCErrorCallback>, ExceptionState&);
88 String iceGatheringState() const;
90 String iceConnectionState() const;
92 MediaStreamVector getLocalStreams() const;
94 MediaStreamVector getRemoteStreams() const;
96 MediaStream* getStreamById(const String& streamId);
98 void addStream(MediaStream*, const Dictionary& mediaConstraints, ExceptionState&);
100 void removeStream(MediaStream*, ExceptionState&);
102 void getStats(PassOwnPtr<RTCStatsCallback> successCallback, MediaStreamTrack* selector);
104 RTCDataChannel* createDataChannel(String label, const Dictionary& dataChannelDict, ExceptionState&);
106 RTCDTMFSender* createDTMFSender(MediaStreamTrack*, ExceptionState&);
108 void close(ExceptionState&);
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; }
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);
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;
134 virtual const AtomicString& interfaceName() const OVERRIDE;
135 virtual ExecutionContext* executionContext() const OVERRIDE;
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
144 return !m_closed && !m_stopped;
147 virtual void trace(Visitor*) OVERRIDE;
150 RTCPeerConnection(ExecutionContext*, PassRefPtr<RTCConfiguration>, WebMediaConstraints, ExceptionState&);
152 static PassRefPtr<RTCConfiguration> parseConfiguration(const Dictionary&, ExceptionState&);
153 static PassRefPtr<RTCOfferOptions> parseOfferOptions(const Dictionary&, ExceptionState&);
155 void scheduleDispatchEvent(PassRefPtrWillBeRawPtr<Event>);
156 void dispatchScheduledEvent();
157 bool hasLocalStreamWithTrackId(const String& trackId);
159 void changeSignalingState(WebRTCPeerConnectionHandlerClient::SignalingState);
160 void changeIceGatheringState(WebRTCPeerConnectionHandlerClient::ICEGatheringState);
161 void changeIceConnectionState(WebRTCPeerConnectionHandlerClient::ICEConnectionState);
163 SignalingState m_signalingState;
164 ICEGatheringState m_iceGatheringState;
165 ICEConnectionState m_iceConnectionState;
167 MediaStreamVector m_localStreams;
168 MediaStreamVector m_remoteStreams;
170 HeapVector<Member<RTCDataChannel> > m_dataChannels;
172 OwnPtr<WebRTCPeerConnectionHandler> m_peerHandler;
174 AsyncMethodRunner<RTCPeerConnection> m_dispatchScheduledEventRunner;
175 WillBeHeapVector<RefPtrWillBeMember<Event> > m_scheduledEvents;
183 #endif // RTCPeerConnection_h