blob: a63dd4342358fc96b21a38fe3f67237ed7d98545 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
Henrik Kjellander15583c12016-02-10 10:53:12 +010011#ifndef WEBRTC_API_PEERCONNECTION_H_
12#define WEBRTC_API_PEERCONNECTION_H_
henrike@webrtc.org28e20752013-07-10 00:45:36 +000013
14#include <string>
15
Henrik Kjellander15583c12016-02-10 10:53:12 +010016#include "webrtc/api/dtlsidentitystore.h"
17#include "webrtc/api/peerconnectionfactory.h"
18#include "webrtc/api/peerconnectioninterface.h"
19#include "webrtc/api/rtpreceiverinterface.h"
20#include "webrtc/api/rtpsenderinterface.h"
21#include "webrtc/api/statscollector.h"
22#include "webrtc/api/streamcollection.h"
23#include "webrtc/api/webrtcsession.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000024#include "webrtc/base/scoped_ptr.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000025
26namespace webrtc {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000027
deadbeefeb459812015-12-15 19:24:43 -080028class MediaStreamObserver;
deadbeefab9b2d12015-10-14 11:33:11 -070029class RemoteMediaStreamFactory;
30
deadbeefab9b2d12015-10-14 11:33:11 -070031// Populates |session_options| from |rtc_options|, and returns true if options
32// are valid.
deadbeef0ed85b22016-02-23 17:24:52 -080033// |session_options|->transport_options map entries must exist in order for
34// them to be populated from |rtc_options|.
deadbeefab9b2d12015-10-14 11:33:11 -070035bool ConvertRtcOptionsForOffer(
36 const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options,
37 cricket::MediaSessionOptions* session_options);
38
39// Populates |session_options| from |constraints|, and returns true if all
40// mandatory constraints are satisfied.
deadbeef0ed85b22016-02-23 17:24:52 -080041// Assumes that |session_options|->transport_options map entries exist.
deadbeefab9b2d12015-10-14 11:33:11 -070042bool ParseConstraintsForAnswer(const MediaConstraintsInterface* constraints,
43 cricket::MediaSessionOptions* session_options);
44
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -080045// Parses the URLs for each server in |servers| to build |stun_servers| and
46// |turn_servers|.
deadbeef0a6c4ca2015-10-06 11:38:28 -070047bool ParseIceServers(const PeerConnectionInterface::IceServers& servers,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -080048 cricket::ServerAddresses* stun_servers,
49 std::vector<cricket::RelayServerConfig>* turn_servers);
deadbeef0a6c4ca2015-10-06 11:38:28 -070050
deadbeef70ab1a12015-09-28 16:53:55 -070051// PeerConnection implements the PeerConnectionInterface interface.
deadbeefab9b2d12015-10-14 11:33:11 -070052// It uses WebRtcSession to implement the PeerConnection functionality.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000053class PeerConnection : public PeerConnectionInterface,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000054 public IceObserver,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000055 public rtc::MessageHandler,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000056 public sigslot::has_slots<> {
57 public:
58 explicit PeerConnection(PeerConnectionFactory* factory);
59
deadbeef653b8e02015-11-11 12:55:10 -080060 bool Initialize(
61 const PeerConnectionInterface::RTCConfiguration& configuration,
62 const MediaConstraintsInterface* constraints,
63 rtc::scoped_ptr<cricket::PortAllocator> allocator,
64 rtc::scoped_ptr<DtlsIdentityStoreInterface> dtls_identity_store,
65 PeerConnectionObserver* observer);
66
deadbeefa67696b2015-09-29 11:56:26 -070067 rtc::scoped_refptr<StreamCollectionInterface> local_streams() override;
68 rtc::scoped_refptr<StreamCollectionInterface> remote_streams() override;
69 bool AddStream(MediaStreamInterface* local_stream) override;
70 void RemoveStream(MediaStreamInterface* local_stream) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000071
deadbeefe1f9d832016-01-14 15:35:42 -080072 rtc::scoped_refptr<RtpSenderInterface> AddTrack(
73 MediaStreamTrackInterface* track,
74 std::vector<MediaStreamInterface*> streams) override;
75 bool RemoveTrack(RtpSenderInterface* sender) override;
76
deadbeefab9b2d12015-10-14 11:33:11 -070077 virtual WebRtcSession* session() { return session_.get(); }
78
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
deadbeeffac06552015-11-25 11:26:01 -080082 rtc::scoped_refptr<RtpSenderInterface> CreateSender(
deadbeefbd7d8f72015-12-18 16:58:44 -080083 const std::string& kind,
84 const std::string& stream_id) override;
deadbeeffac06552015-11-25 11:26:01 -080085
deadbeef70ab1a12015-09-28 16:53:55 -070086 std::vector<rtc::scoped_refptr<RtpSenderInterface>> GetSenders()
87 const override;
88 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> GetReceivers()
89 const override;
90
deadbeefa67696b2015-09-29 11:56:26 -070091 rtc::scoped_refptr<DataChannelInterface> CreateDataChannel(
henrike@webrtc.org28e20752013-07-10 00:45:36 +000092 const std::string& label,
deadbeefa67696b2015-09-29 11:56:26 -070093 const DataChannelInit* config) override;
94 bool GetStats(StatsObserver* observer,
95 webrtc::MediaStreamTrackInterface* track,
96 StatsOutputLevel level) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000097
deadbeefa67696b2015-09-29 11:56:26 -070098 SignalingState signaling_state() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000099
100 // TODO(bemasc): Remove ice_state() when callers are removed.
deadbeefa67696b2015-09-29 11:56:26 -0700101 IceState ice_state() override;
102 IceConnectionState ice_connection_state() override;
103 IceGatheringState ice_gathering_state() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000104
deadbeefa67696b2015-09-29 11:56:26 -0700105 const SessionDescriptionInterface* local_description() const override;
106 const SessionDescriptionInterface* remote_description() const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000107
108 // JSEP01
deadbeefa67696b2015-09-29 11:56:26 -0700109 void CreateOffer(CreateSessionDescriptionObserver* observer,
110 const MediaConstraintsInterface* constraints) override;
111 void CreateOffer(CreateSessionDescriptionObserver* observer,
112 const RTCOfferAnswerOptions& options) override;
113 void CreateAnswer(CreateSessionDescriptionObserver* observer,
114 const MediaConstraintsInterface* constraints) override;
115 void SetLocalDescription(SetSessionDescriptionObserver* observer,
116 SessionDescriptionInterface* desc) override;
117 void SetRemoteDescription(SetSessionDescriptionObserver* observer,
118 SessionDescriptionInterface* desc) override;
119 bool SetConfiguration(
120 const PeerConnectionInterface::RTCConfiguration& config) override;
121 bool AddIceCandidate(const IceCandidateInterface* candidate) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000122
deadbeefa67696b2015-09-29 11:56:26 -0700123 void RegisterUMAObserver(UMAObserver* observer) override;
buildbot@webrtc.org1567b8c2014-05-08 19:54:16 +0000124
deadbeefa67696b2015-09-29 11:56:26 -0700125 void Close() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000126
deadbeefab9b2d12015-10-14 11:33:11 -0700127 // Virtual for unit tests.
128 virtual const std::vector<rtc::scoped_refptr<DataChannel>>&
129 sctp_data_channels() const {
130 return sctp_data_channels_;
131 };
132
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000133 protected:
deadbeefa67696b2015-09-29 11:56:26 -0700134 ~PeerConnection() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000135
136 private:
deadbeefab9b2d12015-10-14 11:33:11 -0700137 struct TrackInfo {
138 TrackInfo() : ssrc(0) {}
139 TrackInfo(const std::string& stream_label,
140 const std::string track_id,
141 uint32_t ssrc)
142 : stream_label(stream_label), track_id(track_id), ssrc(ssrc) {}
deadbeefbda7e0b2015-12-08 17:13:40 -0800143 bool operator==(const TrackInfo& other) {
144 return this->stream_label == other.stream_label &&
145 this->track_id == other.track_id && this->ssrc == other.ssrc;
146 }
deadbeefab9b2d12015-10-14 11:33:11 -0700147 std::string stream_label;
148 std::string track_id;
149 uint32_t ssrc;
150 };
151 typedef std::vector<TrackInfo> TrackInfos;
152
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000153 // Implements MessageHandler.
deadbeefa67696b2015-09-29 11:56:26 -0700154 void OnMessage(rtc::Message* msg) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000155
deadbeefab9b2d12015-10-14 11:33:11 -0700156 void CreateAudioReceiver(MediaStreamInterface* stream,
157 AudioTrackInterface* audio_track,
158 uint32_t ssrc);
159 void CreateVideoReceiver(MediaStreamInterface* stream,
160 VideoTrackInterface* video_track,
161 uint32_t ssrc);
162 void DestroyAudioReceiver(MediaStreamInterface* stream,
163 AudioTrackInterface* audio_track);
164 void DestroyVideoReceiver(MediaStreamInterface* stream,
165 VideoTrackInterface* video_track);
deadbeefab9b2d12015-10-14 11:33:11 -0700166 void DestroyAudioSender(MediaStreamInterface* stream,
167 AudioTrackInterface* audio_track,
168 uint32_t ssrc);
169 void DestroyVideoSender(MediaStreamInterface* stream,
170 VideoTrackInterface* video_track);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000171
172 // Implements IceObserver
Peter Thatcher54360512015-07-08 11:08:35 -0700173 void OnIceConnectionChange(IceConnectionState new_state) override;
174 void OnIceGatheringChange(IceGatheringState new_state) override;
175 void OnIceCandidate(const IceCandidateInterface* candidate) override;
Peter Thatcher54360512015-07-08 11:08:35 -0700176 void OnIceConnectionReceivingChange(bool receiving) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000177
178 // Signals from WebRtcSession.
deadbeefd59daf82015-10-14 15:02:44 -0700179 void OnSessionStateChange(WebRtcSession* session, WebRtcSession::State state);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000180 void ChangeSignalingState(SignalingState signaling_state);
181
deadbeefeb459812015-12-15 19:24:43 -0800182 // Signals from MediaStreamObserver.
183 void OnAudioTrackAdded(AudioTrackInterface* track,
184 MediaStreamInterface* stream);
185 void OnAudioTrackRemoved(AudioTrackInterface* track,
186 MediaStreamInterface* stream);
187 void OnVideoTrackAdded(VideoTrackInterface* track,
188 MediaStreamInterface* stream);
189 void OnVideoTrackRemoved(VideoTrackInterface* track,
190 MediaStreamInterface* stream);
191
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000192 rtc::Thread* signaling_thread() const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000193 return factory_->signaling_thread();
194 }
195
196 void PostSetSessionDescriptionFailure(SetSessionDescriptionObserver* observer,
197 const std::string& error);
deadbeefab9b2d12015-10-14 11:33:11 -0700198 void PostCreateSessionDescriptionFailure(
199 CreateSessionDescriptionObserver* observer,
200 const std::string& error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000201
202 bool IsClosed() const {
203 return signaling_state_ == PeerConnectionInterface::kClosed;
204 }
205
deadbeefab9b2d12015-10-14 11:33:11 -0700206 // Returns a MediaSessionOptions struct with options decided by |options|,
207 // the local MediaStreams and DataChannels.
208 virtual bool GetOptionsForOffer(
209 const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options,
210 cricket::MediaSessionOptions* session_options);
211
212 // Returns a MediaSessionOptions struct with options decided by
213 // |constraints|, the local MediaStreams and DataChannels.
214 virtual bool GetOptionsForAnswer(
215 const MediaConstraintsInterface* constraints,
216 cricket::MediaSessionOptions* session_options);
217
deadbeeffaac4972015-11-12 15:33:07 -0800218 // Remove all local and remote tracks of type |media_type|.
219 // Called when a media type is rejected (m-line set to port 0).
220 void RemoveTracks(cricket::MediaType media_type);
221
deadbeefbda7e0b2015-12-08 17:13:40 -0800222 // Makes sure a MediaStreamTrack is created for each StreamParam in |streams|,
223 // and existing MediaStreamTracks are removed if there is no corresponding
224 // StreamParam. If |default_track_needed| is true, a default MediaStreamTrack
225 // is created if it doesn't exist; if false, it's removed if it exists.
226 // |media_type| is the type of the |streams| and can be either audio or video.
deadbeefab9b2d12015-10-14 11:33:11 -0700227 // If a new MediaStream is created it is added to |new_streams|.
228 void UpdateRemoteStreamsList(
229 const std::vector<cricket::StreamParams>& streams,
deadbeefbda7e0b2015-12-08 17:13:40 -0800230 bool default_track_needed,
deadbeefab9b2d12015-10-14 11:33:11 -0700231 cricket::MediaType media_type,
232 StreamCollection* new_streams);
233
234 // Triggered when a remote track has been seen for the first time in a remote
235 // session description. It creates a remote MediaStreamTrackInterface
236 // implementation and triggers CreateAudioReceiver or CreateVideoReceiver.
237 void OnRemoteTrackSeen(const std::string& stream_label,
238 const std::string& track_id,
239 uint32_t ssrc,
240 cricket::MediaType media_type);
241
242 // Triggered when a remote track has been removed from a remote session
243 // description. It removes the remote track with id |track_id| from a remote
244 // MediaStream and triggers DestroyAudioReceiver or DestroyVideoReceiver.
245 void OnRemoteTrackRemoved(const std::string& stream_label,
246 const std::string& track_id,
247 cricket::MediaType media_type);
248
249 // Finds remote MediaStreams without any tracks and removes them from
250 // |remote_streams_| and notifies the observer that the MediaStreams no longer
251 // exist.
252 void UpdateEndedRemoteMediaStreams();
253
deadbeefab9b2d12015-10-14 11:33:11 -0700254 // Set the MediaStreamTrackInterface::TrackState to |kEnded| on all remote
255 // tracks of type |media_type|.
256 void EndRemoteTracks(cricket::MediaType media_type);
257
258 // Loops through the vector of |streams| and finds added and removed
259 // StreamParams since last time this method was called.
260 // For each new or removed StreamParam, OnLocalTrackSeen or
261 // OnLocalTrackRemoved is invoked.
262 void UpdateLocalTracks(const std::vector<cricket::StreamParams>& streams,
263 cricket::MediaType media_type);
264
265 // Triggered when a local track has been seen for the first time in a local
266 // session description.
267 // This method triggers CreateAudioSender or CreateVideoSender if the rtp
268 // streams in the local SessionDescription can be mapped to a MediaStreamTrack
269 // in a MediaStream in |local_streams_|
270 void OnLocalTrackSeen(const std::string& stream_label,
271 const std::string& track_id,
272 uint32_t ssrc,
273 cricket::MediaType media_type);
274
275 // Triggered when a local track has been removed from a local session
276 // description.
277 // This method triggers DestroyAudioSender or DestroyVideoSender if a stream
278 // has been removed from the local SessionDescription and the stream can be
279 // mapped to a MediaStreamTrack in a MediaStream in |local_streams_|.
280 void OnLocalTrackRemoved(const std::string& stream_label,
281 const std::string& track_id,
282 uint32_t ssrc,
283 cricket::MediaType media_type);
284
285 void UpdateLocalRtpDataChannels(const cricket::StreamParamsVec& streams);
286 void UpdateRemoteRtpDataChannels(const cricket::StreamParamsVec& streams);
287 void UpdateClosingRtpDataChannels(
288 const std::vector<std::string>& active_channels,
289 bool is_local_update);
290 void CreateRemoteRtpDataChannel(const std::string& label,
291 uint32_t remote_ssrc);
292
293 // Creates channel and adds it to the collection of DataChannels that will
294 // be offered in a SessionDescription.
295 rtc::scoped_refptr<DataChannel> InternalCreateDataChannel(
296 const std::string& label,
297 const InternalDataChannelInit* config);
298
299 // Checks if any data channel has been added.
300 bool HasDataChannels() const;
301
302 void AllocateSctpSids(rtc::SSLRole role);
303 void OnSctpDataChannelClosed(DataChannel* channel);
304
305 // Notifications from WebRtcSession relating to BaseChannels.
306 void OnVoiceChannelDestroyed();
307 void OnVideoChannelDestroyed();
308 void OnDataChannelCreated();
309 void OnDataChannelDestroyed();
310 // Called when the cricket::DataChannel receives a message indicating that a
311 // webrtc::DataChannel should be opened.
312 void OnDataChannelOpenMessage(const std::string& label,
313 const InternalDataChannelInit& config);
314
deadbeeffac06552015-11-25 11:26:01 -0800315 RtpSenderInterface* FindSenderById(const std::string& id);
316
deadbeef70ab1a12015-09-28 16:53:55 -0700317 std::vector<rtc::scoped_refptr<RtpSenderInterface>>::iterator
318 FindSenderForTrack(MediaStreamTrackInterface* track);
319 std::vector<rtc::scoped_refptr<RtpReceiverInterface>>::iterator
320 FindReceiverForTrack(MediaStreamTrackInterface* track);
321
deadbeefab9b2d12015-10-14 11:33:11 -0700322 TrackInfos* GetRemoteTracks(cricket::MediaType media_type);
323 TrackInfos* GetLocalTracks(cricket::MediaType media_type);
324 const TrackInfo* FindTrackInfo(const TrackInfos& infos,
325 const std::string& stream_label,
326 const std::string track_id) const;
327
328 // Returns the specified SCTP DataChannel in sctp_data_channels_,
329 // or nullptr if not found.
330 DataChannel* FindDataChannelBySid(int sid) const;
331
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000332 // Storing the factory as a scoped reference pointer ensures that the memory
333 // in the PeerConnectionFactoryImpl remains available as long as the
334 // PeerConnection is running. It is passed to PeerConnection as a raw pointer.
335 // However, since the reference counting is done in the
deadbeefab9b2d12015-10-14 11:33:11 -0700336 // PeerConnectionFactoryInterface all instances created using the raw pointer
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000337 // will refer to the same reference count.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000338 rtc::scoped_refptr<PeerConnectionFactory> factory_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000339 PeerConnectionObserver* observer_;
buildbot@webrtc.org1567b8c2014-05-08 19:54:16 +0000340 UMAObserver* uma_observer_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000341 SignalingState signaling_state_;
342 // TODO(bemasc): Remove ice_state_.
343 IceState ice_state_;
344 IceConnectionState ice_connection_state_;
345 IceGatheringState ice_gathering_state_;
346
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000347 rtc::scoped_ptr<cricket::PortAllocator> port_allocator_;
stefanc1aeaf02015-10-15 07:26:07 -0700348 rtc::scoped_ptr<MediaControllerInterface> media_controller_;
deadbeefab9b2d12015-10-14 11:33:11 -0700349
350 // Streams added via AddStream.
351 rtc::scoped_refptr<StreamCollection> local_streams_;
352 // Streams created as a result of SetRemoteDescription.
353 rtc::scoped_refptr<StreamCollection> remote_streams_;
354
deadbeefeb459812015-12-15 19:24:43 -0800355 std::vector<rtc::scoped_ptr<MediaStreamObserver>> stream_observers_;
356
deadbeefab9b2d12015-10-14 11:33:11 -0700357 // These lists store track info seen in local/remote descriptions.
358 TrackInfos remote_audio_tracks_;
359 TrackInfos remote_video_tracks_;
360 TrackInfos local_audio_tracks_;
361 TrackInfos local_video_tracks_;
362
363 SctpSidAllocator sid_allocator_;
364 // label -> DataChannel
365 std::map<std::string, rtc::scoped_refptr<DataChannel>> rtp_data_channels_;
366 std::vector<rtc::scoped_refptr<DataChannel>> sctp_data_channels_;
deadbeefbd292462015-12-14 18:15:29 -0800367 std::vector<rtc::scoped_refptr<DataChannel>> sctp_data_channels_to_free_;
deadbeefab9b2d12015-10-14 11:33:11 -0700368
deadbeefbda7e0b2015-12-08 17:13:40 -0800369 bool remote_peer_supports_msid_ = false;
deadbeefab9b2d12015-10-14 11:33:11 -0700370 rtc::scoped_ptr<RemoteMediaStreamFactory> remote_stream_factory_;
deadbeef70ab1a12015-09-28 16:53:55 -0700371
372 std::vector<rtc::scoped_refptr<RtpSenderInterface>> senders_;
373 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> receivers_;
deadbeefab9b2d12015-10-14 11:33:11 -0700374
375 // The session_ scoped_ptr is declared at the bottom of PeerConnection
376 // because its destruction fires signals (such as VoiceChannelDestroyed)
377 // which will trigger some final actions in PeerConnection...
378 rtc::scoped_ptr<WebRtcSession> session_;
379 // ... But stats_ depends on session_ so it should be destroyed even earlier.
380 rtc::scoped_ptr<StatsCollector> stats_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000381};
382
383} // namespace webrtc
384
Henrik Kjellander15583c12016-02-10 10:53:12 +0100385#endif // WEBRTC_API_PEERCONNECTION_H_