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 
session()86   virtual WebRtcSession* session() { return session_.get(); }
87 
88   rtc::scoped_refptr<DtmfSenderInterface> CreateDtmfSender(
89       AudioTrackInterface* track) override;
90 
91   rtc::scoped_refptr<RtpSenderInterface> CreateSender(
92       const std::string& kind,
93       const std::string& stream_id) override;
94 
95   std::vector<rtc::scoped_refptr<RtpSenderInterface>> GetSenders()
96       const override;
97   std::vector<rtc::scoped_refptr<RtpReceiverInterface>> GetReceivers()
98       const override;
99 
100   rtc::scoped_refptr<DataChannelInterface> CreateDataChannel(
101       const std::string& label,
102       const DataChannelInit* config) override;
103   bool GetStats(StatsObserver* observer,
104                 webrtc::MediaStreamTrackInterface* track,
105                 StatsOutputLevel level) override;
106 
107   SignalingState signaling_state() override;
108 
109   // TODO(bemasc): Remove ice_state() when callers are removed.
110   IceState ice_state() override;
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   void CreateOffer(CreateSessionDescriptionObserver* observer,
119                    const MediaConstraintsInterface* constraints) override;
120   void CreateOffer(CreateSessionDescriptionObserver* observer,
121                    const RTCOfferAnswerOptions& options) override;
122   void CreateAnswer(CreateSessionDescriptionObserver* observer,
123                     const MediaConstraintsInterface* constraints) override;
124   void SetLocalDescription(SetSessionDescriptionObserver* observer,
125                            SessionDescriptionInterface* desc) override;
126   void SetRemoteDescription(SetSessionDescriptionObserver* observer,
127                             SessionDescriptionInterface* desc) override;
128   bool SetConfiguration(
129       const PeerConnectionInterface::RTCConfiguration& config) override;
130   bool AddIceCandidate(const IceCandidateInterface* candidate) override;
131 
132   void RegisterUMAObserver(UMAObserver* observer) override;
133 
134   void Close() override;
135 
136   // Virtual for unit tests.
137   virtual const std::vector<rtc::scoped_refptr<DataChannel>>&
sctp_data_channels()138   sctp_data_channels() const {
139     return sctp_data_channels_;
140   };
141 
142  protected:
143   ~PeerConnection() override;
144 
145  private:
146   struct TrackInfo {
TrackInfoTrackInfo147     TrackInfo() : ssrc(0) {}
TrackInfoTrackInfo148     TrackInfo(const std::string& stream_label,
149               const std::string track_id,
150               uint32_t ssrc)
151         : stream_label(stream_label), track_id(track_id), ssrc(ssrc) {}
152     bool operator==(const TrackInfo& other) {
153       return this->stream_label == other.stream_label &&
154              this->track_id == other.track_id && this->ssrc == other.ssrc;
155     }
156     std::string stream_label;
157     std::string track_id;
158     uint32_t ssrc;
159   };
160   typedef std::vector<TrackInfo> TrackInfos;
161 
162   // Implements MessageHandler.
163   void OnMessage(rtc::Message* msg) override;
164 
165   void CreateAudioReceiver(MediaStreamInterface* stream,
166                            AudioTrackInterface* audio_track,
167                            uint32_t ssrc);
168   void CreateVideoReceiver(MediaStreamInterface* stream,
169                            VideoTrackInterface* video_track,
170                            uint32_t ssrc);
171   void DestroyAudioReceiver(MediaStreamInterface* stream,
172                             AudioTrackInterface* audio_track);
173   void DestroyVideoReceiver(MediaStreamInterface* stream,
174                             VideoTrackInterface* video_track);
175   void DestroyAudioSender(MediaStreamInterface* stream,
176                           AudioTrackInterface* audio_track,
177                           uint32_t ssrc);
178   void DestroyVideoSender(MediaStreamInterface* stream,
179                           VideoTrackInterface* video_track);
180 
181   // Implements IceObserver
182   void OnIceConnectionChange(IceConnectionState new_state) override;
183   void OnIceGatheringChange(IceGatheringState new_state) override;
184   void OnIceCandidate(const IceCandidateInterface* candidate) override;
185   void OnIceComplete() override;
186   void OnIceConnectionReceivingChange(bool receiving) override;
187 
188   // Signals from WebRtcSession.
189   void OnSessionStateChange(WebRtcSession* session, WebRtcSession::State state);
190   void ChangeSignalingState(SignalingState signaling_state);
191 
192   // Signals from MediaStreamObserver.
193   void OnAudioTrackAdded(AudioTrackInterface* track,
194                          MediaStreamInterface* stream);
195   void OnAudioTrackRemoved(AudioTrackInterface* track,
196                            MediaStreamInterface* stream);
197   void OnVideoTrackAdded(VideoTrackInterface* track,
198                          MediaStreamInterface* stream);
199   void OnVideoTrackRemoved(VideoTrackInterface* track,
200                            MediaStreamInterface* stream);
201 
signaling_thread()202   rtc::Thread* signaling_thread() const {
203     return factory_->signaling_thread();
204   }
205 
206   void PostSetSessionDescriptionFailure(SetSessionDescriptionObserver* observer,
207                                         const std::string& error);
208   void PostCreateSessionDescriptionFailure(
209       CreateSessionDescriptionObserver* observer,
210       const std::string& error);
211 
IsClosed()212   bool IsClosed() const {
213     return signaling_state_ == PeerConnectionInterface::kClosed;
214   }
215 
216   // Returns a MediaSessionOptions struct with options decided by |options|,
217   // the local MediaStreams and DataChannels.
218   virtual bool GetOptionsForOffer(
219       const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options,
220       cricket::MediaSessionOptions* session_options);
221 
222   // Returns a MediaSessionOptions struct with options decided by
223   // |constraints|, the local MediaStreams and DataChannels.
224   virtual bool GetOptionsForAnswer(
225       const MediaConstraintsInterface* constraints,
226       cricket::MediaSessionOptions* session_options);
227 
228   // Remove all local and remote tracks of type |media_type|.
229   // Called when a media type is rejected (m-line set to port 0).
230   void RemoveTracks(cricket::MediaType media_type);
231 
232   // Makes sure a MediaStreamTrack is created for each StreamParam in |streams|,
233   // and existing MediaStreamTracks are removed if there is no corresponding
234   // StreamParam. If |default_track_needed| is true, a default MediaStreamTrack
235   // is created if it doesn't exist; if false, it's removed if it exists.
236   // |media_type| is the type of the |streams| and can be either 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       bool default_track_needed,
241       cricket::MediaType media_type,
242       StreamCollection* new_streams);
243 
244   // Triggered when a remote track has been seen for the first time in a remote
245   // session description. It creates a remote MediaStreamTrackInterface
246   // implementation and triggers CreateAudioReceiver or CreateVideoReceiver.
247   void OnRemoteTrackSeen(const std::string& stream_label,
248                          const std::string& track_id,
249                          uint32_t ssrc,
250                          cricket::MediaType media_type);
251 
252   // Triggered when a remote track has been removed from a remote session
253   // description. It removes the remote track with id |track_id| from a remote
254   // MediaStream and triggers DestroyAudioReceiver or DestroyVideoReceiver.
255   void OnRemoteTrackRemoved(const std::string& stream_label,
256                             const std::string& track_id,
257                             cricket::MediaType media_type);
258 
259   // Finds remote MediaStreams without any tracks and removes them from
260   // |remote_streams_| and notifies the observer that the MediaStreams no longer
261   // exist.
262   void UpdateEndedRemoteMediaStreams();
263 
264   // Set the MediaStreamTrackInterface::TrackState to |kEnded| on all remote
265   // tracks of type |media_type|.
266   void EndRemoteTracks(cricket::MediaType media_type);
267 
268   // Loops through the vector of |streams| and finds added and removed
269   // StreamParams since last time this method was called.
270   // For each new or removed StreamParam, OnLocalTrackSeen or
271   // OnLocalTrackRemoved is invoked.
272   void UpdateLocalTracks(const std::vector<cricket::StreamParams>& streams,
273                          cricket::MediaType media_type);
274 
275   // Triggered when a local track has been seen for the first time in a local
276   // session description.
277   // This method triggers CreateAudioSender or CreateVideoSender if the rtp
278   // streams in the local SessionDescription can be mapped to a MediaStreamTrack
279   // in a MediaStream in |local_streams_|
280   void OnLocalTrackSeen(const std::string& stream_label,
281                         const std::string& track_id,
282                         uint32_t ssrc,
283                         cricket::MediaType media_type);
284 
285   // Triggered when a local track has been removed from a local session
286   // description.
287   // This method triggers DestroyAudioSender or DestroyVideoSender if a stream
288   // has been removed from the local SessionDescription and the stream can be
289   // mapped to a MediaStreamTrack in a MediaStream in |local_streams_|.
290   void OnLocalTrackRemoved(const std::string& stream_label,
291                            const std::string& track_id,
292                            uint32_t ssrc,
293                            cricket::MediaType media_type);
294 
295   void UpdateLocalRtpDataChannels(const cricket::StreamParamsVec& streams);
296   void UpdateRemoteRtpDataChannels(const cricket::StreamParamsVec& streams);
297   void UpdateClosingRtpDataChannels(
298       const std::vector<std::string>& active_channels,
299       bool is_local_update);
300   void CreateRemoteRtpDataChannel(const std::string& label,
301                                   uint32_t 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 receives a message indicating that a
321   // webrtc::DataChannel should be opened.
322   void OnDataChannelOpenMessage(const std::string& label,
323                                 const InternalDataChannelInit& config);
324 
325   RtpSenderInterface* FindSenderById(const std::string& id);
326 
327   std::vector<rtc::scoped_refptr<RtpSenderInterface>>::iterator
328   FindSenderForTrack(MediaStreamTrackInterface* track);
329   std::vector<rtc::scoped_refptr<RtpReceiverInterface>>::iterator
330   FindReceiverForTrack(MediaStreamTrackInterface* track);
331 
332   TrackInfos* GetRemoteTracks(cricket::MediaType media_type);
333   TrackInfos* GetLocalTracks(cricket::MediaType media_type);
334   const TrackInfo* FindTrackInfo(const TrackInfos& infos,
335                                  const std::string& stream_label,
336                                  const std::string track_id) const;
337 
338   // Returns the specified SCTP DataChannel in sctp_data_channels_,
339   // or nullptr if not found.
340   DataChannel* FindDataChannelBySid(int sid) const;
341 
342   // Storing the factory as a scoped reference pointer ensures that the memory
343   // in the PeerConnectionFactoryImpl remains available as long as the
344   // PeerConnection is running. It is passed to PeerConnection as a raw pointer.
345   // However, since the reference counting is done in the
346   // PeerConnectionFactoryInterface all instances created using the raw pointer
347   // will refer to the same reference count.
348   rtc::scoped_refptr<PeerConnectionFactory> factory_;
349   PeerConnectionObserver* observer_;
350   UMAObserver* uma_observer_;
351   SignalingState signaling_state_;
352   // TODO(bemasc): Remove ice_state_.
353   IceState ice_state_;
354   IceConnectionState ice_connection_state_;
355   IceGatheringState ice_gathering_state_;
356 
357   rtc::scoped_ptr<cricket::PortAllocator> port_allocator_;
358   rtc::scoped_ptr<MediaControllerInterface> media_controller_;
359 
360   // Streams added via AddStream.
361   rtc::scoped_refptr<StreamCollection> local_streams_;
362   // Streams created as a result of SetRemoteDescription.
363   rtc::scoped_refptr<StreamCollection> remote_streams_;
364 
365   std::vector<rtc::scoped_ptr<MediaStreamObserver>> stream_observers_;
366 
367   // These lists store track info seen in local/remote descriptions.
368   TrackInfos remote_audio_tracks_;
369   TrackInfos remote_video_tracks_;
370   TrackInfos local_audio_tracks_;
371   TrackInfos local_video_tracks_;
372 
373   SctpSidAllocator sid_allocator_;
374   // label -> DataChannel
375   std::map<std::string, rtc::scoped_refptr<DataChannel>> rtp_data_channels_;
376   std::vector<rtc::scoped_refptr<DataChannel>> sctp_data_channels_;
377   std::vector<rtc::scoped_refptr<DataChannel>> sctp_data_channels_to_free_;
378 
379   bool remote_peer_supports_msid_ = false;
380   rtc::scoped_ptr<RemoteMediaStreamFactory> remote_stream_factory_;
381 
382   std::vector<rtc::scoped_refptr<RtpSenderInterface>> senders_;
383   std::vector<rtc::scoped_refptr<RtpReceiverInterface>> receivers_;
384 
385   // The session_ scoped_ptr is declared at the bottom of PeerConnection
386   // because its destruction fires signals (such as VoiceChannelDestroyed)
387   // which will trigger some final actions in PeerConnection...
388   rtc::scoped_ptr<WebRtcSession> session_;
389   // ... But stats_ depends on session_ so it should be destroyed even earlier.
390   rtc::scoped_ptr<StatsCollector> stats_;
391 };
392 
393 }  // namespace webrtc
394 
395 #endif  // TALK_APP_WEBRTC_PEERCONNECTION_H_
396