| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * libjingle | |
| 3 * Copyright 2012 Google Inc. | |
| 4 * | |
| 5 * Redistribution and use in source and binary forms, with or without | |
| 6 * modification, are permitted provided that the following conditions are met: | |
| 7 * | |
| 8 * 1. Redistributions of source code must retain the above copyright notice, | |
| 9 * this list of conditions and the following disclaimer. | |
| 10 * 2. Redistributions in binary form must reproduce the above copyright notice, | |
| 11 * this list of conditions and the following disclaimer in the documentation | |
| 12 * and/or other materials provided with the distribution. | |
| 13 * 3. The name of the author may not be used to endorse or promote products | |
| 14 * derived from this software without specific prior written permission. | |
| 15 * | |
| 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED | |
| 17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | |
| 18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO | |
| 19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
| 20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
| 21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; | |
| 22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | |
| 23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR | |
| 24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF | |
| 25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 26 */ | |
| 27 | |
| 28 #ifndef TALK_APP_WEBRTC_PEERCONNECTION_H_ | |
| 29 #define TALK_APP_WEBRTC_PEERCONNECTION_H_ | |
| 30 | |
| 31 #include <string> | |
| 32 | |
| 33 #include "talk/app/webrtc/dtlsidentitystore.h" | |
| 34 #include "talk/app/webrtc/peerconnectionfactory.h" | |
| 35 #include "talk/app/webrtc/peerconnectioninterface.h" | |
| 36 #include "talk/app/webrtc/rtpreceiverinterface.h" | |
| 37 #include "talk/app/webrtc/rtpsenderinterface.h" | |
| 38 #include "talk/app/webrtc/statscollector.h" | |
| 39 #include "talk/app/webrtc/streamcollection.h" | |
| 40 #include "talk/app/webrtc/webrtcsession.h" | |
| 41 #include "webrtc/base/scoped_ptr.h" | |
| 42 | |
| 43 namespace webrtc { | |
| 44 | |
| 45 class MediaStreamObserver; | |
| 46 class RemoteMediaStreamFactory; | |
| 47 | |
| 48 // Populates |session_options| from |rtc_options|, and returns true if options | |
| 49 // are valid. | |
| 50 bool ConvertRtcOptionsForOffer( | |
| 51 const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options, | |
| 52 cricket::MediaSessionOptions* session_options); | |
| 53 | |
| 54 // Populates |session_options| from |constraints|, and returns true if all | |
| 55 // mandatory constraints are satisfied. | |
| 56 bool ParseConstraintsForAnswer(const MediaConstraintsInterface* constraints, | |
| 57 cricket::MediaSessionOptions* session_options); | |
| 58 | |
| 59 // Parses the URLs for each server in |servers| to build |stun_servers| and | |
| 60 // |turn_servers|. | |
| 61 bool ParseIceServers(const PeerConnectionInterface::IceServers& servers, | |
| 62 cricket::ServerAddresses* stun_servers, | |
| 63 std::vector<cricket::RelayServerConfig>* turn_servers); | |
| 64 | |
| 65 // PeerConnection implements the PeerConnectionInterface interface. | |
| 66 // It uses WebRtcSession to implement the PeerConnection functionality. | |
| 67 class PeerConnection : public PeerConnectionInterface, | |
| 68 public IceObserver, | |
| 69 public rtc::MessageHandler, | |
| 70 public sigslot::has_slots<> { | |
| 71 public: | |
| 72 explicit PeerConnection(PeerConnectionFactory* factory); | |
| 73 | |
| 74 bool Initialize( | |
| 75 const PeerConnectionInterface::RTCConfiguration& configuration, | |
| 76 const MediaConstraintsInterface* constraints, | |
| 77 rtc::scoped_ptr<cricket::PortAllocator> allocator, | |
| 78 rtc::scoped_ptr<DtlsIdentityStoreInterface> dtls_identity_store, | |
| 79 PeerConnectionObserver* observer); | |
| 80 | |
| 81 rtc::scoped_refptr<StreamCollectionInterface> local_streams() override; | |
| 82 rtc::scoped_refptr<StreamCollectionInterface> remote_streams() override; | |
| 83 bool AddStream(MediaStreamInterface* local_stream) override; | |
| 84 void RemoveStream(MediaStreamInterface* local_stream) override; | |
| 85 | |
| 86 rtc::scoped_refptr<RtpSenderInterface> AddTrack( | |
| 87 MediaStreamTrackInterface* track, | |
| 88 std::vector<MediaStreamInterface*> streams) override; | |
| 89 bool RemoveTrack(RtpSenderInterface* sender) override; | |
| 90 | |
| 91 virtual WebRtcSession* session() { return session_.get(); } | |
| 92 | |
| 93 rtc::scoped_refptr<DtmfSenderInterface> CreateDtmfSender( | |
| 94 AudioTrackInterface* track) override; | |
| 95 | |
| 96 rtc::scoped_refptr<RtpSenderInterface> CreateSender( | |
| 97 const std::string& kind, | |
| 98 const std::string& stream_id) override; | |
| 99 | |
| 100 std::vector<rtc::scoped_refptr<RtpSenderInterface>> GetSenders() | |
| 101 const override; | |
| 102 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> GetReceivers() | |
| 103 const override; | |
| 104 | |
| 105 rtc::scoped_refptr<DataChannelInterface> CreateDataChannel( | |
| 106 const std::string& label, | |
| 107 const DataChannelInit* config) override; | |
| 108 bool GetStats(StatsObserver* observer, | |
| 109 webrtc::MediaStreamTrackInterface* track, | |
| 110 StatsOutputLevel level) override; | |
| 111 | |
| 112 SignalingState signaling_state() override; | |
| 113 | |
| 114 // TODO(bemasc): Remove ice_state() when callers are removed. | |
| 115 IceState ice_state() override; | |
| 116 IceConnectionState ice_connection_state() override; | |
| 117 IceGatheringState ice_gathering_state() override; | |
| 118 | |
| 119 const SessionDescriptionInterface* local_description() const override; | |
| 120 const SessionDescriptionInterface* remote_description() const override; | |
| 121 | |
| 122 // JSEP01 | |
| 123 void CreateOffer(CreateSessionDescriptionObserver* observer, | |
| 124 const MediaConstraintsInterface* constraints) override; | |
| 125 void CreateOffer(CreateSessionDescriptionObserver* observer, | |
| 126 const RTCOfferAnswerOptions& options) override; | |
| 127 void CreateAnswer(CreateSessionDescriptionObserver* observer, | |
| 128 const MediaConstraintsInterface* constraints) override; | |
| 129 void SetLocalDescription(SetSessionDescriptionObserver* observer, | |
| 130 SessionDescriptionInterface* desc) override; | |
| 131 void SetRemoteDescription(SetSessionDescriptionObserver* observer, | |
| 132 SessionDescriptionInterface* desc) override; | |
| 133 bool SetConfiguration( | |
| 134 const PeerConnectionInterface::RTCConfiguration& config) override; | |
| 135 bool AddIceCandidate(const IceCandidateInterface* candidate) override; | |
| 136 | |
| 137 void RegisterUMAObserver(UMAObserver* observer) override; | |
| 138 | |
| 139 void Close() override; | |
| 140 | |
| 141 // Virtual for unit tests. | |
| 142 virtual const std::vector<rtc::scoped_refptr<DataChannel>>& | |
| 143 sctp_data_channels() const { | |
| 144 return sctp_data_channels_; | |
| 145 }; | |
| 146 | |
| 147 protected: | |
| 148 ~PeerConnection() override; | |
| 149 | |
| 150 private: | |
| 151 struct TrackInfo { | |
| 152 TrackInfo() : ssrc(0) {} | |
| 153 TrackInfo(const std::string& stream_label, | |
| 154 const std::string track_id, | |
| 155 uint32_t ssrc) | |
| 156 : stream_label(stream_label), track_id(track_id), ssrc(ssrc) {} | |
| 157 bool operator==(const TrackInfo& other) { | |
| 158 return this->stream_label == other.stream_label && | |
| 159 this->track_id == other.track_id && this->ssrc == other.ssrc; | |
| 160 } | |
| 161 std::string stream_label; | |
| 162 std::string track_id; | |
| 163 uint32_t ssrc; | |
| 164 }; | |
| 165 typedef std::vector<TrackInfo> TrackInfos; | |
| 166 | |
| 167 // Implements MessageHandler. | |
| 168 void OnMessage(rtc::Message* msg) override; | |
| 169 | |
| 170 void CreateAudioReceiver(MediaStreamInterface* stream, | |
| 171 AudioTrackInterface* audio_track, | |
| 172 uint32_t ssrc); | |
| 173 void CreateVideoReceiver(MediaStreamInterface* stream, | |
| 174 VideoTrackInterface* video_track, | |
| 175 uint32_t ssrc); | |
| 176 void DestroyAudioReceiver(MediaStreamInterface* stream, | |
| 177 AudioTrackInterface* audio_track); | |
| 178 void DestroyVideoReceiver(MediaStreamInterface* stream, | |
| 179 VideoTrackInterface* video_track); | |
| 180 void DestroyAudioSender(MediaStreamInterface* stream, | |
| 181 AudioTrackInterface* audio_track, | |
| 182 uint32_t ssrc); | |
| 183 void DestroyVideoSender(MediaStreamInterface* stream, | |
| 184 VideoTrackInterface* video_track); | |
| 185 | |
| 186 // Implements IceObserver | |
| 187 void OnIceConnectionChange(IceConnectionState new_state) override; | |
| 188 void OnIceGatheringChange(IceGatheringState new_state) override; | |
| 189 void OnIceCandidate(const IceCandidateInterface* candidate) override; | |
| 190 void OnIceConnectionReceivingChange(bool receiving) override; | |
| 191 | |
| 192 // Signals from WebRtcSession. | |
| 193 void OnSessionStateChange(WebRtcSession* session, WebRtcSession::State state); | |
| 194 void ChangeSignalingState(SignalingState signaling_state); | |
| 195 | |
| 196 // Signals from MediaStreamObserver. | |
| 197 void OnAudioTrackAdded(AudioTrackInterface* track, | |
| 198 MediaStreamInterface* stream); | |
| 199 void OnAudioTrackRemoved(AudioTrackInterface* track, | |
| 200 MediaStreamInterface* stream); | |
| 201 void OnVideoTrackAdded(VideoTrackInterface* track, | |
| 202 MediaStreamInterface* stream); | |
| 203 void OnVideoTrackRemoved(VideoTrackInterface* track, | |
| 204 MediaStreamInterface* stream); | |
| 205 | |
| 206 rtc::Thread* signaling_thread() const { | |
| 207 return factory_->signaling_thread(); | |
| 208 } | |
| 209 | |
| 210 void PostSetSessionDescriptionFailure(SetSessionDescriptionObserver* observer, | |
| 211 const std::string& error); | |
| 212 void PostCreateSessionDescriptionFailure( | |
| 213 CreateSessionDescriptionObserver* observer, | |
| 214 const std::string& error); | |
| 215 | |
| 216 bool IsClosed() const { | |
| 217 return signaling_state_ == PeerConnectionInterface::kClosed; | |
| 218 } | |
| 219 | |
| 220 // Returns a MediaSessionOptions struct with options decided by |options|, | |
| 221 // the local MediaStreams and DataChannels. | |
| 222 virtual bool GetOptionsForOffer( | |
| 223 const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options, | |
| 224 cricket::MediaSessionOptions* session_options); | |
| 225 | |
| 226 // Returns a MediaSessionOptions struct with options decided by | |
| 227 // |constraints|, the local MediaStreams and DataChannels. | |
| 228 virtual bool GetOptionsForAnswer( | |
| 229 const MediaConstraintsInterface* constraints, | |
| 230 cricket::MediaSessionOptions* session_options); | |
| 231 | |
| 232 // Remove all local and remote tracks of type |media_type|. | |
| 233 // Called when a media type is rejected (m-line set to port 0). | |
| 234 void RemoveTracks(cricket::MediaType media_type); | |
| 235 | |
| 236 // Makes sure a MediaStreamTrack is created for each StreamParam in |streams|, | |
| 237 // and existing MediaStreamTracks are removed if there is no corresponding | |
| 238 // StreamParam. If |default_track_needed| is true, a default MediaStreamTrack | |
| 239 // is created if it doesn't exist; if false, it's removed if it exists. | |
| 240 // |media_type| is the type of the |streams| and can be either audio or video. | |
| 241 // If a new MediaStream is created it is added to |new_streams|. | |
| 242 void UpdateRemoteStreamsList( | |
| 243 const std::vector<cricket::StreamParams>& streams, | |
| 244 bool default_track_needed, | |
| 245 cricket::MediaType media_type, | |
| 246 StreamCollection* new_streams); | |
| 247 | |
| 248 // Triggered when a remote track has been seen for the first time in a remote | |
| 249 // session description. It creates a remote MediaStreamTrackInterface | |
| 250 // implementation and triggers CreateAudioReceiver or CreateVideoReceiver. | |
| 251 void OnRemoteTrackSeen(const std::string& stream_label, | |
| 252 const std::string& track_id, | |
| 253 uint32_t ssrc, | |
| 254 cricket::MediaType media_type); | |
| 255 | |
| 256 // Triggered when a remote track has been removed from a remote session | |
| 257 // description. It removes the remote track with id |track_id| from a remote | |
| 258 // MediaStream and triggers DestroyAudioReceiver or DestroyVideoReceiver. | |
| 259 void OnRemoteTrackRemoved(const std::string& stream_label, | |
| 260 const std::string& track_id, | |
| 261 cricket::MediaType media_type); | |
| 262 | |
| 263 // Finds remote MediaStreams without any tracks and removes them from | |
| 264 // |remote_streams_| and notifies the observer that the MediaStreams no longer | |
| 265 // exist. | |
| 266 void UpdateEndedRemoteMediaStreams(); | |
| 267 | |
| 268 // Set the MediaStreamTrackInterface::TrackState to |kEnded| on all remote | |
| 269 // tracks of type |media_type|. | |
| 270 void EndRemoteTracks(cricket::MediaType media_type); | |
| 271 | |
| 272 // Loops through the vector of |streams| and finds added and removed | |
| 273 // StreamParams since last time this method was called. | |
| 274 // For each new or removed StreamParam, OnLocalTrackSeen or | |
| 275 // OnLocalTrackRemoved is invoked. | |
| 276 void UpdateLocalTracks(const std::vector<cricket::StreamParams>& streams, | |
| 277 cricket::MediaType media_type); | |
| 278 | |
| 279 // Triggered when a local track has been seen for the first time in a local | |
| 280 // session description. | |
| 281 // This method triggers CreateAudioSender or CreateVideoSender if the rtp | |
| 282 // streams in the local SessionDescription can be mapped to a MediaStreamTrack | |
| 283 // in a MediaStream in |local_streams_| | |
| 284 void OnLocalTrackSeen(const std::string& stream_label, | |
| 285 const std::string& track_id, | |
| 286 uint32_t ssrc, | |
| 287 cricket::MediaType media_type); | |
| 288 | |
| 289 // Triggered when a local track has been removed from a local session | |
| 290 // description. | |
| 291 // This method triggers DestroyAudioSender or DestroyVideoSender if a stream | |
| 292 // has been removed from the local SessionDescription and the stream can be | |
| 293 // mapped to a MediaStreamTrack in a MediaStream in |local_streams_|. | |
| 294 void OnLocalTrackRemoved(const std::string& stream_label, | |
| 295 const std::string& track_id, | |
| 296 uint32_t ssrc, | |
| 297 cricket::MediaType media_type); | |
| 298 | |
| 299 void UpdateLocalRtpDataChannels(const cricket::StreamParamsVec& streams); | |
| 300 void UpdateRemoteRtpDataChannels(const cricket::StreamParamsVec& streams); | |
| 301 void UpdateClosingRtpDataChannels( | |
| 302 const std::vector<std::string>& active_channels, | |
| 303 bool is_local_update); | |
| 304 void CreateRemoteRtpDataChannel(const std::string& label, | |
| 305 uint32_t remote_ssrc); | |
| 306 | |
| 307 // Creates channel and adds it to the collection of DataChannels that will | |
| 308 // be offered in a SessionDescription. | |
| 309 rtc::scoped_refptr<DataChannel> InternalCreateDataChannel( | |
| 310 const std::string& label, | |
| 311 const InternalDataChannelInit* config); | |
| 312 | |
| 313 // Checks if any data channel has been added. | |
| 314 bool HasDataChannels() const; | |
| 315 | |
| 316 void AllocateSctpSids(rtc::SSLRole role); | |
| 317 void OnSctpDataChannelClosed(DataChannel* channel); | |
| 318 | |
| 319 // Notifications from WebRtcSession relating to BaseChannels. | |
| 320 void OnVoiceChannelDestroyed(); | |
| 321 void OnVideoChannelDestroyed(); | |
| 322 void OnDataChannelCreated(); | |
| 323 void OnDataChannelDestroyed(); | |
| 324 // Called when the cricket::DataChannel receives a message indicating that a | |
| 325 // webrtc::DataChannel should be opened. | |
| 326 void OnDataChannelOpenMessage(const std::string& label, | |
| 327 const InternalDataChannelInit& config); | |
| 328 | |
| 329 RtpSenderInterface* FindSenderById(const std::string& id); | |
| 330 | |
| 331 std::vector<rtc::scoped_refptr<RtpSenderInterface>>::iterator | |
| 332 FindSenderForTrack(MediaStreamTrackInterface* track); | |
| 333 std::vector<rtc::scoped_refptr<RtpReceiverInterface>>::iterator | |
| 334 FindReceiverForTrack(MediaStreamTrackInterface* track); | |
| 335 | |
| 336 TrackInfos* GetRemoteTracks(cricket::MediaType media_type); | |
| 337 TrackInfos* GetLocalTracks(cricket::MediaType media_type); | |
| 338 const TrackInfo* FindTrackInfo(const TrackInfos& infos, | |
| 339 const std::string& stream_label, | |
| 340 const std::string track_id) const; | |
| 341 | |
| 342 // Returns the specified SCTP DataChannel in sctp_data_channels_, | |
| 343 // or nullptr if not found. | |
| 344 DataChannel* FindDataChannelBySid(int sid) const; | |
| 345 | |
| 346 // Storing the factory as a scoped reference pointer ensures that the memory | |
| 347 // in the PeerConnectionFactoryImpl remains available as long as the | |
| 348 // PeerConnection is running. It is passed to PeerConnection as a raw pointer. | |
| 349 // However, since the reference counting is done in the | |
| 350 // PeerConnectionFactoryInterface all instances created using the raw pointer | |
| 351 // will refer to the same reference count. | |
| 352 rtc::scoped_refptr<PeerConnectionFactory> factory_; | |
| 353 PeerConnectionObserver* observer_; | |
| 354 UMAObserver* uma_observer_; | |
| 355 SignalingState signaling_state_; | |
| 356 // TODO(bemasc): Remove ice_state_. | |
| 357 IceState ice_state_; | |
| 358 IceConnectionState ice_connection_state_; | |
| 359 IceGatheringState ice_gathering_state_; | |
| 360 | |
| 361 rtc::scoped_ptr<cricket::PortAllocator> port_allocator_; | |
| 362 rtc::scoped_ptr<MediaControllerInterface> media_controller_; | |
| 363 | |
| 364 // Streams added via AddStream. | |
| 365 rtc::scoped_refptr<StreamCollection> local_streams_; | |
| 366 // Streams created as a result of SetRemoteDescription. | |
| 367 rtc::scoped_refptr<StreamCollection> remote_streams_; | |
| 368 | |
| 369 std::vector<rtc::scoped_ptr<MediaStreamObserver>> stream_observers_; | |
| 370 | |
| 371 // These lists store track info seen in local/remote descriptions. | |
| 372 TrackInfos remote_audio_tracks_; | |
| 373 TrackInfos remote_video_tracks_; | |
| 374 TrackInfos local_audio_tracks_; | |
| 375 TrackInfos local_video_tracks_; | |
| 376 | |
| 377 SctpSidAllocator sid_allocator_; | |
| 378 // label -> DataChannel | |
| 379 std::map<std::string, rtc::scoped_refptr<DataChannel>> rtp_data_channels_; | |
| 380 std::vector<rtc::scoped_refptr<DataChannel>> sctp_data_channels_; | |
| 381 std::vector<rtc::scoped_refptr<DataChannel>> sctp_data_channels_to_free_; | |
| 382 | |
| 383 bool remote_peer_supports_msid_ = false; | |
| 384 rtc::scoped_ptr<RemoteMediaStreamFactory> remote_stream_factory_; | |
| 385 | |
| 386 std::vector<rtc::scoped_refptr<RtpSenderInterface>> senders_; | |
| 387 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> receivers_; | |
| 388 | |
| 389 // The session_ scoped_ptr is declared at the bottom of PeerConnection | |
| 390 // because its destruction fires signals (such as VoiceChannelDestroyed) | |
| 391 // which will trigger some final actions in PeerConnection... | |
| 392 rtc::scoped_ptr<WebRtcSession> session_; | |
| 393 // ... But stats_ depends on session_ so it should be destroyed even earlier. | |
| 394 rtc::scoped_ptr<StatsCollector> stats_; | |
| 395 }; | |
| 396 | |
| 397 } // namespace webrtc | |
| 398 | |
| 399 #endif // TALK_APP_WEBRTC_PEERCONNECTION_H_ | |
| OLD | NEW |