blob: bb4e4ebc2a895910650c25203b5cd0c4fcc0e21c [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
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/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"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000039#include "webrtc/base/scoped_ptr.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000040
41namespace webrtc {
42class MediaStreamHandlerContainer;
43
44typedef std::vector<PortAllocatorFactoryInterface::StunConfiguration>
45 StunConfigurations;
46typedef 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.
52class PeerConnection : public PeerConnectionInterface,
53 public MediaStreamSignalingObserver,
54 public IceObserver,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000055 public rtc::MessageHandler,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000056 public sigslot::has_slots<> {
57 public:
58 explicit PeerConnection(PeerConnectionFactory* factory);
59
buildbot@webrtc.org41451d42014-05-03 05:39:45 +000060 bool Initialize(
61 const PeerConnectionInterface::RTCConfiguration& configuration,
62 const MediaConstraintsInterface* constraints,
63 PortAllocatorFactoryInterface* allocator_factory,
64 DTLSIdentityServiceInterface* dtls_identity_service,
65 PeerConnectionObserver* observer);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000066 virtual rtc::scoped_refptr<StreamCollectionInterface> local_streams();
67 virtual rtc::scoped_refptr<StreamCollectionInterface> remote_streams();
henrike@webrtc.org28e20752013-07-10 00:45:36 +000068 virtual bool AddStream(MediaStreamInterface* local_stream,
69 const MediaConstraintsInterface* constraints);
70 virtual void RemoveStream(MediaStreamInterface* local_stream);
71
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000072 virtual rtc::scoped_refptr<DtmfSenderInterface> CreateDtmfSender(
henrike@webrtc.org28e20752013-07-10 00:45:36 +000073 AudioTrackInterface* track);
74
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000075 virtual rtc::scoped_refptr<DataChannelInterface> CreateDataChannel(
henrike@webrtc.org28e20752013-07-10 00:45:36 +000076 const std::string& label,
77 const DataChannelInit* config);
78 virtual bool GetStats(StatsObserver* observer,
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000079 webrtc::MediaStreamTrackInterface* track,
80 StatsOutputLevel level);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000081
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 CreateAnswer(CreateSessionDescriptionObserver* observer,
96 const MediaConstraintsInterface* constraints);
97 virtual void SetLocalDescription(SetSessionDescriptionObserver* observer,
98 SessionDescriptionInterface* desc);
99 virtual void SetRemoteDescription(SetSessionDescriptionObserver* observer,
100 SessionDescriptionInterface* desc);
buildbot@webrtc.org41451d42014-05-03 05:39:45 +0000101 // TODO(mallinath) : Deprecated version, remove after all clients are updated.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000102 virtual bool UpdateIce(const IceServers& configuration,
103 const MediaConstraintsInterface* constraints);
buildbot@webrtc.org41451d42014-05-03 05:39:45 +0000104 virtual bool UpdateIce(
105 const PeerConnectionInterface::RTCConfiguration& config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000106 virtual bool AddIceCandidate(const IceCandidateInterface* candidate);
107
buildbot@webrtc.org1567b8c2014-05-08 19:54:16 +0000108 virtual void RegisterUMAObserver(UMAObserver* observer);
109
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000110 virtual void Close();
111
112 protected:
113 virtual ~PeerConnection();
114
115 private:
116 // Implements MessageHandler.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000117 virtual void OnMessage(rtc::Message* msg);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000118
119 // Implements MediaStreamSignalingObserver.
120 virtual void OnAddRemoteStream(MediaStreamInterface* stream) OVERRIDE;
121 virtual void OnRemoveRemoteStream(MediaStreamInterface* stream) OVERRIDE;
122 virtual void OnAddDataChannel(DataChannelInterface* data_channel) OVERRIDE;
123 virtual void OnAddRemoteAudioTrack(MediaStreamInterface* stream,
124 AudioTrackInterface* audio_track,
125 uint32 ssrc) OVERRIDE;
126 virtual void OnAddRemoteVideoTrack(MediaStreamInterface* stream,
127 VideoTrackInterface* video_track,
128 uint32 ssrc) OVERRIDE;
129 virtual void OnRemoveRemoteAudioTrack(
130 MediaStreamInterface* stream,
131 AudioTrackInterface* audio_track) OVERRIDE;
132 virtual void OnRemoveRemoteVideoTrack(
133 MediaStreamInterface* stream,
134 VideoTrackInterface* video_track) OVERRIDE;
135 virtual void OnAddLocalAudioTrack(MediaStreamInterface* stream,
136 AudioTrackInterface* audio_track,
137 uint32 ssrc) OVERRIDE;
138 virtual void OnAddLocalVideoTrack(MediaStreamInterface* stream,
139 VideoTrackInterface* video_track,
140 uint32 ssrc) OVERRIDE;
141 virtual void OnRemoveLocalAudioTrack(
142 MediaStreamInterface* stream,
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000143 AudioTrackInterface* audio_track,
144 uint32 ssrc) OVERRIDE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000145 virtual void OnRemoveLocalVideoTrack(
146 MediaStreamInterface* stream,
147 VideoTrackInterface* video_track) OVERRIDE;
148 virtual void OnRemoveLocalStream(MediaStreamInterface* stream);
149
150 // Implements IceObserver
151 virtual void OnIceConnectionChange(IceConnectionState new_state);
152 virtual void OnIceGatheringChange(IceGatheringState new_state);
153 virtual void OnIceCandidate(const IceCandidateInterface* candidate);
154 virtual void OnIceComplete();
155
156 // Signals from WebRtcSession.
157 void OnSessionStateChange(cricket::BaseSession* session,
158 cricket::BaseSession::State state);
159 void ChangeSignalingState(SignalingState signaling_state);
160
buildbot@webrtc.org41451d42014-05-03 05:39:45 +0000161 bool DoInitialize(IceTransportsType type,
162 const StunConfigurations& stun_config,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000163 const TurnConfigurations& turn_config,
164 const MediaConstraintsInterface* constraints,
wu@webrtc.org91053e72013-08-10 07:18:04 +0000165 PortAllocatorFactoryInterface* allocator_factory,
166 DTLSIdentityServiceInterface* dtls_identity_service,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000167 PeerConnectionObserver* observer);
168
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000169 rtc::Thread* signaling_thread() const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000170 return factory_->signaling_thread();
171 }
172
173 void PostSetSessionDescriptionFailure(SetSessionDescriptionObserver* observer,
174 const std::string& error);
175
176 bool IsClosed() const {
177 return signaling_state_ == PeerConnectionInterface::kClosed;
178 }
179
180 // Storing the factory as a scoped reference pointer ensures that the memory
181 // in the PeerConnectionFactoryImpl remains available as long as the
182 // PeerConnection is running. It is passed to PeerConnection as a raw pointer.
183 // However, since the reference counting is done in the
184 // PeerConnectionFactoryInteface all instances created using the raw pointer
185 // will refer to the same reference count.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000186 rtc::scoped_refptr<PeerConnectionFactory> factory_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000187 PeerConnectionObserver* observer_;
buildbot@webrtc.org1567b8c2014-05-08 19:54:16 +0000188 UMAObserver* uma_observer_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000189 SignalingState signaling_state_;
190 // TODO(bemasc): Remove ice_state_.
191 IceState ice_state_;
192 IceConnectionState ice_connection_state_;
193 IceGatheringState ice_gathering_state_;
194
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000195 rtc::scoped_ptr<cricket::PortAllocator> port_allocator_;
196 rtc::scoped_ptr<WebRtcSession> session_;
197 rtc::scoped_ptr<MediaStreamSignaling> mediastream_signaling_;
198 rtc::scoped_ptr<MediaStreamHandlerContainer> stream_handler_container_;
199 rtc::scoped_ptr<StatsCollector> stats_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000200};
201
202} // namespace webrtc
203
204#endif // TALK_APP_WEBRTC_PEERCONNECTION_H_