OLD | NEW |
---|---|
1 /* | 1 /* |
2 * libjingle | 2 * libjingle |
3 * Copyright 2012 Google Inc. | 3 * Copyright 2012 Google Inc. |
4 * | 4 * |
5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
6 * modification, are permitted provided that the following conditions are met: | 6 * modification, are permitted provided that the following conditions are met: |
7 * | 7 * |
8 * 1. Redistributions of source code must retain the above copyright notice, | 8 * 1. Redistributions of source code must retain the above copyright notice, |
9 * this list of conditions and the following disclaimer. | 9 * this list of conditions and the following disclaimer. |
10 * 2. Redistributions in binary form must reproduce the above copyright notice, | 10 * 2. Redistributions in binary form must reproduce the above copyright notice, |
(...skipping 13 matching lines...) Expand all Loading... | |
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF | 24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 */ | 26 */ |
27 | 27 |
28 #ifndef TALK_APP_WEBRTC_PEERCONNECTION_H_ | 28 #ifndef TALK_APP_WEBRTC_PEERCONNECTION_H_ |
29 #define TALK_APP_WEBRTC_PEERCONNECTION_H_ | 29 #define TALK_APP_WEBRTC_PEERCONNECTION_H_ |
30 | 30 |
31 #include <string> | 31 #include <string> |
32 | 32 |
33 #include "talk/app/webrtc/dtlsidentitystore.h" | 33 #include "talk/app/webrtc/dtlsidentitystore.h" |
34 #include "talk/app/webrtc/mediastreamsignaling.h" | |
35 #include "talk/app/webrtc/peerconnectionfactory.h" | 34 #include "talk/app/webrtc/peerconnectionfactory.h" |
36 #include "talk/app/webrtc/peerconnectioninterface.h" | 35 #include "talk/app/webrtc/peerconnectioninterface.h" |
37 #include "talk/app/webrtc/rtpreceiverinterface.h" | 36 #include "talk/app/webrtc/rtpreceiverinterface.h" |
38 #include "talk/app/webrtc/rtpsenderinterface.h" | 37 #include "talk/app/webrtc/rtpsenderinterface.h" |
39 #include "talk/app/webrtc/statscollector.h" | 38 #include "talk/app/webrtc/statscollector.h" |
40 #include "talk/app/webrtc/streamcollection.h" | 39 #include "talk/app/webrtc/streamcollection.h" |
41 #include "talk/app/webrtc/webrtcsession.h" | 40 #include "talk/app/webrtc/webrtcsession.h" |
42 #include "webrtc/base/scoped_ptr.h" | 41 #include "webrtc/base/scoped_ptr.h" |
43 | 42 |
44 namespace webrtc { | 43 namespace webrtc { |
45 | 44 |
45 class RemoteMediaStreamFactory; | |
46 | |
46 typedef std::vector<PortAllocatorFactoryInterface::StunConfiguration> | 47 typedef std::vector<PortAllocatorFactoryInterface::StunConfiguration> |
47 StunConfigurations; | 48 StunConfigurations; |
48 typedef std::vector<PortAllocatorFactoryInterface::TurnConfiguration> | 49 typedef std::vector<PortAllocatorFactoryInterface::TurnConfiguration> |
49 TurnConfigurations; | 50 TurnConfigurations; |
50 | 51 |
52 // Populates |session_options| from |rtc_options|, and returns true if options | |
53 // are valid. | |
54 // Send streams should already be added to |session_options| before this method | |
55 // is called, as this affects the values of recv_audio and recv_video. | |
56 bool ConvertRtcOptionsForOffer( | |
57 const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options, | |
58 cricket::MediaSessionOptions* session_options); | |
59 | |
60 // Populates |session_options| from |constraints|, and returns true if all | |
61 // mandatory constraints are satisfied. | |
62 bool ParseConstraintsForAnswer(const MediaConstraintsInterface* constraints, | |
63 cricket::MediaSessionOptions* session_options); | |
64 | |
Taylor Brandstetter
2015/10/07 00:26:19
These methods are pulled out of GetOptionsForOffer
| |
51 // PeerConnection implements the PeerConnectionInterface interface. | 65 // PeerConnection implements the PeerConnectionInterface interface. |
52 // It uses MediaStreamSignaling and WebRtcSession to implement | 66 // It uses WebRtcSession to implement the PeerConnection functionality. |
53 // the PeerConnection functionality. | |
54 class PeerConnection : public PeerConnectionInterface, | 67 class PeerConnection : public PeerConnectionInterface, |
55 public MediaStreamSignalingObserver, | |
56 public IceObserver, | 68 public IceObserver, |
57 public rtc::MessageHandler, | 69 public rtc::MessageHandler, |
58 public sigslot::has_slots<> { | 70 public sigslot::has_slots<> { |
59 public: | 71 public: |
60 explicit PeerConnection(PeerConnectionFactory* factory); | 72 explicit PeerConnection(PeerConnectionFactory* factory); |
61 | 73 |
62 bool Initialize( | 74 bool Initialize( |
63 const PeerConnectionInterface::RTCConfiguration& configuration, | 75 const PeerConnectionInterface::RTCConfiguration& configuration, |
64 const MediaConstraintsInterface* constraints, | 76 const MediaConstraintsInterface* constraints, |
65 PortAllocatorFactoryInterface* allocator_factory, | 77 PortAllocatorFactoryInterface* allocator_factory, |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
107 void SetRemoteDescription(SetSessionDescriptionObserver* observer, | 119 void SetRemoteDescription(SetSessionDescriptionObserver* observer, |
108 SessionDescriptionInterface* desc) override; | 120 SessionDescriptionInterface* desc) override; |
109 bool SetConfiguration( | 121 bool SetConfiguration( |
110 const PeerConnectionInterface::RTCConfiguration& config) override; | 122 const PeerConnectionInterface::RTCConfiguration& config) override; |
111 bool AddIceCandidate(const IceCandidateInterface* candidate) override; | 123 bool AddIceCandidate(const IceCandidateInterface* candidate) override; |
112 | 124 |
113 void RegisterUMAObserver(UMAObserver* observer) override; | 125 void RegisterUMAObserver(UMAObserver* observer) override; |
114 | 126 |
115 void Close() override; | 127 void Close() override; |
116 | 128 |
129 // Virtual for unit tests. | |
130 virtual const std::vector<rtc::scoped_refptr<DataChannel>>& | |
131 sctp_data_channels() const { | |
132 return sctp_data_channels_; | |
133 }; | |
134 | |
117 protected: | 135 protected: |
118 ~PeerConnection() override; | 136 ~PeerConnection() override; |
119 | 137 |
120 private: | 138 private: |
139 struct TrackInfo { | |
140 TrackInfo() : ssrc(0) {} | |
141 TrackInfo(const std::string& stream_label, | |
142 const std::string track_id, | |
143 uint32 ssrc) | |
144 : stream_label(stream_label), track_id(track_id), ssrc(ssrc) {} | |
145 std::string stream_label; | |
146 std::string track_id; | |
147 uint32 ssrc; | |
148 }; | |
149 typedef std::vector<TrackInfo> TrackInfos; | |
150 | |
151 struct RemotePeerInfo { | |
152 RemotePeerInfo() | |
153 : msid_supported(false), | |
154 default_audio_track_needed(false), | |
155 default_video_track_needed(false) {} | |
156 // True if it has been discovered that the remote peer support MSID. | |
157 bool msid_supported; | |
158 // The remote peer indicates in the session description that audio will be | |
159 // sent but no MSID is given. | |
160 bool default_audio_track_needed; | |
161 // The remote peer indicates in the session description that video will be | |
162 // sent but no MSID is given. | |
163 bool default_video_track_needed; | |
164 | |
165 bool IsDefaultMediaStreamNeeded() { | |
166 return !msid_supported && | |
167 (default_audio_track_needed || default_video_track_needed); | |
168 } | |
169 }; | |
170 | |
121 // Implements MessageHandler. | 171 // Implements MessageHandler. |
122 void OnMessage(rtc::Message* msg) override; | 172 void OnMessage(rtc::Message* msg) override; |
123 | 173 |
124 // Implements MediaStreamSignalingObserver. | 174 void CreateAudioReceiver(MediaStreamInterface* stream, |
125 void OnAddRemoteStream(MediaStreamInterface* stream) override; | 175 AudioTrackInterface* audio_track, |
126 void OnRemoveRemoteStream(MediaStreamInterface* stream) override; | 176 uint32 ssrc); |
127 void OnAddDataChannel(DataChannelInterface* data_channel) override; | 177 void CreateVideoReceiver(MediaStreamInterface* stream, |
128 void OnAddRemoteAudioTrack(MediaStreamInterface* stream, | 178 VideoTrackInterface* video_track, |
129 AudioTrackInterface* audio_track, | 179 uint32 ssrc); |
130 uint32 ssrc) override; | 180 void DestroyAudioReceiver(MediaStreamInterface* stream, |
131 void OnAddRemoteVideoTrack(MediaStreamInterface* stream, | 181 AudioTrackInterface* audio_track); |
132 VideoTrackInterface* video_track, | 182 void DestroyVideoReceiver(MediaStreamInterface* stream, |
133 uint32 ssrc) override; | 183 VideoTrackInterface* video_track); |
134 void OnRemoveRemoteAudioTrack(MediaStreamInterface* stream, | 184 void CreateAudioSender(MediaStreamInterface* stream, |
135 AudioTrackInterface* audio_track) override; | 185 AudioTrackInterface* audio_track, |
136 void OnRemoveRemoteVideoTrack(MediaStreamInterface* stream, | 186 uint32 ssrc); |
137 VideoTrackInterface* video_track) override; | 187 void CreateVideoSender(MediaStreamInterface* stream, |
138 void OnAddLocalAudioTrack(MediaStreamInterface* stream, | 188 VideoTrackInterface* video_track, |
139 AudioTrackInterface* audio_track, | 189 uint32 ssrc); |
140 uint32 ssrc) override; | 190 void DestroyAudioSender(MediaStreamInterface* stream, |
141 void OnAddLocalVideoTrack(MediaStreamInterface* stream, | 191 AudioTrackInterface* audio_track, |
142 VideoTrackInterface* video_track, | 192 uint32 ssrc); |
143 uint32 ssrc) override; | 193 void DestroyVideoSender(MediaStreamInterface* stream, |
144 void OnRemoveLocalAudioTrack(MediaStreamInterface* stream, | 194 VideoTrackInterface* video_track); |
145 AudioTrackInterface* audio_track, | |
146 uint32 ssrc) override; | |
147 void OnRemoveLocalVideoTrack(MediaStreamInterface* stream, | |
148 VideoTrackInterface* video_track) override; | |
149 void OnRemoveLocalStream(MediaStreamInterface* stream) override; | |
150 | 195 |
151 // Implements IceObserver | 196 // Implements IceObserver |
152 void OnIceConnectionChange(IceConnectionState new_state) override; | 197 void OnIceConnectionChange(IceConnectionState new_state) override; |
153 void OnIceGatheringChange(IceGatheringState new_state) override; | 198 void OnIceGatheringChange(IceGatheringState new_state) override; |
154 void OnIceCandidate(const IceCandidateInterface* candidate) override; | 199 void OnIceCandidate(const IceCandidateInterface* candidate) override; |
155 void OnIceComplete() override; | 200 void OnIceComplete() override; |
156 void OnIceConnectionReceivingChange(bool receiving) override; | 201 void OnIceConnectionReceivingChange(bool receiving) override; |
157 | 202 |
158 // Signals from WebRtcSession. | 203 // Signals from WebRtcSession. |
159 void OnSessionStateChange(cricket::BaseSession* session, | 204 void OnSessionStateChange(cricket::BaseSession* session, |
160 cricket::BaseSession::State state); | 205 cricket::BaseSession::State state); |
161 void ChangeSignalingState(SignalingState signaling_state); | 206 void ChangeSignalingState(SignalingState signaling_state); |
162 | 207 |
163 rtc::Thread* signaling_thread() const { | 208 rtc::Thread* signaling_thread() const { |
164 return factory_->signaling_thread(); | 209 return factory_->signaling_thread(); |
165 } | 210 } |
166 | 211 |
167 void PostSetSessionDescriptionFailure(SetSessionDescriptionObserver* observer, | 212 void PostSetSessionDescriptionFailure(SetSessionDescriptionObserver* observer, |
168 const std::string& error); | 213 const std::string& error); |
214 void PostCreateSessionDescriptionFailure( | |
215 CreateSessionDescriptionObserver* observer, | |
216 const std::string& error); | |
169 | 217 |
170 bool IsClosed() const { | 218 bool IsClosed() const { |
171 return signaling_state_ == PeerConnectionInterface::kClosed; | 219 return signaling_state_ == PeerConnectionInterface::kClosed; |
172 } | 220 } |
173 | 221 |
222 // Returns a MediaSessionOptions struct with options decided by |options|, | |
223 // the local MediaStreams and DataChannels. | |
224 virtual bool GetOptionsForOffer( | |
225 const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options, | |
226 cricket::MediaSessionOptions* session_options); | |
227 | |
228 // Returns a MediaSessionOptions struct with options decided by | |
229 // |constraints|, the local MediaStreams and DataChannels. | |
230 virtual bool GetOptionsForAnswer( | |
231 const MediaConstraintsInterface* constraints, | |
232 cricket::MediaSessionOptions* session_options); | |
233 | |
234 // Makes sure a MediaStream Track is created for each StreamParam in | |
235 // |streams|. |media_type| is the type of the |streams| and can be either | |
236 // audio or video. | |
237 // If a new MediaStream is created it is added to |new_streams|. | |
238 void UpdateRemoteStreamsList( | |
239 const std::vector<cricket::StreamParams>& streams, | |
240 cricket::MediaType media_type, | |
241 StreamCollection* new_streams); | |
242 | |
243 // Triggered when a remote track has been seen for the first time in a remote | |
244 // session description. It creates a remote MediaStreamTrackInterface | |
245 // implementation and triggers CreateAudioReceiver or CreateVideoReceiver. | |
246 void OnRemoteTrackSeen(const std::string& stream_label, | |
247 const std::string& track_id, | |
248 uint32 ssrc, | |
249 cricket::MediaType media_type); | |
250 | |
251 // Triggered when a remote track has been removed from a remote session | |
252 // description. It removes the remote track with id |track_id| from a remote | |
253 // MediaStream and triggers DestroyAudioReceiver or DestroyVideoReceiver. | |
254 void OnRemoteTrackRemoved(const std::string& stream_label, | |
255 const std::string& track_id, | |
256 cricket::MediaType media_type); | |
257 | |
258 // Finds remote MediaStreams without any tracks and removes them from | |
259 // |remote_streams_| and notifies the observer that the MediaStreams no longer | |
260 // exist. | |
261 void UpdateEndedRemoteMediaStreams(); | |
262 | |
263 void MaybeCreateDefaultStream(); | |
264 | |
265 // Set the MediaStreamTrackInterface::TrackState to |kEnded| on all remote | |
266 // tracks of type |media_type|. | |
267 void RejectRemoteTracks(cricket::MediaType media_type); | |
268 | |
269 // Loops through the vector of |streams| and finds added and removed | |
270 // StreamParams since last time this method was called. | |
271 // For each new or removed StreamParam, OnLocalTrackSeen or | |
272 // OnLocalTrackRemoved is invoked. | |
273 void UpdateLocalTracks(const std::vector<cricket::StreamParams>& streams, | |
274 cricket::MediaType media_type); | |
275 | |
276 // Triggered when a local track has been seen for the first time in a local | |
277 // session description. | |
278 // This method triggers CreateAudioSender or CreateVideoSender if the rtp | |
279 // streams in the local SessionDescription can be mapped to a MediaStreamTrack | |
280 // in a MediaStream in |local_streams_| | |
281 void OnLocalTrackSeen(const std::string& stream_label, | |
282 const std::string& track_id, | |
283 uint32 ssrc, | |
284 cricket::MediaType media_type); | |
285 | |
286 // Triggered when a local track has been removed from a local session | |
287 // description. | |
288 // This method triggers DestroyAudioSender or DestroyVideoSender if a stream | |
289 // has been removed from the local SessionDescription and the stream can be | |
290 // mapped to a MediaStreamTrack in a MediaStream in |local_streams_|. | |
291 void OnLocalTrackRemoved(const std::string& stream_label, | |
292 const std::string& track_id, | |
293 uint32 ssrc, | |
294 cricket::MediaType media_type); | |
295 | |
296 void UpdateLocalRtpDataChannels(const cricket::StreamParamsVec& streams); | |
297 void UpdateRemoteRtpDataChannels(const cricket::StreamParamsVec& streams); | |
298 void UpdateClosingRtpDataChannels( | |
299 const std::vector<std::string>& active_channels, | |
300 bool is_local_update); | |
301 void CreateRemoteRtpDataChannel(const std::string& label, uint32 remote_ssrc); | |
302 | |
303 // Creates channel and adds it to the collection of DataChannels that will | |
304 // be offered in a SessionDescription. | |
305 rtc::scoped_refptr<DataChannel> InternalCreateDataChannel( | |
306 const std::string& label, | |
307 const InternalDataChannelInit* config); | |
308 | |
309 // Checks if any data channel has been added. | |
310 bool HasDataChannels() const; | |
311 | |
312 void AllocateSctpSids(rtc::SSLRole role); | |
313 void OnSctpDataChannelClosed(DataChannel* channel); | |
314 | |
315 // Notifications from WebRtcSession relating to BaseChannels. | |
316 void OnVoiceChannelDestroyed(); | |
317 void OnVideoChannelDestroyed(); | |
318 void OnDataChannelCreated(); | |
319 void OnDataChannelDestroyed(); | |
320 // called when the cricket::DataChannel received a message indicating that a | |
pthatcher1
2015/10/07 02:50:51
called = Called
Taylor Brandstetter
2015/10/09 19:54:09
Done.
| |
321 // webrtc::DataChannel should be opened. | |
322 void OnDataChannelCreationRequested(const std::string& label, | |
323 const InternalDataChannelInit& config); | |
324 | |
174 std::vector<rtc::scoped_refptr<RtpSenderInterface>>::iterator | 325 std::vector<rtc::scoped_refptr<RtpSenderInterface>>::iterator |
175 FindSenderForTrack(MediaStreamTrackInterface* track); | 326 FindSenderForTrack(MediaStreamTrackInterface* track); |
176 std::vector<rtc::scoped_refptr<RtpReceiverInterface>>::iterator | 327 std::vector<rtc::scoped_refptr<RtpReceiverInterface>>::iterator |
177 FindReceiverForTrack(MediaStreamTrackInterface* track); | 328 FindReceiverForTrack(MediaStreamTrackInterface* track); |
178 | 329 |
330 TrackInfos* GetRemoteTracks(cricket::MediaType media_type); | |
331 TrackInfos* GetLocalTracks(cricket::MediaType media_type); | |
332 const TrackInfo* FindTrackInfo(const TrackInfos& infos, | |
333 const std::string& stream_label, | |
334 const std::string track_id) const; | |
335 | |
336 // Returns the specified SCTP DataChannel in sctp_data_channels_, | |
337 // or nullptr if not found. | |
338 DataChannel* FindDataChannelBySid(int sid) const; | |
339 | |
179 // Storing the factory as a scoped reference pointer ensures that the memory | 340 // Storing the factory as a scoped reference pointer ensures that the memory |
180 // in the PeerConnectionFactoryImpl remains available as long as the | 341 // in the PeerConnectionFactoryImpl remains available as long as the |
181 // PeerConnection is running. It is passed to PeerConnection as a raw pointer. | 342 // PeerConnection is running. It is passed to PeerConnection as a raw pointer. |
182 // However, since the reference counting is done in the | 343 // However, since the reference counting is done in the |
183 // PeerConnectionFactoryInteface all instances created using the raw pointer | 344 // PeerConnectionFactoryInterface all instances created using the raw pointer |
184 // will refer to the same reference count. | 345 // will refer to the same reference count. |
185 rtc::scoped_refptr<PeerConnectionFactory> factory_; | 346 rtc::scoped_refptr<PeerConnectionFactory> factory_; |
186 PeerConnectionObserver* observer_; | 347 PeerConnectionObserver* observer_; |
187 UMAObserver* uma_observer_; | 348 UMAObserver* uma_observer_; |
188 SignalingState signaling_state_; | 349 SignalingState signaling_state_; |
189 // TODO(bemasc): Remove ice_state_. | 350 // TODO(bemasc): Remove ice_state_. |
190 IceState ice_state_; | 351 IceState ice_state_; |
191 IceConnectionState ice_connection_state_; | 352 IceConnectionState ice_connection_state_; |
192 IceGatheringState ice_gathering_state_; | 353 IceGatheringState ice_gathering_state_; |
193 | 354 |
194 rtc::scoped_ptr<cricket::PortAllocator> port_allocator_; | 355 rtc::scoped_ptr<cricket::PortAllocator> port_allocator_; |
195 rtc::scoped_ptr<WebRtcSession> session_; | |
196 rtc::scoped_ptr<MediaStreamSignaling> mediastream_signaling_; | |
197 rtc::scoped_ptr<StatsCollector> stats_; | 356 rtc::scoped_ptr<StatsCollector> stats_; |
198 | 357 |
358 rtc::scoped_refptr<StreamCollection> local_streams_; | |
359 rtc::scoped_refptr<StreamCollection> remote_streams_; | |
360 | |
361 // These lists store track info seen in local/remote descriptions. | |
362 TrackInfos remote_audio_tracks_; | |
363 TrackInfos remote_video_tracks_; | |
364 TrackInfos local_audio_tracks_; | |
365 TrackInfos local_video_tracks_; | |
366 | |
367 SctpSidAllocator sid_allocator_; | |
368 std::map<std::string, rtc::scoped_refptr<DataChannel>> rtp_data_channels_; | |
369 std::vector<rtc::scoped_refptr<DataChannel>> sctp_data_channels_; | |
370 | |
371 RemotePeerInfo remote_info_; | |
372 rtc::scoped_ptr<RemoteMediaStreamFactory> remote_stream_factory_; | |
pthatcher1
2015/10/07 02:50:51
Can you comment the other things a little. Like,
Taylor Brandstetter
2015/10/09 19:54:09
Done.
| |
373 | |
199 std::vector<rtc::scoped_refptr<RtpSenderInterface>> senders_; | 374 std::vector<rtc::scoped_refptr<RtpSenderInterface>> senders_; |
200 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> receivers_; | 375 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> receivers_; |
376 | |
377 rtc::scoped_ptr<WebRtcSession> session_; | |
Taylor Brandstetter
2015/10/07 00:26:19
Moved session_ down here so it will be destroyed f
pthatcher1
2015/10/07 02:50:51
Can you put a comment in the code saying "This is
Taylor Brandstetter
2015/10/09 19:54:09
Done.
| |
201 }; | 378 }; |
202 | 379 |
203 } // namespace webrtc | 380 } // namespace webrtc |
204 | 381 |
205 #endif // TALK_APP_WEBRTC_PEERCONNECTION_H_ | 382 #endif // TALK_APP_WEBRTC_PEERCONNECTION_H_ |
OLD | NEW |