blob: 8a8701931acf69a53ed4da98f59270058d534048 [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
deadbeef0a6c4ca2015-10-06 11:38:28 -070051// Parses the URLs for each server in |servers| to build |stun_config| and
52// |turn_config|.
53bool ParseIceServers(const PeerConnectionInterface::IceServers& servers,
54 StunConfigurations* stun_config,
55 TurnConfigurations* turn_config);
56
deadbeef70ab1a12015-09-28 16:53:55 -070057// PeerConnection implements the PeerConnectionInterface interface.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000058// It uses MediaStreamSignaling and WebRtcSession to implement
59// the PeerConnection functionality.
60class PeerConnection : public PeerConnectionInterface,
61 public MediaStreamSignalingObserver,
62 public IceObserver,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000063 public rtc::MessageHandler,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000064 public sigslot::has_slots<> {
65 public:
66 explicit PeerConnection(PeerConnectionFactory* factory);
67
buildbot@webrtc.org41451d42014-05-03 05:39:45 +000068 bool Initialize(
69 const PeerConnectionInterface::RTCConfiguration& configuration,
70 const MediaConstraintsInterface* constraints,
71 PortAllocatorFactoryInterface* allocator_factory,
Henrik Boström5e56c592015-08-11 10:33:13 +020072 rtc::scoped_ptr<DtlsIdentityStoreInterface> dtls_identity_store,
buildbot@webrtc.org41451d42014-05-03 05:39:45 +000073 PeerConnectionObserver* observer);
deadbeefa67696b2015-09-29 11:56:26 -070074 rtc::scoped_refptr<StreamCollectionInterface> local_streams() override;
75 rtc::scoped_refptr<StreamCollectionInterface> remote_streams() override;
76 bool AddStream(MediaStreamInterface* local_stream) override;
77 void RemoveStream(MediaStreamInterface* local_stream) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000078
deadbeefa67696b2015-09-29 11:56:26 -070079 rtc::scoped_refptr<DtmfSenderInterface> CreateDtmfSender(
80 AudioTrackInterface* track) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000081
deadbeef70ab1a12015-09-28 16:53:55 -070082 std::vector<rtc::scoped_refptr<RtpSenderInterface>> GetSenders()
83 const override;
84 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> GetReceivers()
85 const override;
86
deadbeefa67696b2015-09-29 11:56:26 -070087 rtc::scoped_refptr<DataChannelInterface> CreateDataChannel(
henrike@webrtc.org28e20752013-07-10 00:45:36 +000088 const std::string& label,
deadbeefa67696b2015-09-29 11:56:26 -070089 const DataChannelInit* config) override;
90 bool GetStats(StatsObserver* observer,
91 webrtc::MediaStreamTrackInterface* track,
92 StatsOutputLevel level) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000093
deadbeefa67696b2015-09-29 11:56:26 -070094 SignalingState signaling_state() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000095
96 // TODO(bemasc): Remove ice_state() when callers are removed.
deadbeefa67696b2015-09-29 11:56:26 -070097 IceState ice_state() override;
98 IceConnectionState ice_connection_state() override;
99 IceGatheringState ice_gathering_state() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000100
deadbeefa67696b2015-09-29 11:56:26 -0700101 const SessionDescriptionInterface* local_description() const override;
102 const SessionDescriptionInterface* remote_description() const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000103
104 // JSEP01
deadbeefa67696b2015-09-29 11:56:26 -0700105 void CreateOffer(CreateSessionDescriptionObserver* observer,
106 const MediaConstraintsInterface* constraints) override;
107 void CreateOffer(CreateSessionDescriptionObserver* observer,
108 const RTCOfferAnswerOptions& options) override;
109 void CreateAnswer(CreateSessionDescriptionObserver* observer,
110 const MediaConstraintsInterface* constraints) override;
111 void SetLocalDescription(SetSessionDescriptionObserver* observer,
112 SessionDescriptionInterface* desc) override;
113 void SetRemoteDescription(SetSessionDescriptionObserver* observer,
114 SessionDescriptionInterface* desc) override;
115 bool SetConfiguration(
116 const PeerConnectionInterface::RTCConfiguration& config) override;
117 bool AddIceCandidate(const IceCandidateInterface* candidate) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000118
deadbeefa67696b2015-09-29 11:56:26 -0700119 void RegisterUMAObserver(UMAObserver* observer) override;
buildbot@webrtc.org1567b8c2014-05-08 19:54:16 +0000120
deadbeefa67696b2015-09-29 11:56:26 -0700121 void Close() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000122
123 protected:
deadbeefa67696b2015-09-29 11:56:26 -0700124 ~PeerConnection() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000125
126 private:
127 // Implements MessageHandler.
deadbeefa67696b2015-09-29 11:56:26 -0700128 void OnMessage(rtc::Message* msg) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000129
130 // Implements MediaStreamSignalingObserver.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000131 void OnAddRemoteStream(MediaStreamInterface* stream) override;
132 void OnRemoveRemoteStream(MediaStreamInterface* stream) override;
133 void OnAddDataChannel(DataChannelInterface* data_channel) override;
134 void OnAddRemoteAudioTrack(MediaStreamInterface* stream,
135 AudioTrackInterface* audio_track,
136 uint32 ssrc) override;
137 void OnAddRemoteVideoTrack(MediaStreamInterface* stream,
138 VideoTrackInterface* video_track,
139 uint32 ssrc) override;
140 void OnRemoveRemoteAudioTrack(MediaStreamInterface* stream,
141 AudioTrackInterface* audio_track) override;
142 void OnRemoveRemoteVideoTrack(MediaStreamInterface* stream,
143 VideoTrackInterface* video_track) override;
144 void OnAddLocalAudioTrack(MediaStreamInterface* stream,
145 AudioTrackInterface* audio_track,
146 uint32 ssrc) override;
147 void OnAddLocalVideoTrack(MediaStreamInterface* stream,
148 VideoTrackInterface* video_track,
149 uint32 ssrc) override;
150 void OnRemoveLocalAudioTrack(MediaStreamInterface* stream,
151 AudioTrackInterface* audio_track,
152 uint32 ssrc) override;
153 void OnRemoveLocalVideoTrack(MediaStreamInterface* stream,
154 VideoTrackInterface* video_track) override;
Peter Thatcher54360512015-07-08 11:08:35 -0700155 void OnRemoveLocalStream(MediaStreamInterface* stream) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000156
157 // Implements IceObserver
Peter Thatcher54360512015-07-08 11:08:35 -0700158 void OnIceConnectionChange(IceConnectionState new_state) override;
159 void OnIceGatheringChange(IceGatheringState new_state) override;
160 void OnIceCandidate(const IceCandidateInterface* candidate) override;
161 void OnIceComplete() override;
162 void OnIceConnectionReceivingChange(bool receiving) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000163
164 // Signals from WebRtcSession.
165 void OnSessionStateChange(cricket::BaseSession* session,
166 cricket::BaseSession::State state);
167 void ChangeSignalingState(SignalingState signaling_state);
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
deadbeef70ab1a12015-09-28 16:53:55 -0700180 std::vector<rtc::scoped_refptr<RtpSenderInterface>>::iterator
181 FindSenderForTrack(MediaStreamTrackInterface* track);
182 std::vector<rtc::scoped_refptr<RtpReceiverInterface>>::iterator
183 FindReceiverForTrack(MediaStreamTrackInterface* track);
184
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000185 // Storing the factory as a scoped reference pointer ensures that the memory
186 // in the PeerConnectionFactoryImpl remains available as long as the
187 // PeerConnection is running. It is passed to PeerConnection as a raw pointer.
188 // However, since the reference counting is done in the
189 // PeerConnectionFactoryInteface all instances created using the raw pointer
190 // will refer to the same reference count.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000191 rtc::scoped_refptr<PeerConnectionFactory> factory_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000192 PeerConnectionObserver* observer_;
buildbot@webrtc.org1567b8c2014-05-08 19:54:16 +0000193 UMAObserver* uma_observer_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000194 SignalingState signaling_state_;
195 // TODO(bemasc): Remove ice_state_.
196 IceState ice_state_;
197 IceConnectionState ice_connection_state_;
198 IceGatheringState ice_gathering_state_;
199
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000200 rtc::scoped_ptr<cricket::PortAllocator> port_allocator_;
201 rtc::scoped_ptr<WebRtcSession> session_;
202 rtc::scoped_ptr<MediaStreamSignaling> mediastream_signaling_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000203 rtc::scoped_ptr<StatsCollector> stats_;
deadbeef70ab1a12015-09-28 16:53:55 -0700204
205 std::vector<rtc::scoped_refptr<RtpSenderInterface>> senders_;
206 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> receivers_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000207};
208
209} // namespace webrtc
210
211#endif // TALK_APP_WEBRTC_PEERCONNECTION_H_