3 * Copyright 2012, Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
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.
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.
28 #ifndef TALK_APP_WEBRTC_PEERCONNECTION_H_
29 #define TALK_APP_WEBRTC_PEERCONNECTION_H_
33 #include "talk/app/webrtc/mediastreamsignaling.h"
34 #include "talk/app/webrtc/peerconnectioninterface.h"
35 #include "talk/app/webrtc/peerconnectionfactory.h"
36 #include "talk/app/webrtc/statscollector.h"
37 #include "talk/app/webrtc/streamcollection.h"
38 #include "talk/app/webrtc/webrtcsession.h"
39 #include "talk/base/scoped_ptr.h"
42 class MediaStreamHandlerContainer;
44 typedef std::vector<PortAllocatorFactoryInterface::StunConfiguration>
46 typedef std::vector<PortAllocatorFactoryInterface::TurnConfiguration>
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,
55 public talk_base::MessageHandler,
56 public sigslot::has_slots<> {
58 explicit PeerConnection(PeerConnectionFactory* factory);
60 bool Initialize(const PeerConnectionInterface::IceServers& configuration,
61 const MediaConstraintsInterface* constraints,
62 PortAllocatorFactoryInterface* allocator_factory,
63 DTLSIdentityServiceInterface* dtls_identity_service,
64 PeerConnectionObserver* observer);
65 virtual talk_base::scoped_refptr<StreamCollectionInterface> local_streams();
66 virtual talk_base::scoped_refptr<StreamCollectionInterface> remote_streams();
67 virtual bool AddStream(MediaStreamInterface* local_stream,
68 const MediaConstraintsInterface* constraints);
69 virtual void RemoveStream(MediaStreamInterface* local_stream);
71 virtual talk_base::scoped_refptr<DtmfSenderInterface> CreateDtmfSender(
72 AudioTrackInterface* track);
74 virtual talk_base::scoped_refptr<DataChannelInterface> CreateDataChannel(
75 const std::string& label,
76 const DataChannelInit* config);
77 virtual bool GetStats(StatsObserver* observer,
78 webrtc::MediaStreamTrackInterface* track);
79 virtual bool GetStats(StatsObserver* observer,
80 webrtc::MediaStreamTrackInterface* track,
81 StatsOutputLevel level);
83 virtual SignalingState signaling_state();
85 // TODO(bemasc): Remove ice_state() when callers are removed.
86 virtual IceState ice_state();
87 virtual IceConnectionState ice_connection_state();
88 virtual IceGatheringState ice_gathering_state();
90 virtual const SessionDescriptionInterface* local_description() const;
91 virtual const SessionDescriptionInterface* remote_description() const;
94 virtual void CreateOffer(CreateSessionDescriptionObserver* observer,
95 const MediaConstraintsInterface* constraints);
96 virtual void CreateAnswer(CreateSessionDescriptionObserver* observer,
97 const MediaConstraintsInterface* constraints);
98 virtual void SetLocalDescription(SetSessionDescriptionObserver* observer,
99 SessionDescriptionInterface* desc);
100 virtual void SetRemoteDescription(SetSessionDescriptionObserver* observer,
101 SessionDescriptionInterface* desc);
102 virtual bool UpdateIce(const IceServers& configuration,
103 const MediaConstraintsInterface* constraints);
104 virtual bool AddIceCandidate(const IceCandidateInterface* candidate);
106 virtual void Close();
109 virtual ~PeerConnection();
112 // Implements MessageHandler.
113 virtual void OnMessage(talk_base::Message* msg);
115 // Implements MediaStreamSignalingObserver.
116 virtual void OnAddRemoteStream(MediaStreamInterface* stream) OVERRIDE;
117 virtual void OnRemoveRemoteStream(MediaStreamInterface* stream) OVERRIDE;
118 virtual void OnAddDataChannel(DataChannelInterface* data_channel) OVERRIDE;
119 virtual void OnAddRemoteAudioTrack(MediaStreamInterface* stream,
120 AudioTrackInterface* audio_track,
121 uint32 ssrc) OVERRIDE;
122 virtual void OnAddRemoteVideoTrack(MediaStreamInterface* stream,
123 VideoTrackInterface* video_track,
124 uint32 ssrc) OVERRIDE;
125 virtual void OnRemoveRemoteAudioTrack(
126 MediaStreamInterface* stream,
127 AudioTrackInterface* audio_track) OVERRIDE;
128 virtual void OnRemoveRemoteVideoTrack(
129 MediaStreamInterface* stream,
130 VideoTrackInterface* video_track) OVERRIDE;
131 virtual void OnAddLocalAudioTrack(MediaStreamInterface* stream,
132 AudioTrackInterface* audio_track,
133 uint32 ssrc) OVERRIDE;
134 virtual void OnAddLocalVideoTrack(MediaStreamInterface* stream,
135 VideoTrackInterface* video_track,
136 uint32 ssrc) OVERRIDE;
137 virtual void OnRemoveLocalAudioTrack(
138 MediaStreamInterface* stream,
139 AudioTrackInterface* audio_track) OVERRIDE;
140 virtual void OnRemoveLocalVideoTrack(
141 MediaStreamInterface* stream,
142 VideoTrackInterface* video_track) OVERRIDE;
143 virtual void OnRemoveLocalStream(MediaStreamInterface* stream);
145 // Implements IceObserver
146 virtual void OnIceConnectionChange(IceConnectionState new_state);
147 virtual void OnIceGatheringChange(IceGatheringState new_state);
148 virtual void OnIceCandidate(const IceCandidateInterface* candidate);
149 virtual void OnIceComplete();
151 // Signals from WebRtcSession.
152 void OnSessionStateChange(cricket::BaseSession* session,
153 cricket::BaseSession::State state);
154 void ChangeSignalingState(SignalingState signaling_state);
156 bool DoInitialize(const StunConfigurations& stun_config,
157 const TurnConfigurations& turn_config,
158 const MediaConstraintsInterface* constraints,
159 PortAllocatorFactoryInterface* allocator_factory,
160 DTLSIdentityServiceInterface* dtls_identity_service,
161 PeerConnectionObserver* observer);
163 talk_base::Thread* signaling_thread() const {
164 return factory_->signaling_thread();
167 void PostSetSessionDescriptionFailure(SetSessionDescriptionObserver* observer,
168 const std::string& error);
170 bool IsClosed() const {
171 return signaling_state_ == PeerConnectionInterface::kClosed;
174 // Storing the factory as a scoped reference pointer ensures that the memory
175 // in the PeerConnectionFactoryImpl remains available as long as the
176 // PeerConnection is running. It is passed to PeerConnection as a raw pointer.
177 // However, since the reference counting is done in the
178 // PeerConnectionFactoryInteface all instances created using the raw pointer
179 // will refer to the same reference count.
180 talk_base::scoped_refptr<PeerConnectionFactory> factory_;
181 PeerConnectionObserver* observer_;
182 SignalingState signaling_state_;
183 // TODO(bemasc): Remove ice_state_.
185 IceConnectionState ice_connection_state_;
186 IceGatheringState ice_gathering_state_;
188 talk_base::scoped_ptr<cricket::PortAllocator> port_allocator_;
189 talk_base::scoped_ptr<WebRtcSession> session_;
190 talk_base::scoped_ptr<MediaStreamSignaling> mediastream_signaling_;
191 talk_base::scoped_ptr<MediaStreamHandlerContainer> stream_handler_container_;
192 StatsCollector stats_;
195 } // namespace webrtc
197 #endif // TALK_APP_WEBRTC_PEERCONNECTION_H_