Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(42)

Side by Side Diff: talk/app/webrtc/peerconnection.h

Issue 1610243002: Move talk/app/webrtc to webrtc/api (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Updated location for peerconnection_unittests.isolate Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 OnIceComplete() override;
191 void OnIceConnectionReceivingChange(bool receiving) override;
192
193 // Signals from WebRtcSession.
194 void OnSessionStateChange(WebRtcSession* session, WebRtcSession::State state);
195 void ChangeSignalingState(SignalingState signaling_state);
196
197 // Signals from MediaStreamObserver.
198 void OnAudioTrackAdded(AudioTrackInterface* track,
199 MediaStreamInterface* stream);
200 void OnAudioTrackRemoved(AudioTrackInterface* track,
201 MediaStreamInterface* stream);
202 void OnVideoTrackAdded(VideoTrackInterface* track,
203 MediaStreamInterface* stream);
204 void OnVideoTrackRemoved(VideoTrackInterface* track,
205 MediaStreamInterface* stream);
206
207 rtc::Thread* signaling_thread() const {
208 return factory_->signaling_thread();
209 }
210
211 void PostSetSessionDescriptionFailure(SetSessionDescriptionObserver* observer,
212 const std::string& error);
213 void PostCreateSessionDescriptionFailure(
214 CreateSessionDescriptionObserver* observer,
215 const std::string& error);
216
217 bool IsClosed() const {
218 return signaling_state_ == PeerConnectionInterface::kClosed;
219 }
220
221 // Returns a MediaSessionOptions struct with options decided by |options|,
222 // the local MediaStreams and DataChannels.
223 virtual bool GetOptionsForOffer(
224 const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options,
225 cricket::MediaSessionOptions* session_options);
226
227 // Returns a MediaSessionOptions struct with options decided by
228 // |constraints|, the local MediaStreams and DataChannels.
229 virtual bool GetOptionsForAnswer(
230 const MediaConstraintsInterface* constraints,
231 cricket::MediaSessionOptions* session_options);
232
233 // Remove all local and remote tracks of type |media_type|.
234 // Called when a media type is rejected (m-line set to port 0).
235 void RemoveTracks(cricket::MediaType media_type);
236
237 // Makes sure a MediaStreamTrack is created for each StreamParam in |streams|,
238 // and existing MediaStreamTracks are removed if there is no corresponding
239 // StreamParam. If |default_track_needed| is true, a default MediaStreamTrack
240 // is created if it doesn't exist; if false, it's removed if it exists.
241 // |media_type| is the type of the |streams| and can be either audio or video.
242 // If a new MediaStream is created it is added to |new_streams|.
243 void UpdateRemoteStreamsList(
244 const std::vector<cricket::StreamParams>& streams,
245 bool default_track_needed,
246 cricket::MediaType media_type,
247 StreamCollection* new_streams);
248
249 // Triggered when a remote track has been seen for the first time in a remote
250 // session description. It creates a remote MediaStreamTrackInterface
251 // implementation and triggers CreateAudioReceiver or CreateVideoReceiver.
252 void OnRemoteTrackSeen(const std::string& stream_label,
253 const std::string& track_id,
254 uint32_t ssrc,
255 cricket::MediaType media_type);
256
257 // Triggered when a remote track has been removed from a remote session
258 // description. It removes the remote track with id |track_id| from a remote
259 // MediaStream and triggers DestroyAudioReceiver or DestroyVideoReceiver.
260 void OnRemoteTrackRemoved(const std::string& stream_label,
261 const std::string& track_id,
262 cricket::MediaType media_type);
263
264 // Finds remote MediaStreams without any tracks and removes them from
265 // |remote_streams_| and notifies the observer that the MediaStreams no longer
266 // exist.
267 void UpdateEndedRemoteMediaStreams();
268
269 // Set the MediaStreamTrackInterface::TrackState to |kEnded| on all remote
270 // tracks of type |media_type|.
271 void EndRemoteTracks(cricket::MediaType media_type);
272
273 // Loops through the vector of |streams| and finds added and removed
274 // StreamParams since last time this method was called.
275 // For each new or removed StreamParam, OnLocalTrackSeen or
276 // OnLocalTrackRemoved is invoked.
277 void UpdateLocalTracks(const std::vector<cricket::StreamParams>& streams,
278 cricket::MediaType media_type);
279
280 // Triggered when a local track has been seen for the first time in a local
281 // session description.
282 // This method triggers CreateAudioSender or CreateVideoSender if the rtp
283 // streams in the local SessionDescription can be mapped to a MediaStreamTrack
284 // in a MediaStream in |local_streams_|
285 void OnLocalTrackSeen(const std::string& stream_label,
286 const std::string& track_id,
287 uint32_t ssrc,
288 cricket::MediaType media_type);
289
290 // Triggered when a local track has been removed from a local session
291 // description.
292 // This method triggers DestroyAudioSender or DestroyVideoSender if a stream
293 // has been removed from the local SessionDescription and the stream can be
294 // mapped to a MediaStreamTrack in a MediaStream in |local_streams_|.
295 void OnLocalTrackRemoved(const std::string& stream_label,
296 const std::string& track_id,
297 uint32_t ssrc,
298 cricket::MediaType media_type);
299
300 void UpdateLocalRtpDataChannels(const cricket::StreamParamsVec& streams);
301 void UpdateRemoteRtpDataChannels(const cricket::StreamParamsVec& streams);
302 void UpdateClosingRtpDataChannels(
303 const std::vector<std::string>& active_channels,
304 bool is_local_update);
305 void CreateRemoteRtpDataChannel(const std::string& label,
306 uint32_t remote_ssrc);
307
308 // Creates channel and adds it to the collection of DataChannels that will
309 // be offered in a SessionDescription.
310 rtc::scoped_refptr<DataChannel> InternalCreateDataChannel(
311 const std::string& label,
312 const InternalDataChannelInit* config);
313
314 // Checks if any data channel has been added.
315 bool HasDataChannels() const;
316
317 void AllocateSctpSids(rtc::SSLRole role);
318 void OnSctpDataChannelClosed(DataChannel* channel);
319
320 // Notifications from WebRtcSession relating to BaseChannels.
321 void OnVoiceChannelDestroyed();
322 void OnVideoChannelDestroyed();
323 void OnDataChannelCreated();
324 void OnDataChannelDestroyed();
325 // Called when the cricket::DataChannel receives a message indicating that a
326 // webrtc::DataChannel should be opened.
327 void OnDataChannelOpenMessage(const std::string& label,
328 const InternalDataChannelInit& config);
329
330 RtpSenderInterface* FindSenderById(const std::string& id);
331
332 std::vector<rtc::scoped_refptr<RtpSenderInterface>>::iterator
333 FindSenderForTrack(MediaStreamTrackInterface* track);
334 std::vector<rtc::scoped_refptr<RtpReceiverInterface>>::iterator
335 FindReceiverForTrack(MediaStreamTrackInterface* track);
336
337 TrackInfos* GetRemoteTracks(cricket::MediaType media_type);
338 TrackInfos* GetLocalTracks(cricket::MediaType media_type);
339 const TrackInfo* FindTrackInfo(const TrackInfos& infos,
340 const std::string& stream_label,
341 const std::string track_id) const;
342
343 // Returns the specified SCTP DataChannel in sctp_data_channels_,
344 // or nullptr if not found.
345 DataChannel* FindDataChannelBySid(int sid) const;
346
347 // Storing the factory as a scoped reference pointer ensures that the memory
348 // in the PeerConnectionFactoryImpl remains available as long as the
349 // PeerConnection is running. It is passed to PeerConnection as a raw pointer.
350 // However, since the reference counting is done in the
351 // PeerConnectionFactoryInterface all instances created using the raw pointer
352 // will refer to the same reference count.
353 rtc::scoped_refptr<PeerConnectionFactory> factory_;
354 PeerConnectionObserver* observer_;
355 UMAObserver* uma_observer_;
356 SignalingState signaling_state_;
357 // TODO(bemasc): Remove ice_state_.
358 IceState ice_state_;
359 IceConnectionState ice_connection_state_;
360 IceGatheringState ice_gathering_state_;
361
362 rtc::scoped_ptr<cricket::PortAllocator> port_allocator_;
363 rtc::scoped_ptr<MediaControllerInterface> media_controller_;
364
365 // Streams added via AddStream.
366 rtc::scoped_refptr<StreamCollection> local_streams_;
367 // Streams created as a result of SetRemoteDescription.
368 rtc::scoped_refptr<StreamCollection> remote_streams_;
369
370 std::vector<rtc::scoped_ptr<MediaStreamObserver>> stream_observers_;
371
372 // These lists store track info seen in local/remote descriptions.
373 TrackInfos remote_audio_tracks_;
374 TrackInfos remote_video_tracks_;
375 TrackInfos local_audio_tracks_;
376 TrackInfos local_video_tracks_;
377
378 SctpSidAllocator sid_allocator_;
379 // label -> DataChannel
380 std::map<std::string, rtc::scoped_refptr<DataChannel>> rtp_data_channels_;
381 std::vector<rtc::scoped_refptr<DataChannel>> sctp_data_channels_;
382 std::vector<rtc::scoped_refptr<DataChannel>> sctp_data_channels_to_free_;
383
384 bool remote_peer_supports_msid_ = false;
385 rtc::scoped_ptr<RemoteMediaStreamFactory> remote_stream_factory_;
386
387 std::vector<rtc::scoped_refptr<RtpSenderInterface>> senders_;
388 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> receivers_;
389
390 // The session_ scoped_ptr is declared at the bottom of PeerConnection
391 // because its destruction fires signals (such as VoiceChannelDestroyed)
392 // which will trigger some final actions in PeerConnection...
393 rtc::scoped_ptr<WebRtcSession> session_;
394 // ... But stats_ depends on session_ so it should be destroyed even earlier.
395 rtc::scoped_ptr<StatsCollector> stats_;
396 };
397
398 } // namespace webrtc
399
400 #endif // TALK_APP_WEBRTC_PEERCONNECTION_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698