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 "core/events/EventTarget.h" 38 #include "core/platform/mediastream/RTCPeerConnectionHandler.h" 39 #include "core/platform/mediastream/RTCPeerConnectionHandlerClient.h" 40 #include "modules/mediastream/MediaStream.h" 41 #include "modules/mediastream/RTCIceCandidate.h" 42 #include "platform/AsyncMethodRunner.h" 43 #include "wtf/RefCounted.h" 44 45 namespace WebCore { 46 47 class ExceptionState; 48 class MediaConstraints; 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 : public RefCounted<RTCPeerConnection>, public ScriptWrappable, public RTCPeerConnectionHandlerClient, public EventTargetWithInlineData, public ActiveDOMObject { 60 REFCOUNTED_EVENT_TARGET(RTCPeerConnection); 61 public: 62 static PassRefPtr<RTCPeerConnection> create(ExecutionContext*, const Dictionary& rtcConfiguration, const Dictionary& mediaConstraints, ExceptionState&); 63 ~RTCPeerConnection(); 64 65 void createOffer(PassOwnPtr<RTCSessionDescriptionCallback>, PassOwnPtr<RTCErrorCallback>, const Dictionary& mediaConstraints, ExceptionState&); 66 67 void createAnswer(PassOwnPtr<RTCSessionDescriptionCallback>, PassOwnPtr<RTCErrorCallback>, const Dictionary& mediaConstraints, ExceptionState&); 68 69 void setLocalDescription(PassRefPtr<RTCSessionDescription>, PassOwnPtr<VoidCallback>, PassOwnPtr<RTCErrorCallback>, ExceptionState&); 70 PassRefPtr<RTCSessionDescription> localDescription(ExceptionState&); 71 72 void setRemoteDescription(PassRefPtr<RTCSessionDescription>, PassOwnPtr<VoidCallback>, PassOwnPtr<RTCErrorCallback>, ExceptionState&); 73 PassRefPtr<RTCSessionDescription> remoteDescription(ExceptionState&); 74 75 String signalingState() const; 76 77 void updateIce(const Dictionary& rtcConfiguration, const Dictionary& mediaConstraints, ExceptionState&); 78 79 // DEPRECATED 80 void addIceCandidate(RTCIceCandidate*, ExceptionState&); 81 82 void addIceCandidate(RTCIceCandidate*, PassOwnPtr<VoidCallback>, PassOwnPtr<RTCErrorCallback>, ExceptionState&); 83 84 String iceGatheringState() const; 85 86 String iceConnectionState() const; 87 88 MediaStreamVector getLocalStreams() const; 89 90 MediaStreamVector getRemoteStreams() const; 91 92 MediaStream* getStreamById(const String& streamId); 93 94 void addStream(PassRefPtr<MediaStream>, const Dictionary& mediaConstraints, ExceptionState&); 95 96 void removeStream(PassRefPtr<MediaStream>, ExceptionState&); 97 98 void getStats(PassOwnPtr<RTCStatsCallback> successCallback, PassRefPtr<MediaStreamTrack> selector); 99 100 PassRefPtr<RTCDataChannel> createDataChannel(String label, const Dictionary& dataChannelDict, ExceptionState&); 101 102 PassRefPtr<RTCDTMFSender> createDTMFSender(PassRefPtr<MediaStreamTrack>, ExceptionState&); 103 104 void close(ExceptionState&); 105 106 DEFINE_ATTRIBUTE_EVENT_LISTENER(negotiationneeded); 107 DEFINE_ATTRIBUTE_EVENT_LISTENER(icecandidate); 108 DEFINE_ATTRIBUTE_EVENT_LISTENER(signalingstatechange); 109 DEFINE_ATTRIBUTE_EVENT_LISTENER(addstream); 110 DEFINE_ATTRIBUTE_EVENT_LISTENER(removestream); 111 DEFINE_ATTRIBUTE_EVENT_LISTENER(iceconnectionstatechange); 112 DEFINE_ATTRIBUTE_EVENT_LISTENER(datachannel); 113 114 // RTCPeerConnectionHandlerClient 115 virtual void negotiationNeeded() OVERRIDE; 116 virtual void didGenerateIceCandidate(blink::WebRTCICECandidate) OVERRIDE; 117 virtual void didChangeSignalingState(SignalingState) OVERRIDE; 118 virtual void didChangeIceGatheringState(IceGatheringState) OVERRIDE; 119 virtual void didChangeIceConnectionState(IceConnectionState) OVERRIDE; 120 virtual void didAddRemoteStream(PassRefPtr<MediaStreamDescriptor>) OVERRIDE; 121 virtual void didRemoveRemoteStream(MediaStreamDescriptor*) OVERRIDE; 122 virtual void didAddRemoteDataChannel(PassOwnPtr<RTCDataChannelHandler>) OVERRIDE; 123 124 // EventTarget 125 virtual const AtomicString& interfaceName() const OVERRIDE; 126 virtual ExecutionContext* executionContext() const OVERRIDE; 127 128 // ActiveDOMObject 129 virtual void suspend() OVERRIDE; 130 virtual void resume() OVERRIDE; 131 virtual void stop() OVERRIDE; hasPendingActivity()132 virtual bool hasPendingActivity() const OVERRIDE { return !m_stopped; } 133 134 private: 135 RTCPeerConnection(ExecutionContext*, PassRefPtr<RTCConfiguration>, PassRefPtr<MediaConstraints>, ExceptionState&); 136 137 static PassRefPtr<RTCConfiguration> parseConfiguration(const Dictionary& configuration, ExceptionState&); 138 void scheduleDispatchEvent(PassRefPtr<Event>); 139 void dispatchScheduledEvent(); 140 bool hasLocalStreamWithTrackId(const String& trackId); 141 142 void changeSignalingState(SignalingState); 143 void changeIceGatheringState(IceGatheringState); 144 void changeIceConnectionState(IceConnectionState); 145 146 SignalingState m_signalingState; 147 IceGatheringState m_iceGatheringState; 148 IceConnectionState m_iceConnectionState; 149 150 MediaStreamVector m_localStreams; 151 MediaStreamVector m_remoteStreams; 152 153 Vector<RefPtr<RTCDataChannel> > m_dataChannels; 154 155 OwnPtr<RTCPeerConnectionHandler> m_peerHandler; 156 157 AsyncMethodRunner<RTCPeerConnection> m_dispatchScheduledEventRunner; 158 Vector<RefPtr<Event> > m_scheduledEvents; 159 160 bool m_stopped; 161 }; 162 163 } // namespace WebCore 164 165 #endif // RTCPeerConnection_h 166