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