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.
32 #include "modules/mediastream/RTCPeerConnection.h"
34 #include "bindings/core/v8/ArrayValue.h"
35 #include "bindings/core/v8/ExceptionMessages.h"
36 #include "bindings/core/v8/ExceptionState.h"
37 #include "core/dom/Document.h"
38 #include "core/dom/ExceptionCode.h"
39 #include "core/dom/ExecutionContext.h"
40 #include "core/frame/LocalFrame.h"
41 #include "core/html/VoidCallback.h"
42 #include "core/loader/FrameLoader.h"
43 #include "core/loader/FrameLoaderClient.h"
44 #include "modules/mediastream/MediaConstraintsImpl.h"
45 #include "modules/mediastream/MediaStreamEvent.h"
46 #include "modules/mediastream/RTCDTMFSender.h"
47 #include "modules/mediastream/RTCDataChannel.h"
48 #include "modules/mediastream/RTCDataChannelEvent.h"
49 #include "modules/mediastream/RTCErrorCallback.h"
50 #include "modules/mediastream/RTCIceCandidateEvent.h"
51 #include "modules/mediastream/RTCSessionDescription.h"
52 #include "modules/mediastream/RTCSessionDescriptionCallback.h"
53 #include "modules/mediastream/RTCSessionDescriptionRequestImpl.h"
54 #include "modules/mediastream/RTCStatsCallback.h"
55 #include "modules/mediastream/RTCStatsRequestImpl.h"
56 #include "modules/mediastream/RTCVoidRequestImpl.h"
57 #include "platform/mediastream/RTCConfiguration.h"
58 #include "platform/mediastream/RTCOfferOptions.h"
59 #include "public/platform/Platform.h"
60 #include "public/platform/WebMediaStream.h"
61 #include "public/platform/WebRTCConfiguration.h"
62 #include "public/platform/WebRTCDataChannelHandler.h"
63 #include "public/platform/WebRTCDataChannelInit.h"
64 #include "public/platform/WebRTCICECandidate.h"
65 #include "public/platform/WebRTCOfferOptions.h"
66 #include "public/platform/WebRTCSessionDescription.h"
67 #include "public/platform/WebRTCSessionDescriptionRequest.h"
68 #include "public/platform/WebRTCStatsRequest.h"
69 #include "public/platform/WebRTCVoidRequest.h"
75 static bool throwExceptionIfSignalingStateClosed(RTCPeerConnection::SignalingState state, ExceptionState& exceptionState)
77 if (state == RTCPeerConnection::SignalingStateClosed) {
78 exceptionState.throwDOMException(InvalidStateError, "The RTCPeerConnection's signalingState is 'closed'.");
87 RTCConfiguration* RTCPeerConnection::parseConfiguration(const Dictionary& configuration, ExceptionState& exceptionState)
89 if (configuration.isUndefinedOrNull())
92 RTCIceTransports iceTransports = RTCIceTransportsAll;
93 String iceTransportsString;
94 if (DictionaryHelper::get(configuration, "iceTransports", iceTransportsString)) {
95 if (iceTransportsString == "none") {
96 iceTransports = RTCIceTransportsNone;
97 } else if (iceTransportsString == "relay") {
98 iceTransports = RTCIceTransportsRelay;
99 } else if (iceTransportsString != "all") {
100 exceptionState.throwTypeError("Malformed RTCIceTransports");
105 ArrayValue iceServers;
106 bool ok = DictionaryHelper::get(configuration, "iceServers", iceServers);
107 if (!ok || iceServers.isUndefinedOrNull()) {
108 exceptionState.throwTypeError("Malformed RTCConfiguration");
112 size_t numberOfServers;
113 ok = iceServers.length(numberOfServers);
115 exceptionState.throwTypeError("Malformed RTCConfiguration");
119 RTCConfiguration* rtcConfiguration = RTCConfiguration::create();
120 rtcConfiguration->setIceTransports(iceTransports);
122 for (size_t i = 0; i < numberOfServers; ++i) {
123 Dictionary iceServer;
124 ok = iceServers.get(i, iceServer);
126 exceptionState.throwTypeError("Malformed RTCIceServer");
130 Vector<String> names;
131 iceServer.getOwnPropertyNames(names);
133 Vector<String> urlStrings;
134 if (names.contains("urls")) {
135 if (!DictionaryHelper::get(iceServer, "urls", urlStrings) || !urlStrings.size()) {
137 if (DictionaryHelper::get(iceServer, "urls", urlString)) {
138 urlStrings.append(urlString);
140 exceptionState.throwTypeError("Malformed RTCIceServer");
144 } else if (names.contains("url")) {
146 if (DictionaryHelper::get(iceServer, "url", urlString)) {
147 urlStrings.append(urlString);
149 exceptionState.throwTypeError("Malformed RTCIceServer");
153 exceptionState.throwTypeError("Malformed RTCIceServer");
157 String username, credential;
158 DictionaryHelper::get(iceServer, "username", username);
159 DictionaryHelper::get(iceServer, "credential", credential);
161 for (Vector<String>::iterator iter = urlStrings.begin(); iter != urlStrings.end(); ++iter) {
162 KURL url(KURL(), *iter);
163 if (!url.isValid() || !(url.protocolIs("turn") || url.protocolIs("turns") || url.protocolIs("stun"))) {
164 exceptionState.throwTypeError("Malformed URL");
168 rtcConfiguration->appendServer(RTCIceServer::create(url, username, credential));
172 return rtcConfiguration;
175 RTCOfferOptions* RTCPeerConnection::parseOfferOptions(const Dictionary& options, ExceptionState& exceptionState)
177 if (options.isUndefinedOrNull())
180 Vector<String> propertyNames;
181 options.getOwnPropertyNames(propertyNames);
183 // Treat |options| as MediaConstraints if it is empty or has "optional" or "mandatory" properties for compatibility.
184 // TODO(jiayl): remove constraints when RTCOfferOptions reaches Stable and client code is ready.
185 if (propertyNames.isEmpty() || propertyNames.contains("optional") || propertyNames.contains("mandatory"))
188 int32_t offerToReceiveVideo = -1;
189 int32_t offerToReceiveAudio = -1;
190 bool voiceActivityDetection = true;
191 bool iceRestart = false;
193 if (DictionaryHelper::get(options, "offerToReceiveVideo", offerToReceiveVideo) && offerToReceiveVideo < 0) {
194 exceptionState.throwTypeError("Invalid offerToReceiveVideo");
198 if (DictionaryHelper::get(options, "offerToReceiveAudio", offerToReceiveAudio) && offerToReceiveAudio < 0) {
199 exceptionState.throwTypeError("Invalid offerToReceiveAudio");
203 DictionaryHelper::get(options, "voiceActivityDetection", voiceActivityDetection);
204 DictionaryHelper::get(options, "iceRestart", iceRestart);
206 RTCOfferOptions* rtcOfferOptions = RTCOfferOptions::create(offerToReceiveVideo, offerToReceiveAudio, voiceActivityDetection, iceRestart);
207 return rtcOfferOptions;
210 RTCPeerConnection* RTCPeerConnection::create(ExecutionContext* context, const Dictionary& rtcConfiguration, const Dictionary& mediaConstraints, ExceptionState& exceptionState)
212 RTCConfiguration* configuration = parseConfiguration(rtcConfiguration, exceptionState);
213 if (exceptionState.hadException())
216 WebMediaConstraints constraints = MediaConstraintsImpl::create(mediaConstraints, exceptionState);
217 if (exceptionState.hadException())
220 RTCPeerConnection* peerConnection = new RTCPeerConnection(context, configuration, constraints, exceptionState);
221 peerConnection->suspendIfNeeded();
222 if (exceptionState.hadException())
225 return peerConnection;
228 RTCPeerConnection::RTCPeerConnection(ExecutionContext* context, RTCConfiguration* configuration, WebMediaConstraints constraints, ExceptionState& exceptionState)
229 : ActiveDOMObject(context)
230 , m_signalingState(SignalingStateStable)
231 , m_iceGatheringState(ICEGatheringStateNew)
232 , m_iceConnectionState(ICEConnectionStateNew)
233 , m_dispatchScheduledEventRunner(this, &RTCPeerConnection::dispatchScheduledEvent)
237 Document* document = toDocument(executionContext());
239 // If we fail, set |m_closed| and |m_stopped| to true, to avoid hitting the assert in the destructor.
241 if (!document->frame()) {
244 exceptionState.throwDOMException(NotSupportedError, "PeerConnections may not be created in detached documents.");
248 m_peerHandler = adoptPtr(Platform::current()->createRTCPeerConnectionHandler(this));
249 if (!m_peerHandler) {
252 exceptionState.throwDOMException(NotSupportedError, "No PeerConnection handler can be created, perhaps WebRTC is disabled?");
256 document->frame()->loader().client()->dispatchWillStartUsingPeerConnectionHandler(m_peerHandler.get());
258 if (!m_peerHandler->initialize(configuration, constraints)) {
261 exceptionState.throwDOMException(NotSupportedError, "Failed to initialize native PeerConnection.");
266 RTCPeerConnection::~RTCPeerConnection()
268 // This checks that close() or stop() is called before the destructor.
269 // We are assuming that a wrapper is always created when RTCPeerConnection is created.
270 ASSERT(m_closed || m_stopped);
273 void RTCPeerConnection::createOffer(RTCSessionDescriptionCallback* successCallback, RTCErrorCallback* errorCallback, const Dictionary& rtcOfferOptions, ExceptionState& exceptionState)
275 if (throwExceptionIfSignalingStateClosed(m_signalingState, exceptionState))
278 ASSERT(successCallback);
280 RTCOfferOptions* offerOptions = parseOfferOptions(rtcOfferOptions, exceptionState);
281 if (exceptionState.hadException())
284 RTCSessionDescriptionRequest* request = RTCSessionDescriptionRequestImpl::create(executionContext(), this, successCallback, errorCallback);
287 m_peerHandler->createOffer(request, offerOptions);
289 WebMediaConstraints constraints = MediaConstraintsImpl::create(rtcOfferOptions, exceptionState);
290 if (exceptionState.hadException())
293 m_peerHandler->createOffer(request, constraints);
297 void RTCPeerConnection::createAnswer(RTCSessionDescriptionCallback* successCallback, RTCErrorCallback* errorCallback, const Dictionary& mediaConstraints, ExceptionState& exceptionState)
299 if (throwExceptionIfSignalingStateClosed(m_signalingState, exceptionState))
302 ASSERT(successCallback);
304 WebMediaConstraints constraints = MediaConstraintsImpl::create(mediaConstraints, exceptionState);
305 if (exceptionState.hadException())
308 RTCSessionDescriptionRequest* request = RTCSessionDescriptionRequestImpl::create(executionContext(), this, successCallback, errorCallback);
309 m_peerHandler->createAnswer(request, constraints);
312 void RTCPeerConnection::setLocalDescription(RTCSessionDescription* sessionDescription, VoidCallback* successCallback, RTCErrorCallback* errorCallback, ExceptionState& exceptionState)
314 if (throwExceptionIfSignalingStateClosed(m_signalingState, exceptionState))
317 if (!sessionDescription) {
318 exceptionState.throwDOMException(TypeMismatchError, ExceptionMessages::argumentNullOrIncorrectType(1, "RTCSessionDescription"));
322 RTCVoidRequest* request = RTCVoidRequestImpl::create(executionContext(), this, successCallback, errorCallback);
323 m_peerHandler->setLocalDescription(request, sessionDescription->webSessionDescription());
326 RTCSessionDescription* RTCPeerConnection::localDescription(ExceptionState& exceptionState)
328 WebRTCSessionDescription webSessionDescription = m_peerHandler->localDescription();
329 if (webSessionDescription.isNull())
332 return RTCSessionDescription::create(webSessionDescription);
335 void RTCPeerConnection::setRemoteDescription(RTCSessionDescription* sessionDescription, VoidCallback* successCallback, RTCErrorCallback* errorCallback, ExceptionState& exceptionState)
337 if (throwExceptionIfSignalingStateClosed(m_signalingState, exceptionState))
340 if (!sessionDescription) {
341 exceptionState.throwDOMException(TypeMismatchError, ExceptionMessages::argumentNullOrIncorrectType(1, "RTCSessionDescription"));
345 RTCVoidRequest* request = RTCVoidRequestImpl::create(executionContext(), this, successCallback, errorCallback);
346 m_peerHandler->setRemoteDescription(request, sessionDescription->webSessionDescription());
349 RTCSessionDescription* RTCPeerConnection::remoteDescription(ExceptionState& exceptionState)
351 WebRTCSessionDescription webSessionDescription = m_peerHandler->remoteDescription();
352 if (webSessionDescription.isNull())
355 return RTCSessionDescription::create(webSessionDescription);
358 void RTCPeerConnection::updateIce(const Dictionary& rtcConfiguration, const Dictionary& mediaConstraints, ExceptionState& exceptionState)
360 if (throwExceptionIfSignalingStateClosed(m_signalingState, exceptionState))
363 RTCConfiguration* configuration = parseConfiguration(rtcConfiguration, exceptionState);
364 if (exceptionState.hadException())
367 WebMediaConstraints constraints = MediaConstraintsImpl::create(mediaConstraints, exceptionState);
368 if (exceptionState.hadException())
371 bool valid = m_peerHandler->updateICE(configuration, constraints);
373 exceptionState.throwDOMException(SyntaxError, "Could not update the ICE Agent with the given configuration.");
376 void RTCPeerConnection::addIceCandidate(RTCIceCandidate* iceCandidate, ExceptionState& exceptionState)
378 if (throwExceptionIfSignalingStateClosed(m_signalingState, exceptionState))
382 exceptionState.throwDOMException(TypeMismatchError, ExceptionMessages::argumentNullOrIncorrectType(1, "RTCIceCandidate"));
386 bool valid = m_peerHandler->addICECandidate(iceCandidate->webCandidate());
388 exceptionState.throwDOMException(SyntaxError, "The ICE candidate could not be added.");
391 void RTCPeerConnection::addIceCandidate(RTCIceCandidate* iceCandidate, VoidCallback* successCallback, RTCErrorCallback* errorCallback, ExceptionState& exceptionState)
393 if (throwExceptionIfSignalingStateClosed(m_signalingState, exceptionState))
397 exceptionState.throwDOMException(TypeMismatchError, ExceptionMessages::argumentNullOrIncorrectType(1, "RTCIceCandidate"));
400 ASSERT(successCallback);
401 ASSERT(errorCallback);
403 RTCVoidRequest* request = RTCVoidRequestImpl::create(executionContext(), this, successCallback, errorCallback);
405 bool implemented = m_peerHandler->addICECandidate(request, iceCandidate->webCandidate());
407 exceptionState.throwDOMException(NotSupportedError, "This method is not yet implemented.");
411 String RTCPeerConnection::signalingState() const
413 switch (m_signalingState) {
414 case SignalingStateStable:
416 case SignalingStateHaveLocalOffer:
417 return "have-local-offer";
418 case SignalingStateHaveRemoteOffer:
419 return "have-remote-offer";
420 case SignalingStateHaveLocalPrAnswer:
421 return "have-local-pranswer";
422 case SignalingStateHaveRemotePrAnswer:
423 return "have-remote-pranswer";
424 case SignalingStateClosed:
428 ASSERT_NOT_REACHED();
432 String RTCPeerConnection::iceGatheringState() const
434 switch (m_iceGatheringState) {
435 case ICEGatheringStateNew:
437 case ICEGatheringStateGathering:
439 case ICEGatheringStateComplete:
443 ASSERT_NOT_REACHED();
447 String RTCPeerConnection::iceConnectionState() const
449 switch (m_iceConnectionState) {
450 case ICEConnectionStateNew:
452 case ICEConnectionStateChecking:
454 case ICEConnectionStateConnected:
456 case ICEConnectionStateCompleted:
458 case ICEConnectionStateFailed:
460 case ICEConnectionStateDisconnected:
461 return "disconnected";
462 case ICEConnectionStateClosed:
466 ASSERT_NOT_REACHED();
470 void RTCPeerConnection::addStream(MediaStream* stream, const Dictionary& mediaConstraints, ExceptionState& exceptionState)
472 if (throwExceptionIfSignalingStateClosed(m_signalingState, exceptionState))
476 exceptionState.throwDOMException(TypeMismatchError, ExceptionMessages::argumentNullOrIncorrectType(1, "MediaStream"));
480 if (m_localStreams.contains(stream))
483 WebMediaConstraints constraints = MediaConstraintsImpl::create(mediaConstraints, exceptionState);
484 if (exceptionState.hadException())
487 m_localStreams.append(stream);
489 bool valid = m_peerHandler->addStream(stream->descriptor(), constraints);
491 exceptionState.throwDOMException(SyntaxError, "Unable to add the provided stream.");
494 void RTCPeerConnection::removeStream(MediaStream* stream, ExceptionState& exceptionState)
496 if (throwExceptionIfSignalingStateClosed(m_signalingState, exceptionState))
500 exceptionState.throwDOMException(TypeMismatchError, ExceptionMessages::argumentNullOrIncorrectType(1, "MediaStream"));
504 size_t pos = m_localStreams.find(stream);
505 if (pos == kNotFound)
508 m_localStreams.remove(pos);
510 m_peerHandler->removeStream(stream->descriptor());
513 MediaStreamVector RTCPeerConnection::getLocalStreams() const
515 return m_localStreams;
518 MediaStreamVector RTCPeerConnection::getRemoteStreams() const
520 return m_remoteStreams;
523 MediaStream* RTCPeerConnection::getStreamById(const String& streamId)
525 for (MediaStreamVector::iterator iter = m_localStreams.begin(); iter != m_localStreams.end(); ++iter) {
526 if ((*iter)->id() == streamId)
530 for (MediaStreamVector::iterator iter = m_remoteStreams.begin(); iter != m_remoteStreams.end(); ++iter) {
531 if ((*iter)->id() == streamId)
538 void RTCPeerConnection::getStats(RTCStatsCallback* successCallback, MediaStreamTrack* selector)
540 RTCStatsRequest* statsRequest = RTCStatsRequestImpl::create(executionContext(), this, successCallback, selector);
541 // FIXME: Add passing selector as part of the statsRequest.
542 m_peerHandler->getStats(statsRequest);
545 RTCDataChannel* RTCPeerConnection::createDataChannel(String label, const Dictionary& options, ExceptionState& exceptionState)
547 if (throwExceptionIfSignalingStateClosed(m_signalingState, exceptionState))
550 WebRTCDataChannelInit init;
551 DictionaryHelper::get(options, "ordered", init.ordered);
552 DictionaryHelper::get(options, "negotiated", init.negotiated);
554 unsigned short value = 0;
555 if (DictionaryHelper::get(options, "id", value))
557 if (DictionaryHelper::get(options, "maxRetransmits", value))
558 init.maxRetransmits = value;
559 if (DictionaryHelper::get(options, "maxRetransmitTime", value))
560 init.maxRetransmitTime = value;
562 String protocolString;
563 DictionaryHelper::get(options, "protocol", protocolString);
564 init.protocol = protocolString;
566 RTCDataChannel* channel = RTCDataChannel::create(executionContext(), this, m_peerHandler.get(), label, init, exceptionState);
567 if (exceptionState.hadException())
569 m_dataChannels.append(channel);
573 bool RTCPeerConnection::hasLocalStreamWithTrackId(const String& trackId)
575 for (MediaStreamVector::iterator iter = m_localStreams.begin(); iter != m_localStreams.end(); ++iter) {
576 if ((*iter)->getTrackById(trackId))
582 RTCDTMFSender* RTCPeerConnection::createDTMFSender(MediaStreamTrack* track, ExceptionState& exceptionState)
584 if (throwExceptionIfSignalingStateClosed(m_signalingState, exceptionState))
588 exceptionState.throwTypeError(ExceptionMessages::argumentNullOrIncorrectType(1, "MediaStreamTrack"));
592 if (!hasLocalStreamWithTrackId(track->id())) {
593 exceptionState.throwDOMException(SyntaxError, "No local stream is available for the track provided.");
597 RTCDTMFSender* dtmfSender = RTCDTMFSender::create(executionContext(), m_peerHandler.get(), track, exceptionState);
598 if (exceptionState.hadException())
603 void RTCPeerConnection::close(ExceptionState& exceptionState)
605 if (throwExceptionIfSignalingStateClosed(m_signalingState, exceptionState))
611 void RTCPeerConnection::negotiationNeeded()
614 scheduleDispatchEvent(Event::create(EventTypeNames::negotiationneeded));
617 void RTCPeerConnection::didGenerateICECandidate(const WebRTCICECandidate& webCandidate)
620 ASSERT(executionContext()->isContextThread());
621 if (webCandidate.isNull())
622 scheduleDispatchEvent(RTCIceCandidateEvent::create(false, false, nullptr));
624 RTCIceCandidate* iceCandidate = RTCIceCandidate::create(webCandidate);
625 scheduleDispatchEvent(RTCIceCandidateEvent::create(false, false, iceCandidate));
629 void RTCPeerConnection::didChangeSignalingState(SignalingState newState)
632 ASSERT(executionContext()->isContextThread());
633 changeSignalingState(newState);
636 void RTCPeerConnection::didChangeICEGatheringState(ICEGatheringState newState)
639 ASSERT(executionContext()->isContextThread());
640 changeIceGatheringState(newState);
643 void RTCPeerConnection::didChangeICEConnectionState(ICEConnectionState newState)
646 ASSERT(executionContext()->isContextThread());
647 changeIceConnectionState(newState);
650 void RTCPeerConnection::didAddRemoteStream(const WebMediaStream& remoteStream)
653 ASSERT(executionContext()->isContextThread());
655 if (m_signalingState == SignalingStateClosed)
658 MediaStream* stream = MediaStream::create(executionContext(), remoteStream);
659 m_remoteStreams.append(stream);
661 scheduleDispatchEvent(MediaStreamEvent::create(EventTypeNames::addstream, false, false, stream));
664 void RTCPeerConnection::didRemoveRemoteStream(const WebMediaStream& remoteStream)
667 ASSERT(executionContext()->isContextThread());
669 MediaStreamDescriptor* streamDescriptor = remoteStream;
670 ASSERT(streamDescriptor->client());
672 MediaStream* stream = static_cast<MediaStream*>(streamDescriptor->client());
673 stream->streamEnded();
675 if (m_signalingState == SignalingStateClosed)
678 size_t pos = m_remoteStreams.find(stream);
679 ASSERT(pos != kNotFound);
680 m_remoteStreams.remove(pos);
682 scheduleDispatchEvent(MediaStreamEvent::create(EventTypeNames::removestream, false, false, stream));
685 void RTCPeerConnection::didAddRemoteDataChannel(WebRTCDataChannelHandler* handler)
688 ASSERT(executionContext()->isContextThread());
690 if (m_signalingState == SignalingStateClosed)
693 RTCDataChannel* channel = RTCDataChannel::create(executionContext(), this, adoptPtr(handler));
694 m_dataChannels.append(channel);
696 scheduleDispatchEvent(RTCDataChannelEvent::create(EventTypeNames::datachannel, false, false, channel));
699 void RTCPeerConnection::releasePeerConnectionHandler()
704 void RTCPeerConnection::closePeerConnection()
706 ASSERT(m_signalingState != RTCPeerConnection::SignalingStateClosed);
710 const AtomicString& RTCPeerConnection::interfaceName() const
712 return EventTargetNames::RTCPeerConnection;
715 ExecutionContext* RTCPeerConnection::executionContext() const
717 return ActiveDOMObject::executionContext();
720 void RTCPeerConnection::suspend()
722 m_dispatchScheduledEventRunner.suspend();
725 void RTCPeerConnection::resume()
727 m_dispatchScheduledEventRunner.resume();
730 void RTCPeerConnection::stop()
736 m_iceConnectionState = ICEConnectionStateClosed;
737 m_signalingState = SignalingStateClosed;
739 HeapVector<Member<RTCDataChannel> >::iterator i = m_dataChannels.begin();
740 for (; i != m_dataChannels.end(); ++i)
742 m_dataChannels.clear();
744 m_dispatchScheduledEventRunner.stop();
746 m_peerHandler.clear();
749 void RTCPeerConnection::changeSignalingState(SignalingState signalingState)
751 if (m_signalingState != SignalingStateClosed && m_signalingState != signalingState) {
752 m_signalingState = signalingState;
753 scheduleDispatchEvent(Event::create(EventTypeNames::signalingstatechange));
757 void RTCPeerConnection::changeIceGatheringState(ICEGatheringState iceGatheringState)
759 m_iceGatheringState = iceGatheringState;
762 void RTCPeerConnection::changeIceConnectionState(ICEConnectionState iceConnectionState)
764 if (m_iceConnectionState != ICEConnectionStateClosed && m_iceConnectionState != iceConnectionState) {
765 m_iceConnectionState = iceConnectionState;
766 scheduleDispatchEvent(Event::create(EventTypeNames::iceconnectionstatechange));
770 void RTCPeerConnection::closeInternal()
772 ASSERT(m_signalingState != RTCPeerConnection::SignalingStateClosed);
773 m_peerHandler->stop();
776 changeIceConnectionState(ICEConnectionStateClosed);
777 changeIceGatheringState(ICEGatheringStateComplete);
778 changeSignalingState(SignalingStateClosed);
781 void RTCPeerConnection::scheduleDispatchEvent(PassRefPtrWillBeRawPtr<Event> event)
783 m_scheduledEvents.append(event);
785 m_dispatchScheduledEventRunner.runAsync();
788 void RTCPeerConnection::dispatchScheduledEvent()
793 WillBeHeapVector<RefPtrWillBeMember<Event> > events;
794 events.swap(m_scheduledEvents);
796 WillBeHeapVector<RefPtrWillBeMember<Event> >::iterator it = events.begin();
797 for (; it != events.end(); ++it)
798 dispatchEvent((*it).release());
803 void RTCPeerConnection::trace(Visitor* visitor)
805 visitor->trace(m_localStreams);
806 visitor->trace(m_remoteStreams);
807 visitor->trace(m_dataChannels);
809 visitor->trace(m_scheduledEvents);
811 EventTargetWithInlineData::trace(visitor);