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/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"
49 class MediaStreamTrack;
50 class RTCConfiguration;
53 class RTCErrorCallback;
54 class RTCSessionDescription;
55 class RTCSessionDescriptionCallback;
56 class RTCStatsCallback;
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);
63 static PassRefPtrWillBeRawPtr<RTCPeerConnection> create(ExecutionContext*, const Dictionary& rtcConfiguration, const Dictionary& mediaConstraints, ExceptionState&);
64 virtual ~RTCPeerConnection();
66 void createOffer(PassOwnPtr<RTCSessionDescriptionCallback>, PassOwnPtr<RTCErrorCallback>, const Dictionary& mediaConstraints, ExceptionState&);
68 void createAnswer(PassOwnPtr<RTCSessionDescriptionCallback>, PassOwnPtr<RTCErrorCallback>, const Dictionary& mediaConstraints, ExceptionState&);
70 void setLocalDescription(PassRefPtrWillBeRawPtr<RTCSessionDescription>, PassOwnPtr<VoidCallback>, PassOwnPtr<RTCErrorCallback>, ExceptionState&);
71 PassRefPtrWillBeRawPtr<RTCSessionDescription> localDescription(ExceptionState&);
73 void setRemoteDescription(PassRefPtrWillBeRawPtr<RTCSessionDescription>, PassOwnPtr<VoidCallback>, PassOwnPtr<RTCErrorCallback>, ExceptionState&);
74 PassRefPtrWillBeRawPtr<RTCSessionDescription> remoteDescription(ExceptionState&);
76 String signalingState() const;
78 void updateIce(const Dictionary& rtcConfiguration, const Dictionary& mediaConstraints, ExceptionState&);
81 void addIceCandidate(RTCIceCandidate*, ExceptionState&);
83 void addIceCandidate(RTCIceCandidate*, PassOwnPtr<VoidCallback>, PassOwnPtr<RTCErrorCallback>, ExceptionState&);
85 String iceGatheringState() const;
87 String iceConnectionState() const;
89 MediaStreamVector getLocalStreams() const;
91 MediaStreamVector getRemoteStreams() const;
93 MediaStream* getStreamById(const String& streamId);
95 void addStream(PassRefPtrWillBeRawPtr<MediaStream>, const Dictionary& mediaConstraints, ExceptionState&);
97 void removeStream(PassRefPtrWillBeRawPtr<MediaStream>, ExceptionState&);
99 void getStats(PassOwnPtr<RTCStatsCallback> successCallback, PassRefPtr<MediaStreamTrack> selector);
101 PassRefPtrWillBeRawPtr<RTCDataChannel> createDataChannel(String label, const Dictionary& dataChannelDict, ExceptionState&);
103 PassRefPtrWillBeRawPtr<RTCDTMFSender> createDTMFSender(PassRefPtrWillBeRawPtr<MediaStreamTrack>, ExceptionState&);
105 void close(ExceptionState&);
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; }
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);
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;
131 virtual const AtomicString& interfaceName() const OVERRIDE;
132 virtual ExecutionContext* executionContext() const OVERRIDE;
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
141 return !m_closed && !m_stopped;
144 virtual void trace(Visitor*) OVERRIDE;
147 RTCPeerConnection(ExecutionContext*, PassRefPtr<RTCConfiguration>, blink::WebMediaConstraints, ExceptionState&);
149 static PassRefPtr<RTCConfiguration> parseConfiguration(const Dictionary& configuration, ExceptionState&);
150 void scheduleDispatchEvent(PassRefPtrWillBeRawPtr<Event>);
151 void dispatchScheduledEvent();
152 bool hasLocalStreamWithTrackId(const String& trackId);
154 void changeSignalingState(blink::WebRTCPeerConnectionHandlerClient::SignalingState);
155 void changeIceGatheringState(blink::WebRTCPeerConnectionHandlerClient::ICEGatheringState);
156 void changeIceConnectionState(blink::WebRTCPeerConnectionHandlerClient::ICEConnectionState);
158 SignalingState m_signalingState;
159 ICEGatheringState m_iceGatheringState;
160 ICEConnectionState m_iceConnectionState;
162 MediaStreamVector m_localStreams;
163 MediaStreamVector m_remoteStreams;
165 WillBeHeapVector<RefPtrWillBeMember<RTCDataChannel> > m_dataChannels;
167 OwnPtr<blink::WebRTCPeerConnectionHandler> m_peerHandler;
169 AsyncMethodRunner<RTCPeerConnection> m_dispatchScheduledEventRunner;
170 WillBeHeapVector<RefPtrWillBeMember<Event> > m_scheduledEvents;
176 } // namespace WebCore
178 #endif // RTCPeerConnection_h