blob: df4cb78fc1de5c0281d66e02f70ad28bff8b505d [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +00003 * Copyright 2012 Google Inc.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004 *
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
Henrik Boström5e56c592015-08-11 10:33:13 +020033#include "talk/app/webrtc/dtlsidentitystore.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000034#include "talk/app/webrtc/mediastreamsignaling.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000035#include "talk/app/webrtc/peerconnectionfactory.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000036#include "talk/app/webrtc/peerconnectioninterface.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000037#include "talk/app/webrtc/statscollector.h"
38#include "talk/app/webrtc/streamcollection.h"
39#include "talk/app/webrtc/webrtcsession.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000040#include "webrtc/base/scoped_ptr.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000041
42namespace webrtc {
43class MediaStreamHandlerContainer;
44
45typedef std::vector<PortAllocatorFactoryInterface::StunConfiguration>
46 StunConfigurations;
47typedef std::vector<PortAllocatorFactoryInterface::TurnConfiguration>
48 TurnConfigurations;
49
50// PeerConnectionImpl implements the PeerConnection interface.
51// It uses MediaStreamSignaling and WebRtcSession to implement
52// the PeerConnection functionality.
53class PeerConnection : public PeerConnectionInterface,
54 public MediaStreamSignalingObserver,
55 public IceObserver,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000056 public rtc::MessageHandler,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000057 public sigslot::has_slots<> {
58 public:
59 explicit PeerConnection(PeerConnectionFactory* factory);
60
buildbot@webrtc.org41451d42014-05-03 05:39:45 +000061 bool Initialize(
62 const PeerConnectionInterface::RTCConfiguration& configuration,
63 const MediaConstraintsInterface* constraints,
64 PortAllocatorFactoryInterface* allocator_factory,
Henrik Boström5e56c592015-08-11 10:33:13 +020065 rtc::scoped_ptr<DtlsIdentityStoreInterface> dtls_identity_store,
buildbot@webrtc.org41451d42014-05-03 05:39:45 +000066 PeerConnectionObserver* observer);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000067 virtual rtc::scoped_refptr<StreamCollectionInterface> local_streams();
68 virtual rtc::scoped_refptr<StreamCollectionInterface> remote_streams();
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +000069 virtual bool AddStream(MediaStreamInterface* local_stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000070 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);
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +000095 virtual void CreateOffer(CreateSessionDescriptionObserver* observer,
96 const RTCOfferAnswerOptions& options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000097 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);
honghaiz90099622015-07-13 12:19:33 -0700103 virtual void SetIceConnectionReceivingTimeout(int timeout_ms);
buildbot@webrtc.org41451d42014-05-03 05:39:45 +0000104 // TODO(mallinath) : Deprecated version, remove after all clients are updated.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000105 virtual bool UpdateIce(const IceServers& configuration,
106 const MediaConstraintsInterface* constraints);
buildbot@webrtc.org41451d42014-05-03 05:39:45 +0000107 virtual bool UpdateIce(
108 const PeerConnectionInterface::RTCConfiguration& config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000109 virtual bool AddIceCandidate(const IceCandidateInterface* candidate);
110
buildbot@webrtc.org1567b8c2014-05-08 19:54:16 +0000111 virtual void RegisterUMAObserver(UMAObserver* observer);
112
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000113 virtual void Close();
114
115 protected:
116 virtual ~PeerConnection();
117
118 private:
119 // Implements MessageHandler.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000120 virtual void OnMessage(rtc::Message* msg);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000121
122 // Implements MediaStreamSignalingObserver.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000123 void OnAddRemoteStream(MediaStreamInterface* stream) override;
124 void OnRemoveRemoteStream(MediaStreamInterface* stream) override;
125 void OnAddDataChannel(DataChannelInterface* data_channel) override;
126 void OnAddRemoteAudioTrack(MediaStreamInterface* stream,
127 AudioTrackInterface* audio_track,
128 uint32 ssrc) override;
129 void OnAddRemoteVideoTrack(MediaStreamInterface* stream,
130 VideoTrackInterface* video_track,
131 uint32 ssrc) override;
132 void OnRemoveRemoteAudioTrack(MediaStreamInterface* stream,
133 AudioTrackInterface* audio_track) override;
134 void OnRemoveRemoteVideoTrack(MediaStreamInterface* stream,
135 VideoTrackInterface* video_track) override;
136 void OnAddLocalAudioTrack(MediaStreamInterface* stream,
137 AudioTrackInterface* audio_track,
138 uint32 ssrc) override;
139 void OnAddLocalVideoTrack(MediaStreamInterface* stream,
140 VideoTrackInterface* video_track,
141 uint32 ssrc) override;
142 void OnRemoveLocalAudioTrack(MediaStreamInterface* stream,
143 AudioTrackInterface* audio_track,
144 uint32 ssrc) override;
145 void OnRemoveLocalVideoTrack(MediaStreamInterface* stream,
146 VideoTrackInterface* video_track) override;
Peter Thatcher54360512015-07-08 11:08:35 -0700147 void OnRemoveLocalStream(MediaStreamInterface* stream) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000148
149 // Implements IceObserver
Peter Thatcher54360512015-07-08 11:08:35 -0700150 void OnIceConnectionChange(IceConnectionState new_state) override;
151 void OnIceGatheringChange(IceGatheringState new_state) override;
152 void OnIceCandidate(const IceCandidateInterface* candidate) override;
153 void OnIceComplete() override;
154 void OnIceConnectionReceivingChange(bool receiving) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000155
156 // Signals from WebRtcSession.
157 void OnSessionStateChange(cricket::BaseSession* session,
158 cricket::BaseSession::State state);
159 void ChangeSignalingState(SignalingState signaling_state);
160
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000161 rtc::Thread* signaling_thread() const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000162 return factory_->signaling_thread();
163 }
164
165 void PostSetSessionDescriptionFailure(SetSessionDescriptionObserver* observer,
166 const std::string& error);
167
168 bool IsClosed() const {
169 return signaling_state_ == PeerConnectionInterface::kClosed;
170 }
171
172 // Storing the factory as a scoped reference pointer ensures that the memory
173 // in the PeerConnectionFactoryImpl remains available as long as the
174 // PeerConnection is running. It is passed to PeerConnection as a raw pointer.
175 // However, since the reference counting is done in the
176 // PeerConnectionFactoryInteface all instances created using the raw pointer
177 // will refer to the same reference count.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000178 rtc::scoped_refptr<PeerConnectionFactory> factory_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000179 PeerConnectionObserver* observer_;
buildbot@webrtc.org1567b8c2014-05-08 19:54:16 +0000180 UMAObserver* uma_observer_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000181 SignalingState signaling_state_;
182 // TODO(bemasc): Remove ice_state_.
183 IceState ice_state_;
184 IceConnectionState ice_connection_state_;
185 IceGatheringState ice_gathering_state_;
186
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000187 rtc::scoped_ptr<cricket::PortAllocator> port_allocator_;
188 rtc::scoped_ptr<WebRtcSession> session_;
189 rtc::scoped_ptr<MediaStreamSignaling> mediastream_signaling_;
190 rtc::scoped_ptr<MediaStreamHandlerContainer> stream_handler_container_;
191 rtc::scoped_ptr<StatsCollector> stats_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000192};
193
194} // namespace webrtc
195
196#endif // TALK_APP_WEBRTC_PEERCONNECTION_H_