blob: 8caa821081a3f9d3f804f1f3d21085bab0e7ec2d [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"
deadbeef70ab1a12015-09-28 16:53:55 -070037#include "talk/app/webrtc/rtpreceiverinterface.h"
38#include "talk/app/webrtc/rtpsenderinterface.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000039#include "talk/app/webrtc/statscollector.h"
40#include "talk/app/webrtc/streamcollection.h"
41#include "talk/app/webrtc/webrtcsession.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000042#include "webrtc/base/scoped_ptr.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000043
44namespace webrtc {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000045
46typedef std::vector<PortAllocatorFactoryInterface::StunConfiguration>
47 StunConfigurations;
48typedef std::vector<PortAllocatorFactoryInterface::TurnConfiguration>
49 TurnConfigurations;
50
deadbeef70ab1a12015-09-28 16:53:55 -070051// PeerConnection implements the PeerConnectionInterface interface.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000052// It uses MediaStreamSignaling and WebRtcSession to implement
53// the PeerConnection functionality.
54class PeerConnection : public PeerConnectionInterface,
55 public MediaStreamSignalingObserver,
56 public IceObserver,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000057 public rtc::MessageHandler,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000058 public sigslot::has_slots<> {
59 public:
60 explicit PeerConnection(PeerConnectionFactory* factory);
61
buildbot@webrtc.org41451d42014-05-03 05:39:45 +000062 bool Initialize(
63 const PeerConnectionInterface::RTCConfiguration& configuration,
64 const MediaConstraintsInterface* constraints,
65 PortAllocatorFactoryInterface* allocator_factory,
Henrik Boström5e56c592015-08-11 10:33:13 +020066 rtc::scoped_ptr<DtlsIdentityStoreInterface> dtls_identity_store,
buildbot@webrtc.org41451d42014-05-03 05:39:45 +000067 PeerConnectionObserver* observer);
deadbeef7603c762015-09-23 17:37:11 -070068 virtual rtc::scoped_refptr<StreamCollectionInterface> local_streams();
69 virtual rtc::scoped_refptr<StreamCollectionInterface> remote_streams();
70 virtual bool AddStream(MediaStreamInterface* local_stream);
71 virtual void RemoveStream(MediaStreamInterface* local_stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000072
deadbeef7603c762015-09-23 17:37:11 -070073 virtual rtc::scoped_refptr<DtmfSenderInterface> CreateDtmfSender(
74 AudioTrackInterface* track);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000075
deadbeef70ab1a12015-09-28 16:53:55 -070076 std::vector<rtc::scoped_refptr<RtpSenderInterface>> GetSenders()
77 const override;
78 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> GetReceivers()
79 const override;
80
deadbeef7603c762015-09-23 17:37:11 -070081 virtual rtc::scoped_refptr<DataChannelInterface> CreateDataChannel(
henrike@webrtc.org28e20752013-07-10 00:45:36 +000082 const std::string& label,
deadbeef7603c762015-09-23 17:37:11 -070083 const DataChannelInit* config);
84 virtual bool GetStats(StatsObserver* observer,
85 webrtc::MediaStreamTrackInterface* track,
86 StatsOutputLevel level);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000087
deadbeef7603c762015-09-23 17:37:11 -070088 virtual SignalingState signaling_state();
henrike@webrtc.org28e20752013-07-10 00:45:36 +000089
90 // TODO(bemasc): Remove ice_state() when callers are removed.
deadbeef7603c762015-09-23 17:37:11 -070091 virtual IceState ice_state();
92 virtual IceConnectionState ice_connection_state();
93 virtual IceGatheringState ice_gathering_state();
henrike@webrtc.org28e20752013-07-10 00:45:36 +000094
deadbeef7603c762015-09-23 17:37:11 -070095 virtual const SessionDescriptionInterface* local_description() const;
96 virtual const SessionDescriptionInterface* remote_description() const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000097
98 // JSEP01
deadbeef7603c762015-09-23 17:37:11 -070099 virtual void CreateOffer(CreateSessionDescriptionObserver* observer,
100 const MediaConstraintsInterface* constraints);
101 virtual void CreateOffer(CreateSessionDescriptionObserver* observer,
102 const RTCOfferAnswerOptions& options);
103 virtual void CreateAnswer(CreateSessionDescriptionObserver* observer,
104 const MediaConstraintsInterface* constraints);
105 virtual void SetLocalDescription(SetSessionDescriptionObserver* observer,
106 SessionDescriptionInterface* desc);
107 virtual void SetRemoteDescription(SetSessionDescriptionObserver* observer,
108 SessionDescriptionInterface* desc);
109 // TODO(mallinath) : Deprecated version, remove after all clients are updated.
110 virtual bool UpdateIce(const IceServers& configuration,
111 const MediaConstraintsInterface* constraints);
112 virtual bool UpdateIce(
113 const PeerConnectionInterface::RTCConfiguration& config);
114 virtual bool AddIceCandidate(const IceCandidateInterface* candidate);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000115
deadbeef7603c762015-09-23 17:37:11 -0700116 virtual void RegisterUMAObserver(UMAObserver* observer);
buildbot@webrtc.org1567b8c2014-05-08 19:54:16 +0000117
deadbeef7603c762015-09-23 17:37:11 -0700118 virtual void Close();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000119
120 protected:
deadbeef7603c762015-09-23 17:37:11 -0700121 virtual ~PeerConnection();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000122
123 private:
124 // Implements MessageHandler.
deadbeef7603c762015-09-23 17:37:11 -0700125 virtual void OnMessage(rtc::Message* msg);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000126
127 // Implements MediaStreamSignalingObserver.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000128 void OnAddRemoteStream(MediaStreamInterface* stream) override;
129 void OnRemoveRemoteStream(MediaStreamInterface* stream) override;
130 void OnAddDataChannel(DataChannelInterface* data_channel) override;
131 void OnAddRemoteAudioTrack(MediaStreamInterface* stream,
132 AudioTrackInterface* audio_track,
133 uint32 ssrc) override;
134 void OnAddRemoteVideoTrack(MediaStreamInterface* stream,
135 VideoTrackInterface* video_track,
136 uint32 ssrc) override;
137 void OnRemoveRemoteAudioTrack(MediaStreamInterface* stream,
138 AudioTrackInterface* audio_track) override;
139 void OnRemoveRemoteVideoTrack(MediaStreamInterface* stream,
140 VideoTrackInterface* video_track) override;
141 void OnAddLocalAudioTrack(MediaStreamInterface* stream,
142 AudioTrackInterface* audio_track,
143 uint32 ssrc) override;
144 void OnAddLocalVideoTrack(MediaStreamInterface* stream,
145 VideoTrackInterface* video_track,
146 uint32 ssrc) override;
147 void OnRemoveLocalAudioTrack(MediaStreamInterface* stream,
148 AudioTrackInterface* audio_track,
149 uint32 ssrc) override;
150 void OnRemoveLocalVideoTrack(MediaStreamInterface* stream,
151 VideoTrackInterface* video_track) override;
Peter Thatcher54360512015-07-08 11:08:35 -0700152 void OnRemoveLocalStream(MediaStreamInterface* stream) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000153
154 // Implements IceObserver
Peter Thatcher54360512015-07-08 11:08:35 -0700155 void OnIceConnectionChange(IceConnectionState new_state) override;
156 void OnIceGatheringChange(IceGatheringState new_state) override;
157 void OnIceCandidate(const IceCandidateInterface* candidate) override;
158 void OnIceComplete() override;
159 void OnIceConnectionReceivingChange(bool receiving) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000160
161 // Signals from WebRtcSession.
162 void OnSessionStateChange(cricket::BaseSession* session,
163 cricket::BaseSession::State state);
164 void ChangeSignalingState(SignalingState signaling_state);
165
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000166 rtc::Thread* signaling_thread() const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000167 return factory_->signaling_thread();
168 }
169
170 void PostSetSessionDescriptionFailure(SetSessionDescriptionObserver* observer,
171 const std::string& error);
172
173 bool IsClosed() const {
174 return signaling_state_ == PeerConnectionInterface::kClosed;
175 }
176
deadbeef70ab1a12015-09-28 16:53:55 -0700177 std::vector<rtc::scoped_refptr<RtpSenderInterface>>::iterator
178 FindSenderForTrack(MediaStreamTrackInterface* track);
179 std::vector<rtc::scoped_refptr<RtpReceiverInterface>>::iterator
180 FindReceiverForTrack(MediaStreamTrackInterface* track);
181
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000182 // 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.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000188 rtc::scoped_refptr<PeerConnectionFactory> factory_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000189 PeerConnectionObserver* observer_;
buildbot@webrtc.org1567b8c2014-05-08 19:54:16 +0000190 UMAObserver* uma_observer_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000191 SignalingState signaling_state_;
192 // TODO(bemasc): Remove ice_state_.
193 IceState ice_state_;
194 IceConnectionState ice_connection_state_;
195 IceGatheringState ice_gathering_state_;
196
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000197 rtc::scoped_ptr<cricket::PortAllocator> port_allocator_;
198 rtc::scoped_ptr<WebRtcSession> session_;
199 rtc::scoped_ptr<MediaStreamSignaling> mediastream_signaling_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000200 rtc::scoped_ptr<StatsCollector> stats_;
deadbeef70ab1a12015-09-28 16:53:55 -0700201
202 std::vector<rtc::scoped_refptr<RtpSenderInterface>> senders_;
203 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> receivers_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000204};
205
206} // namespace webrtc
207
208#endif // TALK_APP_WEBRTC_PEERCONNECTION_H_