Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / libjingle / source / talk / app / webrtc / peerconnection.h
1 /*
2  * libjingle
3  * Copyright 2012, Google Inc.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  *  1. Redistributions of source code must retain the above copyright notice,
9  *     this list of conditions and the following disclaimer.
10  *  2. Redistributions in binary form must reproduce the above copyright notice,
11  *     this list of conditions and the following disclaimer in the documentation
12  *     and/or other materials provided with the distribution.
13  *  3. The name of the author may not be used to endorse or promote products
14  *     derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19  * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27
28 #ifndef TALK_APP_WEBRTC_PEERCONNECTION_H_
29 #define TALK_APP_WEBRTC_PEERCONNECTION_H_
30
31 #include <string>
32
33 #include "talk/app/webrtc/mediastreamsignaling.h"
34 #include "talk/app/webrtc/peerconnectionfactory.h"
35 #include "talk/app/webrtc/peerconnectioninterface.h"
36 #include "talk/app/webrtc/statscollector.h"
37 #include "talk/app/webrtc/streamcollection.h"
38 #include "talk/app/webrtc/webrtcsession.h"
39 #include "webrtc/base/scoped_ptr.h"
40
41 namespace webrtc {
42 class MediaStreamHandlerContainer;
43
44 typedef std::vector<PortAllocatorFactoryInterface::StunConfiguration>
45     StunConfigurations;
46 typedef std::vector<PortAllocatorFactoryInterface::TurnConfiguration>
47     TurnConfigurations;
48
49 // PeerConnectionImpl implements the PeerConnection interface.
50 // It uses MediaStreamSignaling and WebRtcSession to implement
51 // the PeerConnection functionality.
52 class PeerConnection : public PeerConnectionInterface,
53                        public MediaStreamSignalingObserver,
54                        public IceObserver,
55                        public rtc::MessageHandler,
56                        public sigslot::has_slots<> {
57  public:
58   explicit PeerConnection(PeerConnectionFactory* factory);
59
60   bool Initialize(
61       const PeerConnectionInterface::RTCConfiguration& configuration,
62       const MediaConstraintsInterface* constraints,
63       PortAllocatorFactoryInterface* allocator_factory,
64       DTLSIdentityServiceInterface* dtls_identity_service,
65       PeerConnectionObserver* observer);
66   virtual rtc::scoped_refptr<StreamCollectionInterface> local_streams();
67   virtual rtc::scoped_refptr<StreamCollectionInterface> remote_streams();
68   virtual bool AddStream(MediaStreamInterface* local_stream,
69                          const MediaConstraintsInterface* constraints);
70   virtual void RemoveStream(MediaStreamInterface* local_stream);
71
72   virtual rtc::scoped_refptr<DtmfSenderInterface> CreateDtmfSender(
73       AudioTrackInterface* track);
74
75   virtual rtc::scoped_refptr<DataChannelInterface> CreateDataChannel(
76       const std::string& label,
77       const DataChannelInit* config);
78   virtual bool GetStats(StatsObserver* observer,
79                         webrtc::MediaStreamTrackInterface* track,
80                         StatsOutputLevel level);
81
82   virtual SignalingState signaling_state();
83
84   // TODO(bemasc): Remove ice_state() when callers are removed.
85   virtual IceState ice_state();
86   virtual IceConnectionState ice_connection_state();
87   virtual IceGatheringState ice_gathering_state();
88
89   virtual const SessionDescriptionInterface* local_description() const;
90   virtual const SessionDescriptionInterface* remote_description() const;
91
92   // JSEP01
93   virtual void CreateOffer(CreateSessionDescriptionObserver* observer,
94                            const MediaConstraintsInterface* constraints);
95   virtual void CreateOffer(CreateSessionDescriptionObserver* observer,
96                            const RTCOfferAnswerOptions& options);
97   virtual void CreateAnswer(CreateSessionDescriptionObserver* observer,
98                             const MediaConstraintsInterface* constraints);
99   virtual void SetLocalDescription(SetSessionDescriptionObserver* observer,
100                                    SessionDescriptionInterface* desc);
101   virtual void SetRemoteDescription(SetSessionDescriptionObserver* observer,
102                                     SessionDescriptionInterface* desc);
103   // TODO(mallinath) : Deprecated version, remove after all clients are updated.
104   virtual bool UpdateIce(const IceServers& configuration,
105                          const MediaConstraintsInterface* constraints);
106   virtual bool UpdateIce(
107       const PeerConnectionInterface::RTCConfiguration& config);
108   virtual bool AddIceCandidate(const IceCandidateInterface* candidate);
109
110   virtual void RegisterUMAObserver(UMAObserver* observer);
111
112   virtual void Close();
113
114  protected:
115   virtual ~PeerConnection();
116
117  private:
118   // Implements MessageHandler.
119   virtual void OnMessage(rtc::Message* msg);
120
121   // Implements MediaStreamSignalingObserver.
122   virtual void OnAddRemoteStream(MediaStreamInterface* stream) OVERRIDE;
123   virtual void OnRemoveRemoteStream(MediaStreamInterface* stream) OVERRIDE;
124   virtual void OnAddDataChannel(DataChannelInterface* data_channel) OVERRIDE;
125   virtual void OnAddRemoteAudioTrack(MediaStreamInterface* stream,
126                                      AudioTrackInterface* audio_track,
127                                      uint32 ssrc) OVERRIDE;
128   virtual void OnAddRemoteVideoTrack(MediaStreamInterface* stream,
129                                      VideoTrackInterface* video_track,
130                                      uint32 ssrc) OVERRIDE;
131   virtual void OnRemoveRemoteAudioTrack(
132       MediaStreamInterface* stream,
133       AudioTrackInterface* audio_track) OVERRIDE;
134   virtual void OnRemoveRemoteVideoTrack(
135       MediaStreamInterface* stream,
136       VideoTrackInterface* video_track) OVERRIDE;
137   virtual void OnAddLocalAudioTrack(MediaStreamInterface* stream,
138                                     AudioTrackInterface* audio_track,
139                                     uint32 ssrc) OVERRIDE;
140   virtual void OnAddLocalVideoTrack(MediaStreamInterface* stream,
141                                     VideoTrackInterface* video_track,
142                                     uint32 ssrc) OVERRIDE;
143   virtual void OnRemoveLocalAudioTrack(
144       MediaStreamInterface* stream,
145       AudioTrackInterface* audio_track,
146       uint32 ssrc) OVERRIDE;
147   virtual void OnRemoveLocalVideoTrack(
148       MediaStreamInterface* stream,
149       VideoTrackInterface* video_track) OVERRIDE;
150   virtual void OnRemoveLocalStream(MediaStreamInterface* stream);
151
152   // Implements IceObserver
153   virtual void OnIceConnectionChange(IceConnectionState new_state);
154   virtual void OnIceGatheringChange(IceGatheringState new_state);
155   virtual void OnIceCandidate(const IceCandidateInterface* candidate);
156   virtual void OnIceComplete();
157
158   // Signals from WebRtcSession.
159   void OnSessionStateChange(cricket::BaseSession* session,
160                             cricket::BaseSession::State state);
161   void ChangeSignalingState(SignalingState signaling_state);
162
163   bool DoInitialize(IceTransportsType type,
164                     const StunConfigurations& stun_config,
165                     const TurnConfigurations& turn_config,
166                     const MediaConstraintsInterface* constraints,
167                     PortAllocatorFactoryInterface* allocator_factory,
168                     DTLSIdentityServiceInterface* dtls_identity_service,
169                     PeerConnectionObserver* observer);
170
171   rtc::Thread* signaling_thread() const {
172     return factory_->signaling_thread();
173   }
174
175   void PostSetSessionDescriptionFailure(SetSessionDescriptionObserver* observer,
176                                         const std::string& error);
177
178   bool IsClosed() const {
179     return signaling_state_ == PeerConnectionInterface::kClosed;
180   }
181
182   // Storing the factory as a scoped reference pointer ensures that the memory
183   // in the PeerConnectionFactoryImpl remains available as long as the
184   // PeerConnection is running. It is passed to PeerConnection as a raw pointer.
185   // However, since the reference counting is done in the
186   // PeerConnectionFactoryInteface all instances created using the raw pointer
187   // will refer to the same reference count.
188   rtc::scoped_refptr<PeerConnectionFactory> factory_;
189   PeerConnectionObserver* observer_;
190   UMAObserver* uma_observer_;
191   SignalingState signaling_state_;
192   // TODO(bemasc): Remove ice_state_.
193   IceState ice_state_;
194   IceConnectionState ice_connection_state_;
195   IceGatheringState ice_gathering_state_;
196
197   rtc::scoped_ptr<cricket::PortAllocator> port_allocator_;
198   rtc::scoped_ptr<WebRtcSession> session_;
199   rtc::scoped_ptr<MediaStreamSignaling> mediastream_signaling_;
200   rtc::scoped_ptr<MediaStreamHandlerContainer> stream_handler_container_;
201   rtc::scoped_ptr<StatsCollector> stats_;
202 };
203
204 }  // namespace webrtc
205
206 #endif  // TALK_APP_WEBRTC_PEERCONNECTION_H_