blob: 25880203500296b1b781c0b76c8113edc6f23fff [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
deadbeeffac06552015-11-25 11:26:01 -0800104 rtc::scoped_refptr<RtpSenderInterface> CreateSender(
105 const std::string& kind) override;
106
deadbeef70ab1a12015-09-28 16:53:55 -0700107 std::vector<rtc::scoped_refptr<RtpSenderInterface>> GetSenders()
108 const override;
109 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> GetReceivers()
110 const override;
111
deadbeefa67696b2015-09-29 11:56:26 -0700112 rtc::scoped_refptr<DataChannelInterface> CreateDataChannel(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000113 const std::string& label,
deadbeefa67696b2015-09-29 11:56:26 -0700114 const DataChannelInit* config) override;
115 bool GetStats(StatsObserver* observer,
116 webrtc::MediaStreamTrackInterface* track,
117 StatsOutputLevel level) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000118
deadbeefa67696b2015-09-29 11:56:26 -0700119 SignalingState signaling_state() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000120
121 // TODO(bemasc): Remove ice_state() when callers are removed.
deadbeefa67696b2015-09-29 11:56:26 -0700122 IceState ice_state() override;
123 IceConnectionState ice_connection_state() override;
124 IceGatheringState ice_gathering_state() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000125
deadbeefa67696b2015-09-29 11:56:26 -0700126 const SessionDescriptionInterface* local_description() const override;
127 const SessionDescriptionInterface* remote_description() const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000128
129 // JSEP01
deadbeefa67696b2015-09-29 11:56:26 -0700130 void CreateOffer(CreateSessionDescriptionObserver* observer,
131 const MediaConstraintsInterface* constraints) override;
132 void CreateOffer(CreateSessionDescriptionObserver* observer,
133 const RTCOfferAnswerOptions& options) override;
134 void CreateAnswer(CreateSessionDescriptionObserver* observer,
135 const MediaConstraintsInterface* constraints) override;
136 void SetLocalDescription(SetSessionDescriptionObserver* observer,
137 SessionDescriptionInterface* desc) override;
138 void SetRemoteDescription(SetSessionDescriptionObserver* observer,
139 SessionDescriptionInterface* desc) override;
140 bool SetConfiguration(
141 const PeerConnectionInterface::RTCConfiguration& config) override;
142 bool AddIceCandidate(const IceCandidateInterface* candidate) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000143
deadbeefa67696b2015-09-29 11:56:26 -0700144 void RegisterUMAObserver(UMAObserver* observer) override;
buildbot@webrtc.org1567b8c2014-05-08 19:54:16 +0000145
deadbeefa67696b2015-09-29 11:56:26 -0700146 void Close() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000147
deadbeefab9b2d12015-10-14 11:33:11 -0700148 // Virtual for unit tests.
149 virtual const std::vector<rtc::scoped_refptr<DataChannel>>&
150 sctp_data_channels() const {
151 return sctp_data_channels_;
152 };
153
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000154 protected:
deadbeefa67696b2015-09-29 11:56:26 -0700155 ~PeerConnection() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000156
157 private:
deadbeefab9b2d12015-10-14 11:33:11 -0700158 struct TrackInfo {
159 TrackInfo() : ssrc(0) {}
160 TrackInfo(const std::string& stream_label,
161 const std::string track_id,
162 uint32_t ssrc)
163 : stream_label(stream_label), track_id(track_id), ssrc(ssrc) {}
164 std::string stream_label;
165 std::string track_id;
166 uint32_t ssrc;
167 };
168 typedef std::vector<TrackInfo> TrackInfos;
169
170 struct RemotePeerInfo {
171 RemotePeerInfo()
172 : msid_supported(false),
173 default_audio_track_needed(false),
174 default_video_track_needed(false) {}
175 // True if it has been discovered that the remote peer support MSID.
176 bool msid_supported;
177 // The remote peer indicates in the session description that audio will be
178 // sent but no MSID is given.
179 bool default_audio_track_needed;
180 // The remote peer indicates in the session description that video will be
181 // sent but no MSID is given.
182 bool default_video_track_needed;
183
184 bool IsDefaultMediaStreamNeeded() {
185 return !msid_supported &&
186 (default_audio_track_needed || default_video_track_needed);
187 }
188 };
189
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000190 // Implements MessageHandler.
deadbeefa67696b2015-09-29 11:56:26 -0700191 void OnMessage(rtc::Message* msg) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000192
deadbeefab9b2d12015-10-14 11:33:11 -0700193 void CreateAudioReceiver(MediaStreamInterface* stream,
194 AudioTrackInterface* audio_track,
195 uint32_t ssrc);
196 void CreateVideoReceiver(MediaStreamInterface* stream,
197 VideoTrackInterface* video_track,
198 uint32_t ssrc);
199 void DestroyAudioReceiver(MediaStreamInterface* stream,
200 AudioTrackInterface* audio_track);
201 void DestroyVideoReceiver(MediaStreamInterface* stream,
202 VideoTrackInterface* video_track);
deadbeefab9b2d12015-10-14 11:33:11 -0700203 void DestroyAudioSender(MediaStreamInterface* stream,
204 AudioTrackInterface* audio_track,
205 uint32_t ssrc);
206 void DestroyVideoSender(MediaStreamInterface* stream,
207 VideoTrackInterface* video_track);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000208
209 // Implements IceObserver
Peter Thatcher54360512015-07-08 11:08:35 -0700210 void OnIceConnectionChange(IceConnectionState new_state) override;
211 void OnIceGatheringChange(IceGatheringState new_state) override;
212 void OnIceCandidate(const IceCandidateInterface* candidate) override;
213 void OnIceComplete() override;
214 void OnIceConnectionReceivingChange(bool receiving) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000215
216 // Signals from WebRtcSession.
deadbeefd59daf82015-10-14 15:02:44 -0700217 void OnSessionStateChange(WebRtcSession* session, WebRtcSession::State state);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000218 void ChangeSignalingState(SignalingState signaling_state);
219
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000220 rtc::Thread* signaling_thread() const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000221 return factory_->signaling_thread();
222 }
223
224 void PostSetSessionDescriptionFailure(SetSessionDescriptionObserver* observer,
225 const std::string& error);
deadbeefab9b2d12015-10-14 11:33:11 -0700226 void PostCreateSessionDescriptionFailure(
227 CreateSessionDescriptionObserver* observer,
228 const std::string& error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000229
230 bool IsClosed() const {
231 return signaling_state_ == PeerConnectionInterface::kClosed;
232 }
233
deadbeefab9b2d12015-10-14 11:33:11 -0700234 // Returns a MediaSessionOptions struct with options decided by |options|,
235 // the local MediaStreams and DataChannels.
236 virtual bool GetOptionsForOffer(
237 const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options,
238 cricket::MediaSessionOptions* session_options);
239
240 // Returns a MediaSessionOptions struct with options decided by
241 // |constraints|, the local MediaStreams and DataChannels.
242 virtual bool GetOptionsForAnswer(
243 const MediaConstraintsInterface* constraints,
244 cricket::MediaSessionOptions* session_options);
245
deadbeeffaac4972015-11-12 15:33:07 -0800246 // Remove all local and remote tracks of type |media_type|.
247 // Called when a media type is rejected (m-line set to port 0).
248 void RemoveTracks(cricket::MediaType media_type);
249
deadbeefab9b2d12015-10-14 11:33:11 -0700250 // Makes sure a MediaStream Track is created for each StreamParam in
251 // |streams|. |media_type| is the type of the |streams| and can be either
252 // audio or video.
253 // If a new MediaStream is created it is added to |new_streams|.
254 void UpdateRemoteStreamsList(
255 const std::vector<cricket::StreamParams>& streams,
256 cricket::MediaType media_type,
257 StreamCollection* new_streams);
258
259 // Triggered when a remote track has been seen for the first time in a remote
260 // session description. It creates a remote MediaStreamTrackInterface
261 // implementation and triggers CreateAudioReceiver or CreateVideoReceiver.
262 void OnRemoteTrackSeen(const std::string& stream_label,
263 const std::string& track_id,
264 uint32_t ssrc,
265 cricket::MediaType media_type);
266
267 // Triggered when a remote track has been removed from a remote session
268 // description. It removes the remote track with id |track_id| from a remote
269 // MediaStream and triggers DestroyAudioReceiver or DestroyVideoReceiver.
270 void OnRemoteTrackRemoved(const std::string& stream_label,
271 const std::string& track_id,
272 cricket::MediaType media_type);
273
274 // Finds remote MediaStreams without any tracks and removes them from
275 // |remote_streams_| and notifies the observer that the MediaStreams no longer
276 // exist.
277 void UpdateEndedRemoteMediaStreams();
278
279 void MaybeCreateDefaultStream();
280
281 // Set the MediaStreamTrackInterface::TrackState to |kEnded| on all remote
282 // tracks of type |media_type|.
283 void EndRemoteTracks(cricket::MediaType media_type);
284
285 // Loops through the vector of |streams| and finds added and removed
286 // StreamParams since last time this method was called.
287 // For each new or removed StreamParam, OnLocalTrackSeen or
288 // OnLocalTrackRemoved is invoked.
289 void UpdateLocalTracks(const std::vector<cricket::StreamParams>& streams,
290 cricket::MediaType media_type);
291
292 // Triggered when a local track has been seen for the first time in a local
293 // session description.
294 // This method triggers CreateAudioSender or CreateVideoSender if the rtp
295 // streams in the local SessionDescription can be mapped to a MediaStreamTrack
296 // in a MediaStream in |local_streams_|
297 void OnLocalTrackSeen(const std::string& stream_label,
298 const std::string& track_id,
299 uint32_t ssrc,
300 cricket::MediaType media_type);
301
302 // Triggered when a local track has been removed from a local session
303 // description.
304 // This method triggers DestroyAudioSender or DestroyVideoSender if a stream
305 // has been removed from the local SessionDescription and the stream can be
306 // mapped to a MediaStreamTrack in a MediaStream in |local_streams_|.
307 void OnLocalTrackRemoved(const std::string& stream_label,
308 const std::string& track_id,
309 uint32_t ssrc,
310 cricket::MediaType media_type);
311
312 void UpdateLocalRtpDataChannels(const cricket::StreamParamsVec& streams);
313 void UpdateRemoteRtpDataChannels(const cricket::StreamParamsVec& streams);
314 void UpdateClosingRtpDataChannels(
315 const std::vector<std::string>& active_channels,
316 bool is_local_update);
317 void CreateRemoteRtpDataChannel(const std::string& label,
318 uint32_t remote_ssrc);
319
320 // Creates channel and adds it to the collection of DataChannels that will
321 // be offered in a SessionDescription.
322 rtc::scoped_refptr<DataChannel> InternalCreateDataChannel(
323 const std::string& label,
324 const InternalDataChannelInit* config);
325
326 // Checks if any data channel has been added.
327 bool HasDataChannels() const;
328
329 void AllocateSctpSids(rtc::SSLRole role);
330 void OnSctpDataChannelClosed(DataChannel* channel);
331
332 // Notifications from WebRtcSession relating to BaseChannels.
333 void OnVoiceChannelDestroyed();
334 void OnVideoChannelDestroyed();
335 void OnDataChannelCreated();
336 void OnDataChannelDestroyed();
337 // Called when the cricket::DataChannel receives a message indicating that a
338 // webrtc::DataChannel should be opened.
339 void OnDataChannelOpenMessage(const std::string& label,
340 const InternalDataChannelInit& config);
341
deadbeeffac06552015-11-25 11:26:01 -0800342 RtpSenderInterface* FindSenderById(const std::string& id);
343
deadbeef70ab1a12015-09-28 16:53:55 -0700344 std::vector<rtc::scoped_refptr<RtpSenderInterface>>::iterator
345 FindSenderForTrack(MediaStreamTrackInterface* track);
346 std::vector<rtc::scoped_refptr<RtpReceiverInterface>>::iterator
347 FindReceiverForTrack(MediaStreamTrackInterface* track);
348
deadbeefab9b2d12015-10-14 11:33:11 -0700349 TrackInfos* GetRemoteTracks(cricket::MediaType media_type);
350 TrackInfos* GetLocalTracks(cricket::MediaType media_type);
351 const TrackInfo* FindTrackInfo(const TrackInfos& infos,
352 const std::string& stream_label,
353 const std::string track_id) const;
354
355 // Returns the specified SCTP DataChannel in sctp_data_channels_,
356 // or nullptr if not found.
357 DataChannel* FindDataChannelBySid(int sid) const;
358
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000359 // Storing the factory as a scoped reference pointer ensures that the memory
360 // in the PeerConnectionFactoryImpl remains available as long as the
361 // PeerConnection is running. It is passed to PeerConnection as a raw pointer.
362 // However, since the reference counting is done in the
deadbeefab9b2d12015-10-14 11:33:11 -0700363 // PeerConnectionFactoryInterface all instances created using the raw pointer
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000364 // will refer to the same reference count.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000365 rtc::scoped_refptr<PeerConnectionFactory> factory_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000366 PeerConnectionObserver* observer_;
buildbot@webrtc.org1567b8c2014-05-08 19:54:16 +0000367 UMAObserver* uma_observer_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000368 SignalingState signaling_state_;
369 // TODO(bemasc): Remove ice_state_.
370 IceState ice_state_;
371 IceConnectionState ice_connection_state_;
372 IceGatheringState ice_gathering_state_;
373
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000374 rtc::scoped_ptr<cricket::PortAllocator> port_allocator_;
stefanc1aeaf02015-10-15 07:26:07 -0700375 rtc::scoped_ptr<MediaControllerInterface> media_controller_;
deadbeefab9b2d12015-10-14 11:33:11 -0700376
377 // Streams added via AddStream.
378 rtc::scoped_refptr<StreamCollection> local_streams_;
379 // Streams created as a result of SetRemoteDescription.
380 rtc::scoped_refptr<StreamCollection> remote_streams_;
381
382 // These lists store track info seen in local/remote descriptions.
383 TrackInfos remote_audio_tracks_;
384 TrackInfos remote_video_tracks_;
385 TrackInfos local_audio_tracks_;
386 TrackInfos local_video_tracks_;
387
388 SctpSidAllocator sid_allocator_;
389 // label -> DataChannel
390 std::map<std::string, rtc::scoped_refptr<DataChannel>> rtp_data_channels_;
391 std::vector<rtc::scoped_refptr<DataChannel>> sctp_data_channels_;
392
393 RemotePeerInfo remote_info_;
394 rtc::scoped_ptr<RemoteMediaStreamFactory> remote_stream_factory_;
deadbeef70ab1a12015-09-28 16:53:55 -0700395
396 std::vector<rtc::scoped_refptr<RtpSenderInterface>> senders_;
397 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> receivers_;
deadbeefab9b2d12015-10-14 11:33:11 -0700398
399 // The session_ scoped_ptr is declared at the bottom of PeerConnection
400 // because its destruction fires signals (such as VoiceChannelDestroyed)
401 // which will trigger some final actions in PeerConnection...
402 rtc::scoped_ptr<WebRtcSession> session_;
403 // ... But stats_ depends on session_ so it should be destroyed even earlier.
404 rtc::scoped_ptr<StatsCollector> stats_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000405};
406
407} // namespace webrtc
408
409#endif // TALK_APP_WEBRTC_PEERCONNECTION_H_