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 DEFINE_WRAPPERTYPEINFO();
65 WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(RTCPeerConnection);
67 static RTCPeerConnection* create(ExecutionContext*, const Dictionary&, const Dictionary&, ExceptionState&);
68 virtual ~RTCPeerConnection();
70 void createOffer(RTCSessionDescriptionCallback*, RTCErrorCallback*, const Dictionary&, ExceptionState&);
72 void createAnswer(RTCSessionDescriptionCallback*, RTCErrorCallback*, const Dictionary&, ExceptionState&);
74 void setLocalDescription(RTCSessionDescription*, VoidCallback*, RTCErrorCallback*, ExceptionState&);
75 RTCSessionDescription* localDescription(ExceptionState&);
77 void setRemoteDescription(RTCSessionDescription*, VoidCallback*, RTCErrorCallback*, ExceptionState&);
78 RTCSessionDescription* remoteDescription(ExceptionState&);
80 String signalingState() const;
82 void updateIce(const Dictionary& rtcConfiguration, const Dictionary& mediaConstraints, ExceptionState&);
85 void addIceCandidate(RTCIceCandidate*, ExceptionState&);
87 void addIceCandidate(RTCIceCandidate*, VoidCallback*, RTCErrorCallback*, ExceptionState&);
89 String iceGatheringState() const;
91 String iceConnectionState() const;
93 MediaStreamVector getLocalStreams() const;
95 MediaStreamVector getRemoteStreams() const;
97 MediaStream* getStreamById(const String& streamId);
99 void addStream(MediaStream*, const Dictionary& mediaConstraints, ExceptionState&);
101 void removeStream(MediaStream*, ExceptionState&);
103 void getStats(RTCStatsCallback* successCallback, MediaStreamTrack* selector);
105 RTCDataChannel* createDataChannel(String label, const Dictionary& dataChannelDict, ExceptionState&);
107 RTCDTMFSender* createDTMFSender(MediaStreamTrack*, ExceptionState&);
109 void close(ExceptionState&);
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; }
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);
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;
136 virtual const AtomicString& interfaceName() const override;
137 virtual ExecutionContext* executionContext() const override;
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
146 return !m_closed && !m_stopped;
149 virtual void trace(Visitor*) override;
152 RTCPeerConnection(ExecutionContext*, RTCConfiguration*, WebMediaConstraints, ExceptionState&);
155 static RTCConfiguration* parseConfiguration(const Dictionary&, ExceptionState&);
156 static RTCOfferOptions* parseOfferOptions(const Dictionary&, ExceptionState&);
158 void scheduleDispatchEvent(PassRefPtrWillBeRawPtr<Event>);
159 void dispatchScheduledEvent();
160 bool hasLocalStreamWithTrackId(const String& trackId);
162 void changeSignalingState(WebRTCPeerConnectionHandlerClient::SignalingState);
163 void changeIceGatheringState(WebRTCPeerConnectionHandlerClient::ICEGatheringState);
164 void changeIceConnectionState(WebRTCPeerConnectionHandlerClient::ICEConnectionState);
166 void closeInternal();
168 SignalingState m_signalingState;
169 ICEGatheringState m_iceGatheringState;
170 ICEConnectionState m_iceConnectionState;
172 MediaStreamVector m_localStreams;
173 MediaStreamVector m_remoteStreams;
175 HeapVector<Member<RTCDataChannel> > m_dataChannels;
177 OwnPtr<WebRTCPeerConnectionHandler> m_peerHandler;
179 AsyncMethodRunner<RTCPeerConnection> m_dispatchScheduledEventRunner;
180 WillBeHeapVector<RefPtrWillBeMember<Event> > m_scheduledEvents;
188 #endif // RTCPeerConnection_h