OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright 2012 The WebRTC project authors. All Rights Reserved. | |
3 * | |
4 * 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. | |
9 */ | |
10 | |
11 #ifndef WEBRTC_API_PEERCONNECTION_H_ | |
12 #define WEBRTC_API_PEERCONNECTION_H_ | |
13 | |
14 #include <string> | |
15 #include <map> | |
16 #include <memory> | |
17 #include <vector> | |
18 | |
19 #include "webrtc/api/peerconnectionfactory.h" | |
20 #include "webrtc/api/peerconnectioninterface.h" | |
21 #include "webrtc/api/rtcstatscollector.h" | |
22 #include "webrtc/api/rtpreceiver.h" | |
23 #include "webrtc/api/rtpsender.h" | |
24 #include "webrtc/api/statscollector.h" | |
25 #include "webrtc/api/streamcollection.h" | |
26 #include "webrtc/api/webrtcsession.h" | |
27 | |
28 namespace webrtc { | |
29 | |
30 class MediaStreamObserver; | |
31 class VideoRtpReceiver; | |
32 class RtcEventLog; | |
33 | |
34 // Populates |session_options| from |rtc_options|, and returns true if options | |
35 // are valid. | |
36 // |session_options|->transport_options map entries must exist in order for | |
37 // them to be populated from |rtc_options|. | |
38 bool ExtractMediaSessionOptions( | |
39 const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options, | |
40 bool is_offer, | |
41 cricket::MediaSessionOptions* session_options); | |
42 | |
43 // Populates |session_options| from |constraints|, and returns true if all | |
44 // mandatory constraints are satisfied. | |
45 // Assumes that |session_options|->transport_options map entries exist. | |
46 // Will also set defaults if corresponding constraints are not present: | |
47 // recv_audio=true, recv_video=true, bundle_enabled=true. | |
48 // Other fields will be left with existing values. | |
49 // | |
50 // Deprecated. Will be removed once callers that use constraints are gone. | |
51 // TODO(hta): Remove when callers are gone. | |
52 // https://bugs.chromium.org/p/webrtc/issues/detail?id=5617 | |
53 bool ParseConstraintsForAnswer(const MediaConstraintsInterface* constraints, | |
54 cricket::MediaSessionOptions* session_options); | |
55 | |
56 // Parses the URLs for each server in |servers| to build |stun_servers| and | |
57 // |turn_servers|. Can return SYNTAX_ERROR if the URL is malformed, or | |
58 // INVALID_PARAMETER if a TURN server is missing |username| or |password|. | |
59 RTCErrorType ParseIceServers( | |
60 const PeerConnectionInterface::IceServers& servers, | |
61 cricket::ServerAddresses* stun_servers, | |
62 std::vector<cricket::RelayServerConfig>* turn_servers); | |
63 | |
64 // PeerConnection implements the PeerConnectionInterface interface. | |
65 // It uses WebRtcSession to implement the PeerConnection functionality. | |
66 class PeerConnection : public PeerConnectionInterface, | |
67 public IceObserver, | |
68 public rtc::MessageHandler, | |
69 public sigslot::has_slots<> { | |
70 public: | |
71 explicit PeerConnection(PeerConnectionFactory* factory); | |
72 | |
73 bool Initialize( | |
74 const PeerConnectionInterface::RTCConfiguration& configuration, | |
75 std::unique_ptr<cricket::PortAllocator> allocator, | |
76 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator, | |
77 PeerConnectionObserver* observer); | |
78 | |
79 rtc::scoped_refptr<StreamCollectionInterface> local_streams() override; | |
80 rtc::scoped_refptr<StreamCollectionInterface> remote_streams() override; | |
81 bool AddStream(MediaStreamInterface* local_stream) override; | |
82 void RemoveStream(MediaStreamInterface* local_stream) override; | |
83 | |
84 rtc::scoped_refptr<RtpSenderInterface> AddTrack( | |
85 MediaStreamTrackInterface* track, | |
86 std::vector<MediaStreamInterface*> streams) override; | |
87 bool RemoveTrack(RtpSenderInterface* sender) override; | |
88 | |
89 virtual WebRtcSession* session() { return session_.get(); } | |
90 | |
91 rtc::scoped_refptr<DtmfSenderInterface> CreateDtmfSender( | |
92 AudioTrackInterface* track) override; | |
93 | |
94 rtc::scoped_refptr<RtpSenderInterface> CreateSender( | |
95 const std::string& kind, | |
96 const std::string& stream_id) override; | |
97 | |
98 std::vector<rtc::scoped_refptr<RtpSenderInterface>> GetSenders() | |
99 const override; | |
100 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> GetReceivers() | |
101 const override; | |
102 | |
103 rtc::scoped_refptr<DataChannelInterface> CreateDataChannel( | |
104 const std::string& label, | |
105 const DataChannelInit* config) override; | |
106 bool GetStats(StatsObserver* observer, | |
107 webrtc::MediaStreamTrackInterface* track, | |
108 StatsOutputLevel level) override; | |
109 void GetStats(RTCStatsCollectorCallback* callback) override; | |
110 | |
111 SignalingState signaling_state() override; | |
112 | |
113 IceConnectionState ice_connection_state() override; | |
114 IceGatheringState ice_gathering_state() override; | |
115 | |
116 const SessionDescriptionInterface* local_description() const override; | |
117 const SessionDescriptionInterface* remote_description() const override; | |
118 const SessionDescriptionInterface* current_local_description() const override; | |
119 const SessionDescriptionInterface* current_remote_description() | |
120 const override; | |
121 const SessionDescriptionInterface* pending_local_description() const override; | |
122 const SessionDescriptionInterface* pending_remote_description() | |
123 const override; | |
124 | |
125 // JSEP01 | |
126 // Deprecated, use version without constraints. | |
127 void CreateOffer(CreateSessionDescriptionObserver* observer, | |
128 const MediaConstraintsInterface* constraints) override; | |
129 void CreateOffer(CreateSessionDescriptionObserver* observer, | |
130 const RTCOfferAnswerOptions& options) override; | |
131 // Deprecated, use version without constraints. | |
132 void CreateAnswer(CreateSessionDescriptionObserver* observer, | |
133 const MediaConstraintsInterface* constraints) override; | |
134 void CreateAnswer(CreateSessionDescriptionObserver* observer, | |
135 const RTCOfferAnswerOptions& options) override; | |
136 void SetLocalDescription(SetSessionDescriptionObserver* observer, | |
137 SessionDescriptionInterface* desc) override; | |
138 void SetRemoteDescription(SetSessionDescriptionObserver* observer, | |
139 SessionDescriptionInterface* desc) override; | |
140 PeerConnectionInterface::RTCConfiguration GetConfiguration() override; | |
141 bool SetConfiguration( | |
142 const PeerConnectionInterface::RTCConfiguration& configuration, | |
143 RTCError* error) override; | |
144 bool SetConfiguration( | |
145 const PeerConnectionInterface::RTCConfiguration& configuration) override { | |
146 return SetConfiguration(configuration, nullptr); | |
147 } | |
148 bool AddIceCandidate(const IceCandidateInterface* candidate) override; | |
149 bool RemoveIceCandidates( | |
150 const std::vector<cricket::Candidate>& candidates) override; | |
151 | |
152 void RegisterUMAObserver(UMAObserver* observer) override; | |
153 | |
154 bool StartRtcEventLog(rtc::PlatformFile file, | |
155 int64_t max_size_bytes) override; | |
156 void StopRtcEventLog() override; | |
157 | |
158 void Close() override; | |
159 | |
160 sigslot::signal1<DataChannel*> SignalDataChannelCreated; | |
161 | |
162 // Virtual for unit tests. | |
163 virtual const std::vector<rtc::scoped_refptr<DataChannel>>& | |
164 sctp_data_channels() const { | |
165 return sctp_data_channels_; | |
166 } | |
167 | |
168 protected: | |
169 ~PeerConnection() override; | |
170 | |
171 private: | |
172 struct TrackInfo { | |
173 TrackInfo() : ssrc(0) {} | |
174 TrackInfo(const std::string& stream_label, | |
175 const std::string track_id, | |
176 uint32_t ssrc) | |
177 : stream_label(stream_label), track_id(track_id), ssrc(ssrc) {} | |
178 bool operator==(const TrackInfo& other) { | |
179 return this->stream_label == other.stream_label && | |
180 this->track_id == other.track_id && this->ssrc == other.ssrc; | |
181 } | |
182 std::string stream_label; | |
183 std::string track_id; | |
184 uint32_t ssrc; | |
185 }; | |
186 typedef std::vector<TrackInfo> TrackInfos; | |
187 | |
188 // Implements MessageHandler. | |
189 void OnMessage(rtc::Message* msg) override; | |
190 | |
191 void CreateAudioReceiver(MediaStreamInterface* stream, | |
192 const std::string& track_id, | |
193 uint32_t ssrc); | |
194 | |
195 void CreateVideoReceiver(MediaStreamInterface* stream, | |
196 const std::string& track_id, | |
197 uint32_t ssrc); | |
198 void DestroyReceiver(const std::string& track_id); | |
199 void DestroyAudioSender(MediaStreamInterface* stream, | |
200 AudioTrackInterface* audio_track, | |
201 uint32_t ssrc); | |
202 void DestroyVideoSender(MediaStreamInterface* stream, | |
203 VideoTrackInterface* video_track); | |
204 | |
205 // Implements IceObserver | |
206 void OnIceConnectionChange(IceConnectionState new_state) override; | |
207 void OnIceGatheringChange(IceGatheringState new_state) override; | |
208 void OnIceCandidate(const IceCandidateInterface* candidate) override; | |
209 void OnIceCandidatesRemoved( | |
210 const std::vector<cricket::Candidate>& candidates) override; | |
211 void OnIceConnectionReceivingChange(bool receiving) override; | |
212 | |
213 // Signals from WebRtcSession. | |
214 void OnSessionStateChange(WebRtcSession* session, WebRtcSession::State state); | |
215 void ChangeSignalingState(SignalingState signaling_state); | |
216 | |
217 // Signals from MediaStreamObserver. | |
218 void OnAudioTrackAdded(AudioTrackInterface* track, | |
219 MediaStreamInterface* stream); | |
220 void OnAudioTrackRemoved(AudioTrackInterface* track, | |
221 MediaStreamInterface* stream); | |
222 void OnVideoTrackAdded(VideoTrackInterface* track, | |
223 MediaStreamInterface* stream); | |
224 void OnVideoTrackRemoved(VideoTrackInterface* track, | |
225 MediaStreamInterface* stream); | |
226 | |
227 rtc::Thread* signaling_thread() const { | |
228 return factory_->signaling_thread(); | |
229 } | |
230 | |
231 rtc::Thread* network_thread() const { return factory_->network_thread(); } | |
232 | |
233 void PostSetSessionDescriptionFailure(SetSessionDescriptionObserver* observer, | |
234 const std::string& error); | |
235 void PostCreateSessionDescriptionFailure( | |
236 CreateSessionDescriptionObserver* observer, | |
237 const std::string& error); | |
238 | |
239 bool IsClosed() const { | |
240 return signaling_state_ == PeerConnectionInterface::kClosed; | |
241 } | |
242 | |
243 // Returns a MediaSessionOptions struct with options decided by |options|, | |
244 // the local MediaStreams and DataChannels. | |
245 virtual bool GetOptionsForOffer( | |
246 const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options, | |
247 cricket::MediaSessionOptions* session_options); | |
248 | |
249 // Returns a MediaSessionOptions struct with options decided by | |
250 // |constraints|, the local MediaStreams and DataChannels. | |
251 // Deprecated, use version without constraints. | |
252 virtual bool GetOptionsForAnswer( | |
253 const MediaConstraintsInterface* constraints, | |
254 cricket::MediaSessionOptions* session_options); | |
255 virtual bool GetOptionsForAnswer( | |
256 const RTCOfferAnswerOptions& options, | |
257 cricket::MediaSessionOptions* session_options); | |
258 | |
259 void InitializeOptionsForAnswer( | |
260 cricket::MediaSessionOptions* session_options); | |
261 | |
262 // Helper function for options processing. | |
263 // Deprecated. | |
264 virtual void FinishOptionsForAnswer( | |
265 cricket::MediaSessionOptions* session_options); | |
266 | |
267 // Remove all local and remote tracks of type |media_type|. | |
268 // Called when a media type is rejected (m-line set to port 0). | |
269 void RemoveTracks(cricket::MediaType media_type); | |
270 | |
271 // Makes sure a MediaStreamTrack is created for each StreamParam in |streams|, | |
272 // and existing MediaStreamTracks are removed if there is no corresponding | |
273 // StreamParam. If |default_track_needed| is true, a default MediaStreamTrack | |
274 // is created if it doesn't exist; if false, it's removed if it exists. | |
275 // |media_type| is the type of the |streams| and can be either audio or video. | |
276 // If a new MediaStream is created it is added to |new_streams|. | |
277 void UpdateRemoteStreamsList( | |
278 const std::vector<cricket::StreamParams>& streams, | |
279 bool default_track_needed, | |
280 cricket::MediaType media_type, | |
281 StreamCollection* new_streams); | |
282 | |
283 // Triggered when a remote track has been seen for the first time in a remote | |
284 // session description. It creates a remote MediaStreamTrackInterface | |
285 // implementation and triggers CreateAudioReceiver or CreateVideoReceiver. | |
286 void OnRemoteTrackSeen(const std::string& stream_label, | |
287 const std::string& track_id, | |
288 uint32_t ssrc, | |
289 cricket::MediaType media_type); | |
290 | |
291 // Triggered when a remote track has been removed from a remote session | |
292 // description. It removes the remote track with id |track_id| from a remote | |
293 // MediaStream and triggers DestroyAudioReceiver or DestroyVideoReceiver. | |
294 void OnRemoteTrackRemoved(const std::string& stream_label, | |
295 const std::string& track_id, | |
296 cricket::MediaType media_type); | |
297 | |
298 // Finds remote MediaStreams without any tracks and removes them from | |
299 // |remote_streams_| and notifies the observer that the MediaStreams no longer | |
300 // exist. | |
301 void UpdateEndedRemoteMediaStreams(); | |
302 | |
303 // Loops through the vector of |streams| and finds added and removed | |
304 // StreamParams since last time this method was called. | |
305 // For each new or removed StreamParam, OnLocalTrackSeen or | |
306 // OnLocalTrackRemoved is invoked. | |
307 void UpdateLocalTracks(const std::vector<cricket::StreamParams>& streams, | |
308 cricket::MediaType media_type); | |
309 | |
310 // Triggered when a local track has been seen for the first time in a local | |
311 // session description. | |
312 // This method triggers CreateAudioSender or CreateVideoSender if the rtp | |
313 // streams in the local SessionDescription can be mapped to a MediaStreamTrack | |
314 // in a MediaStream in |local_streams_| | |
315 void OnLocalTrackSeen(const std::string& stream_label, | |
316 const std::string& track_id, | |
317 uint32_t ssrc, | |
318 cricket::MediaType media_type); | |
319 | |
320 // Triggered when a local track has been removed from a local session | |
321 // description. | |
322 // This method triggers DestroyAudioSender or DestroyVideoSender if a stream | |
323 // has been removed from the local SessionDescription and the stream can be | |
324 // mapped to a MediaStreamTrack in a MediaStream in |local_streams_|. | |
325 void OnLocalTrackRemoved(const std::string& stream_label, | |
326 const std::string& track_id, | |
327 uint32_t ssrc, | |
328 cricket::MediaType media_type); | |
329 | |
330 void UpdateLocalRtpDataChannels(const cricket::StreamParamsVec& streams); | |
331 void UpdateRemoteRtpDataChannels(const cricket::StreamParamsVec& streams); | |
332 void UpdateClosingRtpDataChannels( | |
333 const std::vector<std::string>& active_channels, | |
334 bool is_local_update); | |
335 void CreateRemoteRtpDataChannel(const std::string& label, | |
336 uint32_t remote_ssrc); | |
337 | |
338 // Creates channel and adds it to the collection of DataChannels that will | |
339 // be offered in a SessionDescription. | |
340 rtc::scoped_refptr<DataChannel> InternalCreateDataChannel( | |
341 const std::string& label, | |
342 const InternalDataChannelInit* config); | |
343 | |
344 // Checks if any data channel has been added. | |
345 bool HasDataChannels() const; | |
346 | |
347 void AllocateSctpSids(rtc::SSLRole role); | |
348 void OnSctpDataChannelClosed(DataChannel* channel); | |
349 | |
350 // Notifications from WebRtcSession relating to BaseChannels. | |
351 void OnVoiceChannelCreated(); | |
352 void OnVoiceChannelDestroyed(); | |
353 void OnVideoChannelCreated(); | |
354 void OnVideoChannelDestroyed(); | |
355 void OnDataChannelCreated(); | |
356 void OnDataChannelDestroyed(); | |
357 // Called when the cricket::DataChannel receives a message indicating that a | |
358 // webrtc::DataChannel should be opened. | |
359 void OnDataChannelOpenMessage(const std::string& label, | |
360 const InternalDataChannelInit& config); | |
361 | |
362 RtpSenderInternal* FindSenderById(const std::string& id); | |
363 | |
364 std::vector<rtc::scoped_refptr< | |
365 RtpSenderProxyWithInternal<RtpSenderInternal>>>::iterator | |
366 FindSenderForTrack(MediaStreamTrackInterface* track); | |
367 std::vector<rtc::scoped_refptr< | |
368 RtpReceiverProxyWithInternal<RtpReceiverInternal>>>::iterator | |
369 FindReceiverForTrack(const std::string& track_id); | |
370 | |
371 TrackInfos* GetRemoteTracks(cricket::MediaType media_type); | |
372 TrackInfos* GetLocalTracks(cricket::MediaType media_type); | |
373 const TrackInfo* FindTrackInfo(const TrackInfos& infos, | |
374 const std::string& stream_label, | |
375 const std::string track_id) const; | |
376 | |
377 // Returns the specified SCTP DataChannel in sctp_data_channels_, | |
378 // or nullptr if not found. | |
379 DataChannel* FindDataChannelBySid(int sid) const; | |
380 | |
381 // Called when first configuring the port allocator. | |
382 bool InitializePortAllocator_n(const RTCConfiguration& configuration); | |
383 // Called when SetConfiguration is called to apply the supported subset | |
384 // of the configuration on the network thread. | |
385 bool ReconfigurePortAllocator_n( | |
386 const cricket::ServerAddresses& stun_servers, | |
387 const std::vector<cricket::RelayServerConfig>& turn_servers, | |
388 IceTransportsType type, | |
389 int candidate_pool_size, | |
390 bool prune_turn_ports); | |
391 | |
392 // Starts recording an Rtc EventLog using the supplied platform file. | |
393 // This function should only be called from the worker thread. | |
394 bool StartRtcEventLog_w(rtc::PlatformFile file, int64_t max_size_bytes); | |
395 // Starts recording an Rtc EventLog using the supplied platform file. | |
396 // This function should only be called from the worker thread. | |
397 void StopRtcEventLog_w(); | |
398 | |
399 // Storing the factory as a scoped reference pointer ensures that the memory | |
400 // in the PeerConnectionFactoryImpl remains available as long as the | |
401 // PeerConnection is running. It is passed to PeerConnection as a raw pointer. | |
402 // However, since the reference counting is done in the | |
403 // PeerConnectionFactoryInterface all instances created using the raw pointer | |
404 // will refer to the same reference count. | |
405 rtc::scoped_refptr<PeerConnectionFactory> factory_; | |
406 PeerConnectionObserver* observer_; | |
407 UMAObserver* uma_observer_; | |
408 SignalingState signaling_state_; | |
409 IceConnectionState ice_connection_state_; | |
410 IceGatheringState ice_gathering_state_; | |
411 PeerConnectionInterface::RTCConfiguration configuration_; | |
412 | |
413 std::unique_ptr<cricket::PortAllocator> port_allocator_; | |
414 // The EventLog needs to outlive the media controller. | |
415 std::unique_ptr<RtcEventLog> event_log_; | |
416 std::unique_ptr<MediaControllerInterface> media_controller_; | |
417 | |
418 // One PeerConnection has only one RTCP CNAME. | |
419 // https://tools.ietf.org/html/draft-ietf-rtcweb-rtp-usage-26#section-4.9 | |
420 std::string rtcp_cname_; | |
421 | |
422 // Streams added via AddStream. | |
423 rtc::scoped_refptr<StreamCollection> local_streams_; | |
424 // Streams created as a result of SetRemoteDescription. | |
425 rtc::scoped_refptr<StreamCollection> remote_streams_; | |
426 | |
427 std::vector<std::unique_ptr<MediaStreamObserver>> stream_observers_; | |
428 | |
429 // These lists store track info seen in local/remote descriptions. | |
430 TrackInfos remote_audio_tracks_; | |
431 TrackInfos remote_video_tracks_; | |
432 TrackInfos local_audio_tracks_; | |
433 TrackInfos local_video_tracks_; | |
434 | |
435 SctpSidAllocator sid_allocator_; | |
436 // label -> DataChannel | |
437 std::map<std::string, rtc::scoped_refptr<DataChannel>> rtp_data_channels_; | |
438 std::vector<rtc::scoped_refptr<DataChannel>> sctp_data_channels_; | |
439 std::vector<rtc::scoped_refptr<DataChannel>> sctp_data_channels_to_free_; | |
440 | |
441 bool remote_peer_supports_msid_ = false; | |
442 | |
443 std::vector<rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>> | |
444 senders_; | |
445 std::vector< | |
446 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>> | |
447 receivers_; | |
448 std::unique_ptr<WebRtcSession> session_; | |
449 std::unique_ptr<StatsCollector> stats_; | |
450 rtc::scoped_refptr<RTCStatsCollector> stats_collector_; | |
451 }; | |
452 | |
453 } // namespace webrtc | |
454 | |
455 #endif // WEBRTC_API_PEERCONNECTION_H_ | |
OLD | NEW |