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