blob: c233388343ec5950ea0f1dcdd0ef51fc9c5e22eb [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/peerconnectionfactory.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000035#include "talk/app/webrtc/peerconnectioninterface.h"
deadbeef70ab1a12015-09-28 16:53:55 -070036#include "talk/app/webrtc/rtpreceiverinterface.h"
37#include "talk/app/webrtc/rtpsenderinterface.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000038#include "talk/app/webrtc/statscollector.h"
39#include "talk/app/webrtc/streamcollection.h"
40#include "talk/app/webrtc/webrtcsession.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000041#include "webrtc/base/scoped_ptr.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000042
43namespace webrtc {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000044
deadbeefab9b2d12015-10-14 11:33:11 -070045class RemoteMediaStreamFactory;
46
henrike@webrtc.org28e20752013-07-10 00:45:36 +000047typedef std::vector<PortAllocatorFactoryInterface::StunConfiguration>
48 StunConfigurations;
49typedef std::vector<PortAllocatorFactoryInterface::TurnConfiguration>
50 TurnConfigurations;
51
deadbeefab9b2d12015-10-14 11:33:11 -070052// Populates |session_options| from |rtc_options|, and returns true if options
53// are valid.
deadbeefab9b2d12015-10-14 11:33:11 -070054bool ConvertRtcOptionsForOffer(
55 const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options,
56 cricket::MediaSessionOptions* session_options);
57
58// Populates |session_options| from |constraints|, and returns true if all
59// mandatory constraints are satisfied.
60bool ParseConstraintsForAnswer(const MediaConstraintsInterface* constraints,
61 cricket::MediaSessionOptions* session_options);
62
deadbeef0a6c4ca2015-10-06 11:38:28 -070063// Parses the URLs for each server in |servers| to build |stun_config| and
64// |turn_config|.
65bool ParseIceServers(const PeerConnectionInterface::IceServers& servers,
66 StunConfigurations* stun_config,
67 TurnConfigurations* turn_config);
68
deadbeef70ab1a12015-09-28 16:53:55 -070069// PeerConnection implements the PeerConnectionInterface interface.
deadbeefab9b2d12015-10-14 11:33:11 -070070// It uses WebRtcSession to implement the PeerConnection functionality.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000071class PeerConnection : public PeerConnectionInterface,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000072 public IceObserver,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000073 public rtc::MessageHandler,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000074 public sigslot::has_slots<> {
75 public:
76 explicit PeerConnection(PeerConnectionFactory* factory);
77
buildbot@webrtc.org41451d42014-05-03 05:39:45 +000078 bool Initialize(
79 const PeerConnectionInterface::RTCConfiguration& configuration,
80 const MediaConstraintsInterface* constraints,
81 PortAllocatorFactoryInterface* allocator_factory,
Henrik Boström5e56c592015-08-11 10:33:13 +020082 rtc::scoped_ptr<DtlsIdentityStoreInterface> dtls_identity_store,
buildbot@webrtc.org41451d42014-05-03 05:39:45 +000083 PeerConnectionObserver* observer);
deadbeefa67696b2015-09-29 11:56:26 -070084 rtc::scoped_refptr<StreamCollectionInterface> local_streams() override;
85 rtc::scoped_refptr<StreamCollectionInterface> remote_streams() override;
86 bool AddStream(MediaStreamInterface* local_stream) override;
87 void RemoveStream(MediaStreamInterface* local_stream) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000088
deadbeefab9b2d12015-10-14 11:33:11 -070089 virtual WebRtcSession* session() { return session_.get(); }
90
deadbeefa67696b2015-09-29 11:56:26 -070091 rtc::scoped_refptr<DtmfSenderInterface> CreateDtmfSender(
92 AudioTrackInterface* track) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000093
deadbeefac9d92c2015-10-26 11:48:22 -070094 rtc::scoped_refptr<RtpSenderInterface> CreateSender(
95 const std::string& kind) override;
96
deadbeef70ab1a12015-09-28 16:53:55 -070097 std::vector<rtc::scoped_refptr<RtpSenderInterface>> GetSenders()
98 const override;
99 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> GetReceivers()
100 const override;
101
deadbeefa67696b2015-09-29 11:56:26 -0700102 rtc::scoped_refptr<DataChannelInterface> CreateDataChannel(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000103 const std::string& label,
deadbeefa67696b2015-09-29 11:56:26 -0700104 const DataChannelInit* config) override;
105 bool GetStats(StatsObserver* observer,
106 webrtc::MediaStreamTrackInterface* track,
107 StatsOutputLevel level) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000108
deadbeefa67696b2015-09-29 11:56:26 -0700109 SignalingState signaling_state() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000110
111 // TODO(bemasc): Remove ice_state() when callers are removed.
deadbeefa67696b2015-09-29 11:56:26 -0700112 IceState ice_state() override;
113 IceConnectionState ice_connection_state() override;
114 IceGatheringState ice_gathering_state() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000115
deadbeefa67696b2015-09-29 11:56:26 -0700116 const SessionDescriptionInterface* local_description() const override;
117 const SessionDescriptionInterface* remote_description() const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000118
119 // JSEP01
deadbeefa67696b2015-09-29 11:56:26 -0700120 void CreateOffer(CreateSessionDescriptionObserver* observer,
121 const MediaConstraintsInterface* constraints) override;
122 void CreateOffer(CreateSessionDescriptionObserver* observer,
123 const RTCOfferAnswerOptions& options) override;
124 void CreateAnswer(CreateSessionDescriptionObserver* observer,
125 const MediaConstraintsInterface* constraints) override;
126 void SetLocalDescription(SetSessionDescriptionObserver* observer,
127 SessionDescriptionInterface* desc) override;
128 void SetRemoteDescription(SetSessionDescriptionObserver* observer,
129 SessionDescriptionInterface* desc) override;
130 bool SetConfiguration(
131 const PeerConnectionInterface::RTCConfiguration& config) override;
132 bool AddIceCandidate(const IceCandidateInterface* candidate) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000133
deadbeefa67696b2015-09-29 11:56:26 -0700134 void RegisterUMAObserver(UMAObserver* observer) override;
buildbot@webrtc.org1567b8c2014-05-08 19:54:16 +0000135
deadbeefa67696b2015-09-29 11:56:26 -0700136 void Close() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000137
deadbeefab9b2d12015-10-14 11:33:11 -0700138 // Virtual for unit tests.
139 virtual const std::vector<rtc::scoped_refptr<DataChannel>>&
140 sctp_data_channels() const {
141 return sctp_data_channels_;
142 };
143
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000144 protected:
deadbeefa67696b2015-09-29 11:56:26 -0700145 ~PeerConnection() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000146
147 private:
deadbeefab9b2d12015-10-14 11:33:11 -0700148 struct TrackInfo {
149 TrackInfo() : ssrc(0) {}
150 TrackInfo(const std::string& stream_label,
151 const std::string track_id,
152 uint32_t ssrc)
153 : stream_label(stream_label), track_id(track_id), ssrc(ssrc) {}
154 std::string stream_label;
155 std::string track_id;
156 uint32_t ssrc;
157 };
158 typedef std::vector<TrackInfo> TrackInfos;
159
160 struct RemotePeerInfo {
161 RemotePeerInfo()
162 : msid_supported(false),
163 default_audio_track_needed(false),
164 default_video_track_needed(false) {}
165 // True if it has been discovered that the remote peer support MSID.
166 bool msid_supported;
167 // The remote peer indicates in the session description that audio will be
168 // sent but no MSID is given.
169 bool default_audio_track_needed;
170 // The remote peer indicates in the session description that video will be
171 // sent but no MSID is given.
172 bool default_video_track_needed;
173
174 bool IsDefaultMediaStreamNeeded() {
175 return !msid_supported &&
176 (default_audio_track_needed || default_video_track_needed);
177 }
178 };
179
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000180 // Implements MessageHandler.
deadbeefa67696b2015-09-29 11:56:26 -0700181 void OnMessage(rtc::Message* msg) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000182
deadbeefab9b2d12015-10-14 11:33:11 -0700183 void CreateAudioReceiver(MediaStreamInterface* stream,
184 AudioTrackInterface* audio_track,
185 uint32_t ssrc);
186 void CreateVideoReceiver(MediaStreamInterface* stream,
187 VideoTrackInterface* video_track,
188 uint32_t ssrc);
189 void DestroyAudioReceiver(MediaStreamInterface* stream,
190 AudioTrackInterface* audio_track);
191 void DestroyVideoReceiver(MediaStreamInterface* stream,
192 VideoTrackInterface* video_track);
deadbeefab9b2d12015-10-14 11:33:11 -0700193 void DestroyAudioSender(MediaStreamInterface* stream,
194 AudioTrackInterface* audio_track,
195 uint32_t ssrc);
196 void DestroyVideoSender(MediaStreamInterface* stream,
197 VideoTrackInterface* video_track);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000198
199 // Implements IceObserver
Peter Thatcher54360512015-07-08 11:08:35 -0700200 void OnIceConnectionChange(IceConnectionState new_state) override;
201 void OnIceGatheringChange(IceGatheringState new_state) override;
202 void OnIceCandidate(const IceCandidateInterface* candidate) override;
203 void OnIceComplete() override;
204 void OnIceConnectionReceivingChange(bool receiving) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000205
206 // Signals from WebRtcSession.
deadbeefd59daf82015-10-14 15:02:44 -0700207 void OnSessionStateChange(WebRtcSession* session, WebRtcSession::State state);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000208 void ChangeSignalingState(SignalingState signaling_state);
209
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000210 rtc::Thread* signaling_thread() const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000211 return factory_->signaling_thread();
212 }
213
214 void PostSetSessionDescriptionFailure(SetSessionDescriptionObserver* observer,
215 const std::string& error);
deadbeefab9b2d12015-10-14 11:33:11 -0700216 void PostCreateSessionDescriptionFailure(
217 CreateSessionDescriptionObserver* observer,
218 const std::string& error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000219
220 bool IsClosed() const {
221 return signaling_state_ == PeerConnectionInterface::kClosed;
222 }
223
deadbeefab9b2d12015-10-14 11:33:11 -0700224 // Returns a MediaSessionOptions struct with options decided by |options|,
225 // the local MediaStreams and DataChannels.
226 virtual bool GetOptionsForOffer(
227 const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options,
228 cricket::MediaSessionOptions* session_options);
229
230 // Returns a MediaSessionOptions struct with options decided by
231 // |constraints|, the local MediaStreams and DataChannels.
232 virtual bool GetOptionsForAnswer(
233 const MediaConstraintsInterface* constraints,
234 cricket::MediaSessionOptions* session_options);
235
236 // Makes sure a MediaStream Track is created for each StreamParam in
237 // |streams|. |media_type| is the type of the |streams| and can be either
238 // audio or video.
239 // If a new MediaStream is created it is added to |new_streams|.
240 void UpdateRemoteStreamsList(
241 const std::vector<cricket::StreamParams>& streams,
242 cricket::MediaType media_type,
243 StreamCollection* new_streams);
244
245 // Triggered when a remote track has been seen for the first time in a remote
246 // session description. It creates a remote MediaStreamTrackInterface
247 // implementation and triggers CreateAudioReceiver or CreateVideoReceiver.
248 void OnRemoteTrackSeen(const std::string& stream_label,
249 const std::string& track_id,
250 uint32_t ssrc,
251 cricket::MediaType media_type);
252
253 // Triggered when a remote track has been removed from a remote session
254 // description. It removes the remote track with id |track_id| from a remote
255 // MediaStream and triggers DestroyAudioReceiver or DestroyVideoReceiver.
256 void OnRemoteTrackRemoved(const std::string& stream_label,
257 const std::string& track_id,
258 cricket::MediaType media_type);
259
260 // Finds remote MediaStreams without any tracks and removes them from
261 // |remote_streams_| and notifies the observer that the MediaStreams no longer
262 // exist.
263 void UpdateEndedRemoteMediaStreams();
264
265 void MaybeCreateDefaultStream();
266
267 // Set the MediaStreamTrackInterface::TrackState to |kEnded| on all remote
268 // tracks of type |media_type|.
269 void EndRemoteTracks(cricket::MediaType media_type);
270
271 // Loops through the vector of |streams| and finds added and removed
272 // StreamParams since last time this method was called.
273 // For each new or removed StreamParam, OnLocalTrackSeen or
274 // OnLocalTrackRemoved is invoked.
275 void UpdateLocalTracks(const std::vector<cricket::StreamParams>& streams,
276 cricket::MediaType media_type);
277
278 // Triggered when a local track has been seen for the first time in a local
279 // session description.
280 // This method triggers CreateAudioSender or CreateVideoSender if the rtp
281 // streams in the local SessionDescription can be mapped to a MediaStreamTrack
282 // in a MediaStream in |local_streams_|
283 void OnLocalTrackSeen(const std::string& stream_label,
284 const std::string& track_id,
285 uint32_t ssrc,
286 cricket::MediaType media_type);
287
288 // Triggered when a local track has been removed from a local session
289 // description.
290 // This method triggers DestroyAudioSender or DestroyVideoSender if a stream
291 // has been removed from the local SessionDescription and the stream can be
292 // mapped to a MediaStreamTrack in a MediaStream in |local_streams_|.
293 void OnLocalTrackRemoved(const std::string& stream_label,
294 const std::string& track_id,
295 uint32_t ssrc,
296 cricket::MediaType media_type);
297
298 void UpdateLocalRtpDataChannels(const cricket::StreamParamsVec& streams);
299 void UpdateRemoteRtpDataChannels(const cricket::StreamParamsVec& streams);
300 void UpdateClosingRtpDataChannels(
301 const std::vector<std::string>& active_channels,
302 bool is_local_update);
303 void CreateRemoteRtpDataChannel(const std::string& label,
304 uint32_t remote_ssrc);
305
306 // Creates channel and adds it to the collection of DataChannels that will
307 // be offered in a SessionDescription.
308 rtc::scoped_refptr<DataChannel> InternalCreateDataChannel(
309 const std::string& label,
310 const InternalDataChannelInit* config);
311
312 // Checks if any data channel has been added.
313 bool HasDataChannels() const;
314
315 void AllocateSctpSids(rtc::SSLRole role);
316 void OnSctpDataChannelClosed(DataChannel* channel);
317
318 // Notifications from WebRtcSession relating to BaseChannels.
319 void OnVoiceChannelDestroyed();
320 void OnVideoChannelDestroyed();
321 void OnDataChannelCreated();
322 void OnDataChannelDestroyed();
323 // Called when the cricket::DataChannel receives a message indicating that a
324 // webrtc::DataChannel should be opened.
325 void OnDataChannelOpenMessage(const std::string& label,
326 const InternalDataChannelInit& config);
327
deadbeefac9d92c2015-10-26 11:48:22 -0700328 RtpSenderInterface* FindSenderById(const std::string& id);
329
deadbeef70ab1a12015-09-28 16:53:55 -0700330 std::vector<rtc::scoped_refptr<RtpSenderInterface>>::iterator
331 FindSenderForTrack(MediaStreamTrackInterface* track);
332 std::vector<rtc::scoped_refptr<RtpReceiverInterface>>::iterator
333 FindReceiverForTrack(MediaStreamTrackInterface* track);
334
deadbeefab9b2d12015-10-14 11:33:11 -0700335 TrackInfos* GetRemoteTracks(cricket::MediaType media_type);
336 TrackInfos* GetLocalTracks(cricket::MediaType media_type);
337 const TrackInfo* FindTrackInfo(const TrackInfos& infos,
338 const std::string& stream_label,
339 const std::string track_id) const;
340
341 // Returns the specified SCTP DataChannel in sctp_data_channels_,
342 // or nullptr if not found.
343 DataChannel* FindDataChannelBySid(int sid) const;
344
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000345 // Storing the factory as a scoped reference pointer ensures that the memory
346 // in the PeerConnectionFactoryImpl remains available as long as the
347 // PeerConnection is running. It is passed to PeerConnection as a raw pointer.
348 // However, since the reference counting is done in the
deadbeefab9b2d12015-10-14 11:33:11 -0700349 // PeerConnectionFactoryInterface all instances created using the raw pointer
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000350 // will refer to the same reference count.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000351 rtc::scoped_refptr<PeerConnectionFactory> factory_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000352 PeerConnectionObserver* observer_;
buildbot@webrtc.org1567b8c2014-05-08 19:54:16 +0000353 UMAObserver* uma_observer_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000354 SignalingState signaling_state_;
355 // TODO(bemasc): Remove ice_state_.
356 IceState ice_state_;
357 IceConnectionState ice_connection_state_;
358 IceGatheringState ice_gathering_state_;
359
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000360 rtc::scoped_ptr<cricket::PortAllocator> port_allocator_;
stefanc1aeaf02015-10-15 07:26:07 -0700361 rtc::scoped_ptr<MediaControllerInterface> media_controller_;
deadbeefab9b2d12015-10-14 11:33:11 -0700362
363 // Streams added via AddStream.
364 rtc::scoped_refptr<StreamCollection> local_streams_;
365 // Streams created as a result of SetRemoteDescription.
366 rtc::scoped_refptr<StreamCollection> remote_streams_;
367
368 // These lists store track info seen in local/remote descriptions.
369 TrackInfos remote_audio_tracks_;
370 TrackInfos remote_video_tracks_;
371 TrackInfos local_audio_tracks_;
372 TrackInfos local_video_tracks_;
373
374 SctpSidAllocator sid_allocator_;
375 // label -> DataChannel
376 std::map<std::string, rtc::scoped_refptr<DataChannel>> rtp_data_channels_;
377 std::vector<rtc::scoped_refptr<DataChannel>> sctp_data_channels_;
378
379 RemotePeerInfo remote_info_;
380 rtc::scoped_ptr<RemoteMediaStreamFactory> remote_stream_factory_;
deadbeef70ab1a12015-09-28 16:53:55 -0700381
382 std::vector<rtc::scoped_refptr<RtpSenderInterface>> senders_;
383 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> receivers_;
deadbeefab9b2d12015-10-14 11:33:11 -0700384
385 // The session_ scoped_ptr is declared at the bottom of PeerConnection
386 // because its destruction fires signals (such as VoiceChannelDestroyed)
387 // which will trigger some final actions in PeerConnection...
388 rtc::scoped_ptr<WebRtcSession> session_;
389 // ... But stats_ depends on session_ so it should be destroyed even earlier.
390 rtc::scoped_ptr<StatsCollector> stats_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000391};
392
393} // namespace webrtc
394
395#endif // TALK_APP_WEBRTC_PEERCONNECTION_H_