blob: cd6f67121f2d32384bbda7889c804134023d36b3 [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
deadbeef653b8e02015-11-11 12:55:10 -080078 // TODO(deadbeef): Remove this overload of Initialize once everyone is moved
79 // to the new version.
buildbot@webrtc.org41451d42014-05-03 05:39:45 +000080 bool Initialize(
81 const PeerConnectionInterface::RTCConfiguration& configuration,
82 const MediaConstraintsInterface* constraints,
83 PortAllocatorFactoryInterface* allocator_factory,
Henrik Boström5e56c592015-08-11 10:33:13 +020084 rtc::scoped_ptr<DtlsIdentityStoreInterface> dtls_identity_store,
buildbot@webrtc.org41451d42014-05-03 05:39:45 +000085 PeerConnectionObserver* observer);
deadbeef653b8e02015-11-11 12:55:10 -080086
87 bool Initialize(
88 const PeerConnectionInterface::RTCConfiguration& configuration,
89 const MediaConstraintsInterface* constraints,
90 rtc::scoped_ptr<cricket::PortAllocator> allocator,
91 rtc::scoped_ptr<DtlsIdentityStoreInterface> dtls_identity_store,
92 PeerConnectionObserver* observer);
93
deadbeefa67696b2015-09-29 11:56:26 -070094 rtc::scoped_refptr<StreamCollectionInterface> local_streams() override;
95 rtc::scoped_refptr<StreamCollectionInterface> remote_streams() override;
96 bool AddStream(MediaStreamInterface* local_stream) override;
97 void RemoveStream(MediaStreamInterface* local_stream) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000098
deadbeefab9b2d12015-10-14 11:33:11 -070099 virtual WebRtcSession* session() { return session_.get(); }
100
deadbeefa67696b2015-09-29 11:56:26 -0700101 rtc::scoped_refptr<DtmfSenderInterface> CreateDtmfSender(
102 AudioTrackInterface* track) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000103
deadbeef70ab1a12015-09-28 16:53:55 -0700104 std::vector<rtc::scoped_refptr<RtpSenderInterface>> GetSenders()
105 const override;
106 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> GetReceivers()
107 const override;
108
deadbeefa67696b2015-09-29 11:56:26 -0700109 rtc::scoped_refptr<DataChannelInterface> CreateDataChannel(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000110 const std::string& label,
deadbeefa67696b2015-09-29 11:56:26 -0700111 const DataChannelInit* config) override;
112 bool GetStats(StatsObserver* observer,
113 webrtc::MediaStreamTrackInterface* track,
114 StatsOutputLevel level) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000115
deadbeefa67696b2015-09-29 11:56:26 -0700116 SignalingState signaling_state() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000117
118 // TODO(bemasc): Remove ice_state() when callers are removed.
deadbeefa67696b2015-09-29 11:56:26 -0700119 IceState ice_state() override;
120 IceConnectionState ice_connection_state() override;
121 IceGatheringState ice_gathering_state() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000122
deadbeefa67696b2015-09-29 11:56:26 -0700123 const SessionDescriptionInterface* local_description() const override;
124 const SessionDescriptionInterface* remote_description() const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000125
126 // JSEP01
deadbeefa67696b2015-09-29 11:56:26 -0700127 void CreateOffer(CreateSessionDescriptionObserver* observer,
128 const MediaConstraintsInterface* constraints) override;
129 void CreateOffer(CreateSessionDescriptionObserver* observer,
130 const RTCOfferAnswerOptions& options) override;
131 void CreateAnswer(CreateSessionDescriptionObserver* observer,
132 const MediaConstraintsInterface* constraints) override;
133 void SetLocalDescription(SetSessionDescriptionObserver* observer,
134 SessionDescriptionInterface* desc) override;
135 void SetRemoteDescription(SetSessionDescriptionObserver* observer,
136 SessionDescriptionInterface* desc) override;
137 bool SetConfiguration(
138 const PeerConnectionInterface::RTCConfiguration& config) override;
139 bool AddIceCandidate(const IceCandidateInterface* candidate) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000140
deadbeefa67696b2015-09-29 11:56:26 -0700141 void RegisterUMAObserver(UMAObserver* observer) override;
buildbot@webrtc.org1567b8c2014-05-08 19:54:16 +0000142
deadbeefa67696b2015-09-29 11:56:26 -0700143 void Close() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000144
deadbeefab9b2d12015-10-14 11:33:11 -0700145 // Virtual for unit tests.
146 virtual const std::vector<rtc::scoped_refptr<DataChannel>>&
147 sctp_data_channels() const {
148 return sctp_data_channels_;
149 };
150
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000151 protected:
deadbeefa67696b2015-09-29 11:56:26 -0700152 ~PeerConnection() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000153
154 private:
deadbeefab9b2d12015-10-14 11:33:11 -0700155 struct TrackInfo {
156 TrackInfo() : ssrc(0) {}
157 TrackInfo(const std::string& stream_label,
158 const std::string track_id,
159 uint32_t ssrc)
160 : stream_label(stream_label), track_id(track_id), ssrc(ssrc) {}
161 std::string stream_label;
162 std::string track_id;
163 uint32_t ssrc;
164 };
165 typedef std::vector<TrackInfo> TrackInfos;
166
167 struct RemotePeerInfo {
168 RemotePeerInfo()
169 : msid_supported(false),
170 default_audio_track_needed(false),
171 default_video_track_needed(false) {}
172 // True if it has been discovered that the remote peer support MSID.
173 bool msid_supported;
174 // The remote peer indicates in the session description that audio will be
175 // sent but no MSID is given.
176 bool default_audio_track_needed;
177 // The remote peer indicates in the session description that video will be
178 // sent but no MSID is given.
179 bool default_video_track_needed;
180
181 bool IsDefaultMediaStreamNeeded() {
182 return !msid_supported &&
183 (default_audio_track_needed || default_video_track_needed);
184 }
185 };
186
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000187 // Implements MessageHandler.
deadbeefa67696b2015-09-29 11:56:26 -0700188 void OnMessage(rtc::Message* msg) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000189
deadbeefab9b2d12015-10-14 11:33:11 -0700190 void CreateAudioReceiver(MediaStreamInterface* stream,
191 AudioTrackInterface* audio_track,
192 uint32_t ssrc);
193 void CreateVideoReceiver(MediaStreamInterface* stream,
194 VideoTrackInterface* video_track,
195 uint32_t ssrc);
196 void DestroyAudioReceiver(MediaStreamInterface* stream,
197 AudioTrackInterface* audio_track);
198 void DestroyVideoReceiver(MediaStreamInterface* stream,
199 VideoTrackInterface* video_track);
deadbeef8f46c632015-10-26 14:11:17 -0700200 void CreateAudioSender(MediaStreamInterface* stream,
201 AudioTrackInterface* audio_track,
202 uint32_t ssrc);
203 void CreateVideoSender(MediaStreamInterface* stream,
204 VideoTrackInterface* video_track,
205 uint32_t ssrc);
deadbeefab9b2d12015-10-14 11:33:11 -0700206 void DestroyAudioSender(MediaStreamInterface* stream,
207 AudioTrackInterface* audio_track,
208 uint32_t ssrc);
209 void DestroyVideoSender(MediaStreamInterface* stream,
210 VideoTrackInterface* video_track);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000211
212 // Implements IceObserver
Peter Thatcher54360512015-07-08 11:08:35 -0700213 void OnIceConnectionChange(IceConnectionState new_state) override;
214 void OnIceGatheringChange(IceGatheringState new_state) override;
215 void OnIceCandidate(const IceCandidateInterface* candidate) override;
216 void OnIceComplete() override;
217 void OnIceConnectionReceivingChange(bool receiving) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000218
219 // Signals from WebRtcSession.
deadbeefd59daf82015-10-14 15:02:44 -0700220 void OnSessionStateChange(WebRtcSession* session, WebRtcSession::State state);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000221 void ChangeSignalingState(SignalingState signaling_state);
222
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000223 rtc::Thread* signaling_thread() const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000224 return factory_->signaling_thread();
225 }
226
227 void PostSetSessionDescriptionFailure(SetSessionDescriptionObserver* observer,
228 const std::string& error);
deadbeefab9b2d12015-10-14 11:33:11 -0700229 void PostCreateSessionDescriptionFailure(
230 CreateSessionDescriptionObserver* observer,
231 const std::string& error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000232
233 bool IsClosed() const {
234 return signaling_state_ == PeerConnectionInterface::kClosed;
235 }
236
deadbeefab9b2d12015-10-14 11:33:11 -0700237 // Returns a MediaSessionOptions struct with options decided by |options|,
238 // the local MediaStreams and DataChannels.
239 virtual bool GetOptionsForOffer(
240 const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options,
241 cricket::MediaSessionOptions* session_options);
242
243 // Returns a MediaSessionOptions struct with options decided by
244 // |constraints|, the local MediaStreams and DataChannels.
245 virtual bool GetOptionsForAnswer(
246 const MediaConstraintsInterface* constraints,
247 cricket::MediaSessionOptions* session_options);
248
deadbeeffaac4972015-11-12 15:33:07 -0800249 // Remove all local and remote tracks of type |media_type|.
250 // Called when a media type is rejected (m-line set to port 0).
251 void RemoveTracks(cricket::MediaType media_type);
252
deadbeefab9b2d12015-10-14 11:33:11 -0700253 // Makes sure a MediaStream Track is created for each StreamParam in
254 // |streams|. |media_type| is the type of the |streams| and can be either
255 // audio or video.
256 // If a new MediaStream is created it is added to |new_streams|.
257 void UpdateRemoteStreamsList(
258 const std::vector<cricket::StreamParams>& streams,
259 cricket::MediaType media_type,
260 StreamCollection* new_streams);
261
262 // Triggered when a remote track has been seen for the first time in a remote
263 // session description. It creates a remote MediaStreamTrackInterface
264 // implementation and triggers CreateAudioReceiver or CreateVideoReceiver.
265 void OnRemoteTrackSeen(const std::string& stream_label,
266 const std::string& track_id,
267 uint32_t ssrc,
268 cricket::MediaType media_type);
269
270 // Triggered when a remote track has been removed from a remote session
271 // description. It removes the remote track with id |track_id| from a remote
272 // MediaStream and triggers DestroyAudioReceiver or DestroyVideoReceiver.
273 void OnRemoteTrackRemoved(const std::string& stream_label,
274 const std::string& track_id,
275 cricket::MediaType media_type);
276
277 // Finds remote MediaStreams without any tracks and removes them from
278 // |remote_streams_| and notifies the observer that the MediaStreams no longer
279 // exist.
280 void UpdateEndedRemoteMediaStreams();
281
282 void MaybeCreateDefaultStream();
283
284 // Set the MediaStreamTrackInterface::TrackState to |kEnded| on all remote
285 // tracks of type |media_type|.
286 void EndRemoteTracks(cricket::MediaType media_type);
287
288 // Loops through the vector of |streams| and finds added and removed
289 // StreamParams since last time this method was called.
290 // For each new or removed StreamParam, OnLocalTrackSeen or
291 // OnLocalTrackRemoved is invoked.
292 void UpdateLocalTracks(const std::vector<cricket::StreamParams>& streams,
293 cricket::MediaType media_type);
294
295 // Triggered when a local track has been seen for the first time in a local
296 // session description.
297 // This method triggers CreateAudioSender or CreateVideoSender if the rtp
298 // streams in the local SessionDescription can be mapped to a MediaStreamTrack
299 // in a MediaStream in |local_streams_|
300 void OnLocalTrackSeen(const std::string& stream_label,
301 const std::string& track_id,
302 uint32_t ssrc,
303 cricket::MediaType media_type);
304
305 // Triggered when a local track has been removed from a local session
306 // description.
307 // This method triggers DestroyAudioSender or DestroyVideoSender if a stream
308 // has been removed from the local SessionDescription and the stream can be
309 // mapped to a MediaStreamTrack in a MediaStream in |local_streams_|.
310 void OnLocalTrackRemoved(const std::string& stream_label,
311 const std::string& track_id,
312 uint32_t ssrc,
313 cricket::MediaType media_type);
314
315 void UpdateLocalRtpDataChannels(const cricket::StreamParamsVec& streams);
316 void UpdateRemoteRtpDataChannels(const cricket::StreamParamsVec& streams);
317 void UpdateClosingRtpDataChannels(
318 const std::vector<std::string>& active_channels,
319 bool is_local_update);
320 void CreateRemoteRtpDataChannel(const std::string& label,
321 uint32_t remote_ssrc);
322
323 // Creates channel and adds it to the collection of DataChannels that will
324 // be offered in a SessionDescription.
325 rtc::scoped_refptr<DataChannel> InternalCreateDataChannel(
326 const std::string& label,
327 const InternalDataChannelInit* config);
328
329 // Checks if any data channel has been added.
330 bool HasDataChannels() const;
331
332 void AllocateSctpSids(rtc::SSLRole role);
333 void OnSctpDataChannelClosed(DataChannel* channel);
334
335 // Notifications from WebRtcSession relating to BaseChannels.
336 void OnVoiceChannelDestroyed();
337 void OnVideoChannelDestroyed();
338 void OnDataChannelCreated();
339 void OnDataChannelDestroyed();
340 // Called when the cricket::DataChannel receives a message indicating that a
341 // webrtc::DataChannel should be opened.
342 void OnDataChannelOpenMessage(const std::string& label,
343 const InternalDataChannelInit& config);
344
deadbeef70ab1a12015-09-28 16:53:55 -0700345 std::vector<rtc::scoped_refptr<RtpSenderInterface>>::iterator
346 FindSenderForTrack(MediaStreamTrackInterface* track);
347 std::vector<rtc::scoped_refptr<RtpReceiverInterface>>::iterator
348 FindReceiverForTrack(MediaStreamTrackInterface* track);
349
deadbeefab9b2d12015-10-14 11:33:11 -0700350 TrackInfos* GetRemoteTracks(cricket::MediaType media_type);
351 TrackInfos* GetLocalTracks(cricket::MediaType media_type);
352 const TrackInfo* FindTrackInfo(const TrackInfos& infos,
353 const std::string& stream_label,
354 const std::string track_id) const;
355
356 // Returns the specified SCTP DataChannel in sctp_data_channels_,
357 // or nullptr if not found.
358 DataChannel* FindDataChannelBySid(int sid) const;
359
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000360 // Storing the factory as a scoped reference pointer ensures that the memory
361 // in the PeerConnectionFactoryImpl remains available as long as the
362 // PeerConnection is running. It is passed to PeerConnection as a raw pointer.
363 // However, since the reference counting is done in the
deadbeefab9b2d12015-10-14 11:33:11 -0700364 // PeerConnectionFactoryInterface all instances created using the raw pointer
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000365 // will refer to the same reference count.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000366 rtc::scoped_refptr<PeerConnectionFactory> factory_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000367 PeerConnectionObserver* observer_;
buildbot@webrtc.org1567b8c2014-05-08 19:54:16 +0000368 UMAObserver* uma_observer_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000369 SignalingState signaling_state_;
370 // TODO(bemasc): Remove ice_state_.
371 IceState ice_state_;
372 IceConnectionState ice_connection_state_;
373 IceGatheringState ice_gathering_state_;
374
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000375 rtc::scoped_ptr<cricket::PortAllocator> port_allocator_;
stefanc1aeaf02015-10-15 07:26:07 -0700376 rtc::scoped_ptr<MediaControllerInterface> media_controller_;
deadbeefab9b2d12015-10-14 11:33:11 -0700377
378 // Streams added via AddStream.
379 rtc::scoped_refptr<StreamCollection> local_streams_;
380 // Streams created as a result of SetRemoteDescription.
381 rtc::scoped_refptr<StreamCollection> remote_streams_;
382
383 // These lists store track info seen in local/remote descriptions.
384 TrackInfos remote_audio_tracks_;
385 TrackInfos remote_video_tracks_;
386 TrackInfos local_audio_tracks_;
387 TrackInfos local_video_tracks_;
388
389 SctpSidAllocator sid_allocator_;
390 // label -> DataChannel
391 std::map<std::string, rtc::scoped_refptr<DataChannel>> rtp_data_channels_;
392 std::vector<rtc::scoped_refptr<DataChannel>> sctp_data_channels_;
393
394 RemotePeerInfo remote_info_;
395 rtc::scoped_ptr<RemoteMediaStreamFactory> remote_stream_factory_;
deadbeef70ab1a12015-09-28 16:53:55 -0700396
397 std::vector<rtc::scoped_refptr<RtpSenderInterface>> senders_;
398 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> receivers_;
deadbeefab9b2d12015-10-14 11:33:11 -0700399
400 // The session_ scoped_ptr is declared at the bottom of PeerConnection
401 // because its destruction fires signals (such as VoiceChannelDestroyed)
402 // which will trigger some final actions in PeerConnection...
403 rtc::scoped_ptr<WebRtcSession> session_;
404 // ... But stats_ depends on session_ so it should be destroyed even earlier.
405 rtc::scoped_ptr<StatsCollector> stats_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000406};
407
408} // namespace webrtc
409
410#endif // TALK_APP_WEBRTC_PEERCONNECTION_H_