blob: 7ad1940f77a89b68a5bdd57a27a405b13188281a [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.
deadbeefab9b2d12015-10-14 11:33:11 -070033bool ConvertRtcOptionsForOffer(
34 const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options,
35 cricket::MediaSessionOptions* session_options);
36
37// Populates |session_options| from |constraints|, and returns true if all
38// mandatory constraints are satisfied.
39bool ParseConstraintsForAnswer(const MediaConstraintsInterface* constraints,
40 cricket::MediaSessionOptions* session_options);
41
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -080042// Parses the URLs for each server in |servers| to build |stun_servers| and
43// |turn_servers|.
deadbeef0a6c4ca2015-10-06 11:38:28 -070044bool ParseIceServers(const PeerConnectionInterface::IceServers& servers,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -080045 cricket::ServerAddresses* stun_servers,
46 std::vector<cricket::RelayServerConfig>* turn_servers);
deadbeef0a6c4ca2015-10-06 11:38:28 -070047
deadbeef70ab1a12015-09-28 16:53:55 -070048// PeerConnection implements the PeerConnectionInterface interface.
deadbeefab9b2d12015-10-14 11:33:11 -070049// It uses WebRtcSession to implement the PeerConnection functionality.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000050class PeerConnection : public PeerConnectionInterface,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000051 public IceObserver,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000052 public rtc::MessageHandler,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000053 public sigslot::has_slots<> {
54 public:
55 explicit PeerConnection(PeerConnectionFactory* factory);
56
deadbeef653b8e02015-11-11 12:55:10 -080057 bool Initialize(
58 const PeerConnectionInterface::RTCConfiguration& configuration,
59 const MediaConstraintsInterface* constraints,
60 rtc::scoped_ptr<cricket::PortAllocator> allocator,
61 rtc::scoped_ptr<DtlsIdentityStoreInterface> dtls_identity_store,
62 PeerConnectionObserver* observer);
63
deadbeefa67696b2015-09-29 11:56:26 -070064 rtc::scoped_refptr<StreamCollectionInterface> local_streams() override;
65 rtc::scoped_refptr<StreamCollectionInterface> remote_streams() override;
66 bool AddStream(MediaStreamInterface* local_stream) override;
67 void RemoveStream(MediaStreamInterface* local_stream) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000068
deadbeefe1f9d832016-01-14 15:35:42 -080069 rtc::scoped_refptr<RtpSenderInterface> AddTrack(
70 MediaStreamTrackInterface* track,
71 std::vector<MediaStreamInterface*> streams) override;
72 bool RemoveTrack(RtpSenderInterface* sender) override;
73
deadbeefab9b2d12015-10-14 11:33:11 -070074 virtual WebRtcSession* session() { return session_.get(); }
75
deadbeefa67696b2015-09-29 11:56:26 -070076 rtc::scoped_refptr<DtmfSenderInterface> CreateDtmfSender(
77 AudioTrackInterface* track) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000078
deadbeeffac06552015-11-25 11:26:01 -080079 rtc::scoped_refptr<RtpSenderInterface> CreateSender(
deadbeefbd7d8f72015-12-18 16:58:44 -080080 const std::string& kind,
81 const std::string& stream_id) override;
deadbeeffac06552015-11-25 11:26:01 -080082
deadbeef70ab1a12015-09-28 16:53:55 -070083 std::vector<rtc::scoped_refptr<RtpSenderInterface>> GetSenders()
84 const override;
85 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> GetReceivers()
86 const override;
87
deadbeefa67696b2015-09-29 11:56:26 -070088 rtc::scoped_refptr<DataChannelInterface> CreateDataChannel(
henrike@webrtc.org28e20752013-07-10 00:45:36 +000089 const std::string& label,
deadbeefa67696b2015-09-29 11:56:26 -070090 const DataChannelInit* config) override;
91 bool GetStats(StatsObserver* observer,
92 webrtc::MediaStreamTrackInterface* track,
93 StatsOutputLevel level) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000094
deadbeefa67696b2015-09-29 11:56:26 -070095 SignalingState signaling_state() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000096
97 // TODO(bemasc): Remove ice_state() when callers are removed.
deadbeefa67696b2015-09-29 11:56:26 -070098 IceState ice_state() override;
99 IceConnectionState ice_connection_state() override;
100 IceGatheringState ice_gathering_state() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000101
deadbeefa67696b2015-09-29 11:56:26 -0700102 const SessionDescriptionInterface* local_description() const override;
103 const SessionDescriptionInterface* remote_description() const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000104
105 // JSEP01
deadbeefa67696b2015-09-29 11:56:26 -0700106 void CreateOffer(CreateSessionDescriptionObserver* observer,
107 const MediaConstraintsInterface* constraints) override;
108 void CreateOffer(CreateSessionDescriptionObserver* observer,
109 const RTCOfferAnswerOptions& options) override;
110 void CreateAnswer(CreateSessionDescriptionObserver* observer,
111 const MediaConstraintsInterface* constraints) override;
112 void SetLocalDescription(SetSessionDescriptionObserver* observer,
113 SessionDescriptionInterface* desc) override;
114 void SetRemoteDescription(SetSessionDescriptionObserver* observer,
115 SessionDescriptionInterface* desc) override;
116 bool SetConfiguration(
117 const PeerConnectionInterface::RTCConfiguration& config) override;
118 bool AddIceCandidate(const IceCandidateInterface* candidate) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000119
deadbeefa67696b2015-09-29 11:56:26 -0700120 void RegisterUMAObserver(UMAObserver* observer) override;
buildbot@webrtc.org1567b8c2014-05-08 19:54:16 +0000121
deadbeefa67696b2015-09-29 11:56:26 -0700122 void Close() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000123
deadbeefab9b2d12015-10-14 11:33:11 -0700124 // Virtual for unit tests.
125 virtual const std::vector<rtc::scoped_refptr<DataChannel>>&
126 sctp_data_channels() const {
127 return sctp_data_channels_;
128 };
129
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000130 protected:
deadbeefa67696b2015-09-29 11:56:26 -0700131 ~PeerConnection() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000132
133 private:
deadbeefab9b2d12015-10-14 11:33:11 -0700134 struct TrackInfo {
135 TrackInfo() : ssrc(0) {}
136 TrackInfo(const std::string& stream_label,
137 const std::string track_id,
138 uint32_t ssrc)
139 : stream_label(stream_label), track_id(track_id), ssrc(ssrc) {}
deadbeefbda7e0b2015-12-08 17:13:40 -0800140 bool operator==(const TrackInfo& other) {
141 return this->stream_label == other.stream_label &&
142 this->track_id == other.track_id && this->ssrc == other.ssrc;
143 }
deadbeefab9b2d12015-10-14 11:33:11 -0700144 std::string stream_label;
145 std::string track_id;
146 uint32_t ssrc;
147 };
148 typedef std::vector<TrackInfo> TrackInfos;
149
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000150 // Implements MessageHandler.
deadbeefa67696b2015-09-29 11:56:26 -0700151 void OnMessage(rtc::Message* msg) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000152
deadbeefab9b2d12015-10-14 11:33:11 -0700153 void CreateAudioReceiver(MediaStreamInterface* stream,
154 AudioTrackInterface* audio_track,
155 uint32_t ssrc);
156 void CreateVideoReceiver(MediaStreamInterface* stream,
157 VideoTrackInterface* video_track,
158 uint32_t ssrc);
159 void DestroyAudioReceiver(MediaStreamInterface* stream,
160 AudioTrackInterface* audio_track);
161 void DestroyVideoReceiver(MediaStreamInterface* stream,
162 VideoTrackInterface* video_track);
deadbeefab9b2d12015-10-14 11:33:11 -0700163 void DestroyAudioSender(MediaStreamInterface* stream,
164 AudioTrackInterface* audio_track,
165 uint32_t ssrc);
166 void DestroyVideoSender(MediaStreamInterface* stream,
167 VideoTrackInterface* video_track);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000168
169 // Implements IceObserver
Peter Thatcher54360512015-07-08 11:08:35 -0700170 void OnIceConnectionChange(IceConnectionState new_state) override;
171 void OnIceGatheringChange(IceGatheringState new_state) override;
172 void OnIceCandidate(const IceCandidateInterface* candidate) override;
Peter Thatcher54360512015-07-08 11:08:35 -0700173 void OnIceConnectionReceivingChange(bool receiving) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000174
175 // Signals from WebRtcSession.
deadbeefd59daf82015-10-14 15:02:44 -0700176 void OnSessionStateChange(WebRtcSession* session, WebRtcSession::State state);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000177 void ChangeSignalingState(SignalingState signaling_state);
178
deadbeefeb459812015-12-15 19:24:43 -0800179 // Signals from MediaStreamObserver.
180 void OnAudioTrackAdded(AudioTrackInterface* track,
181 MediaStreamInterface* stream);
182 void OnAudioTrackRemoved(AudioTrackInterface* track,
183 MediaStreamInterface* stream);
184 void OnVideoTrackAdded(VideoTrackInterface* track,
185 MediaStreamInterface* stream);
186 void OnVideoTrackRemoved(VideoTrackInterface* track,
187 MediaStreamInterface* stream);
188
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000189 rtc::Thread* signaling_thread() const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000190 return factory_->signaling_thread();
191 }
192
193 void PostSetSessionDescriptionFailure(SetSessionDescriptionObserver* observer,
194 const std::string& error);
deadbeefab9b2d12015-10-14 11:33:11 -0700195 void PostCreateSessionDescriptionFailure(
196 CreateSessionDescriptionObserver* observer,
197 const std::string& error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000198
199 bool IsClosed() const {
200 return signaling_state_ == PeerConnectionInterface::kClosed;
201 }
202
deadbeefab9b2d12015-10-14 11:33:11 -0700203 // Returns a MediaSessionOptions struct with options decided by |options|,
204 // the local MediaStreams and DataChannels.
205 virtual bool GetOptionsForOffer(
206 const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options,
207 cricket::MediaSessionOptions* session_options);
208
209 // Returns a MediaSessionOptions struct with options decided by
210 // |constraints|, the local MediaStreams and DataChannels.
211 virtual bool GetOptionsForAnswer(
212 const MediaConstraintsInterface* constraints,
213 cricket::MediaSessionOptions* session_options);
214
deadbeeffaac4972015-11-12 15:33:07 -0800215 // Remove all local and remote tracks of type |media_type|.
216 // Called when a media type is rejected (m-line set to port 0).
217 void RemoveTracks(cricket::MediaType media_type);
218
deadbeefbda7e0b2015-12-08 17:13:40 -0800219 // Makes sure a MediaStreamTrack is created for each StreamParam in |streams|,
220 // and existing MediaStreamTracks are removed if there is no corresponding
221 // StreamParam. If |default_track_needed| is true, a default MediaStreamTrack
222 // is created if it doesn't exist; if false, it's removed if it exists.
223 // |media_type| is the type of the |streams| and can be either audio or video.
deadbeefab9b2d12015-10-14 11:33:11 -0700224 // If a new MediaStream is created it is added to |new_streams|.
225 void UpdateRemoteStreamsList(
226 const std::vector<cricket::StreamParams>& streams,
deadbeefbda7e0b2015-12-08 17:13:40 -0800227 bool default_track_needed,
deadbeefab9b2d12015-10-14 11:33:11 -0700228 cricket::MediaType media_type,
229 StreamCollection* new_streams);
230
231 // Triggered when a remote track has been seen for the first time in a remote
232 // session description. It creates a remote MediaStreamTrackInterface
233 // implementation and triggers CreateAudioReceiver or CreateVideoReceiver.
234 void OnRemoteTrackSeen(const std::string& stream_label,
235 const std::string& track_id,
236 uint32_t ssrc,
237 cricket::MediaType media_type);
238
239 // Triggered when a remote track has been removed from a remote session
240 // description. It removes the remote track with id |track_id| from a remote
241 // MediaStream and triggers DestroyAudioReceiver or DestroyVideoReceiver.
242 void OnRemoteTrackRemoved(const std::string& stream_label,
243 const std::string& track_id,
244 cricket::MediaType media_type);
245
246 // Finds remote MediaStreams without any tracks and removes them from
247 // |remote_streams_| and notifies the observer that the MediaStreams no longer
248 // exist.
249 void UpdateEndedRemoteMediaStreams();
250
deadbeefab9b2d12015-10-14 11:33:11 -0700251 // Set the MediaStreamTrackInterface::TrackState to |kEnded| on all remote
252 // tracks of type |media_type|.
253 void EndRemoteTracks(cricket::MediaType media_type);
254
255 // Loops through the vector of |streams| and finds added and removed
256 // StreamParams since last time this method was called.
257 // For each new or removed StreamParam, OnLocalTrackSeen or
258 // OnLocalTrackRemoved is invoked.
259 void UpdateLocalTracks(const std::vector<cricket::StreamParams>& streams,
260 cricket::MediaType media_type);
261
262 // Triggered when a local track has been seen for the first time in a local
263 // session description.
264 // This method triggers CreateAudioSender or CreateVideoSender if the rtp
265 // streams in the local SessionDescription can be mapped to a MediaStreamTrack
266 // in a MediaStream in |local_streams_|
267 void OnLocalTrackSeen(const std::string& stream_label,
268 const std::string& track_id,
269 uint32_t ssrc,
270 cricket::MediaType media_type);
271
272 // Triggered when a local track has been removed from a local session
273 // description.
274 // This method triggers DestroyAudioSender or DestroyVideoSender if a stream
275 // has been removed from the local SessionDescription and the stream can be
276 // mapped to a MediaStreamTrack in a MediaStream in |local_streams_|.
277 void OnLocalTrackRemoved(const std::string& stream_label,
278 const std::string& track_id,
279 uint32_t ssrc,
280 cricket::MediaType media_type);
281
282 void UpdateLocalRtpDataChannels(const cricket::StreamParamsVec& streams);
283 void UpdateRemoteRtpDataChannels(const cricket::StreamParamsVec& streams);
284 void UpdateClosingRtpDataChannels(
285 const std::vector<std::string>& active_channels,
286 bool is_local_update);
287 void CreateRemoteRtpDataChannel(const std::string& label,
288 uint32_t remote_ssrc);
289
290 // Creates channel and adds it to the collection of DataChannels that will
291 // be offered in a SessionDescription.
292 rtc::scoped_refptr<DataChannel> InternalCreateDataChannel(
293 const std::string& label,
294 const InternalDataChannelInit* config);
295
296 // Checks if any data channel has been added.
297 bool HasDataChannels() const;
298
299 void AllocateSctpSids(rtc::SSLRole role);
300 void OnSctpDataChannelClosed(DataChannel* channel);
301
302 // Notifications from WebRtcSession relating to BaseChannels.
303 void OnVoiceChannelDestroyed();
304 void OnVideoChannelDestroyed();
305 void OnDataChannelCreated();
306 void OnDataChannelDestroyed();
307 // Called when the cricket::DataChannel receives a message indicating that a
308 // webrtc::DataChannel should be opened.
309 void OnDataChannelOpenMessage(const std::string& label,
310 const InternalDataChannelInit& config);
311
deadbeeffac06552015-11-25 11:26:01 -0800312 RtpSenderInterface* FindSenderById(const std::string& id);
313
deadbeef70ab1a12015-09-28 16:53:55 -0700314 std::vector<rtc::scoped_refptr<RtpSenderInterface>>::iterator
315 FindSenderForTrack(MediaStreamTrackInterface* track);
316 std::vector<rtc::scoped_refptr<RtpReceiverInterface>>::iterator
317 FindReceiverForTrack(MediaStreamTrackInterface* track);
318
deadbeefab9b2d12015-10-14 11:33:11 -0700319 TrackInfos* GetRemoteTracks(cricket::MediaType media_type);
320 TrackInfos* GetLocalTracks(cricket::MediaType media_type);
321 const TrackInfo* FindTrackInfo(const TrackInfos& infos,
322 const std::string& stream_label,
323 const std::string track_id) const;
324
325 // Returns the specified SCTP DataChannel in sctp_data_channels_,
326 // or nullptr if not found.
327 DataChannel* FindDataChannelBySid(int sid) const;
328
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000329 // Storing the factory as a scoped reference pointer ensures that the memory
330 // in the PeerConnectionFactoryImpl remains available as long as the
331 // PeerConnection is running. It is passed to PeerConnection as a raw pointer.
332 // However, since the reference counting is done in the
deadbeefab9b2d12015-10-14 11:33:11 -0700333 // PeerConnectionFactoryInterface all instances created using the raw pointer
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000334 // will refer to the same reference count.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000335 rtc::scoped_refptr<PeerConnectionFactory> factory_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000336 PeerConnectionObserver* observer_;
buildbot@webrtc.org1567b8c2014-05-08 19:54:16 +0000337 UMAObserver* uma_observer_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000338 SignalingState signaling_state_;
339 // TODO(bemasc): Remove ice_state_.
340 IceState ice_state_;
341 IceConnectionState ice_connection_state_;
342 IceGatheringState ice_gathering_state_;
343
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000344 rtc::scoped_ptr<cricket::PortAllocator> port_allocator_;
stefanc1aeaf02015-10-15 07:26:07 -0700345 rtc::scoped_ptr<MediaControllerInterface> media_controller_;
deadbeefab9b2d12015-10-14 11:33:11 -0700346
347 // Streams added via AddStream.
348 rtc::scoped_refptr<StreamCollection> local_streams_;
349 // Streams created as a result of SetRemoteDescription.
350 rtc::scoped_refptr<StreamCollection> remote_streams_;
351
deadbeefeb459812015-12-15 19:24:43 -0800352 std::vector<rtc::scoped_ptr<MediaStreamObserver>> stream_observers_;
353
deadbeefab9b2d12015-10-14 11:33:11 -0700354 // These lists store track info seen in local/remote descriptions.
355 TrackInfos remote_audio_tracks_;
356 TrackInfos remote_video_tracks_;
357 TrackInfos local_audio_tracks_;
358 TrackInfos local_video_tracks_;
359
360 SctpSidAllocator sid_allocator_;
361 // label -> DataChannel
362 std::map<std::string, rtc::scoped_refptr<DataChannel>> rtp_data_channels_;
363 std::vector<rtc::scoped_refptr<DataChannel>> sctp_data_channels_;
deadbeefbd292462015-12-14 18:15:29 -0800364 std::vector<rtc::scoped_refptr<DataChannel>> sctp_data_channels_to_free_;
deadbeefab9b2d12015-10-14 11:33:11 -0700365
deadbeefbda7e0b2015-12-08 17:13:40 -0800366 bool remote_peer_supports_msid_ = false;
deadbeefab9b2d12015-10-14 11:33:11 -0700367 rtc::scoped_ptr<RemoteMediaStreamFactory> remote_stream_factory_;
deadbeef70ab1a12015-09-28 16:53:55 -0700368
369 std::vector<rtc::scoped_refptr<RtpSenderInterface>> senders_;
370 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> receivers_;
deadbeefab9b2d12015-10-14 11:33:11 -0700371
372 // The session_ scoped_ptr is declared at the bottom of PeerConnection
373 // because its destruction fires signals (such as VoiceChannelDestroyed)
374 // which will trigger some final actions in PeerConnection...
375 rtc::scoped_ptr<WebRtcSession> session_;
376 // ... But stats_ depends on session_ so it should be destroyed even earlier.
377 rtc::scoped_ptr<StatsCollector> stats_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000378};
379
380} // namespace webrtc
381
Henrik Kjellander15583c12016-02-10 10:53:12 +0100382#endif // WEBRTC_API_PEERCONNECTION_H_