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

Side by Side Diff: webrtc/api/webrtcsession.h

Issue 2514883002: Create //webrtc/api:libjingle_peerconnection_api + refactorings. (Closed)
Patch Set: Added three headers for backwards-compatibility, specifically for building chromium. Created 4 years 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 * 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_WEBRTCSESSION_H_
12 #define WEBRTC_API_WEBRTCSESSION_H_
13
14 #include <memory>
15 #include <set>
16 #include <string>
17 #include <vector>
18
19 #include "webrtc/api/datachannel.h"
20 #include "webrtc/api/dtmfsender.h"
21 #include "webrtc/api/mediacontroller.h"
22 #include "webrtc/api/peerconnectioninterface.h"
23 #include "webrtc/api/statstypes.h"
24 #include "webrtc/base/constructormagic.h"
25 #include "webrtc/base/sigslot.h"
26 #include "webrtc/base/sslidentity.h"
27 #include "webrtc/base/thread.h"
28 #include "webrtc/media/base/mediachannel.h"
29 #include "webrtc/p2p/base/candidate.h"
30 #include "webrtc/p2p/base/transportcontroller.h"
31 #include "webrtc/pc/mediasession.h"
32
33 #ifdef HAVE_QUIC
34 #include "webrtc/api/quicdatatransport.h"
35 #endif // HAVE_QUIC
36
37 namespace cricket {
38
39 class ChannelManager;
40 class DataChannel;
41 class StatsReport;
42 class VideoChannel;
43 class VoiceChannel;
44
45 #ifdef HAVE_QUIC
46 class QuicTransportChannel;
47 #endif // HAVE_QUIC
48
49 } // namespace cricket
50
51 namespace webrtc {
52
53 class IceRestartAnswerLatch;
54 class JsepIceCandidate;
55 class MediaStreamSignaling;
56 class WebRtcSessionDescriptionFactory;
57
58 extern const char kBundleWithoutRtcpMux[];
59 extern const char kCreateChannelFailed[];
60 extern const char kInvalidCandidates[];
61 extern const char kInvalidSdp[];
62 extern const char kMlineMismatch[];
63 extern const char kPushDownTDFailed[];
64 extern const char kSdpWithoutDtlsFingerprint[];
65 extern const char kSdpWithoutSdesCrypto[];
66 extern const char kSdpWithoutIceUfragPwd[];
67 extern const char kSdpWithoutSdesAndDtlsDisabled[];
68 extern const char kSessionError[];
69 extern const char kSessionErrorDesc[];
70 extern const char kDtlsSetupFailureRtp[];
71 extern const char kDtlsSetupFailureRtcp[];
72 extern const char kEnableBundleFailed[];
73
74 // Maximum number of received video streams that will be processed by webrtc
75 // even if they are not signalled beforehand.
76 extern const int kMaxUnsignalledRecvStreams;
77
78 // ICE state callback interface.
79 class IceObserver {
80 public:
81 IceObserver() {}
82 // Called any time the IceConnectionState changes
83 // TODO(honghaiz): Change the name to OnIceConnectionStateChange so as to
84 // conform to the w3c standard.
85 virtual void OnIceConnectionChange(
86 PeerConnectionInterface::IceConnectionState new_state) {}
87 // Called any time the IceGatheringState changes
88 virtual void OnIceGatheringChange(
89 PeerConnectionInterface::IceGatheringState new_state) {}
90 // New Ice candidate have been found.
91 virtual void OnIceCandidate(const IceCandidateInterface* candidate) = 0;
92
93 // Some local ICE candidates have been removed.
94 virtual void OnIceCandidatesRemoved(
95 const std::vector<cricket::Candidate>& candidates) = 0;
96
97 // Called whenever the state changes between receiving and not receiving.
98 virtual void OnIceConnectionReceivingChange(bool receiving) {}
99
100 protected:
101 ~IceObserver() {}
102
103 private:
104 RTC_DISALLOW_COPY_AND_ASSIGN(IceObserver);
105 };
106
107 // Statistics for all the transports of the session.
108 typedef std::map<std::string, cricket::TransportStats> TransportStatsMap;
109 typedef std::map<std::string, std::string> ProxyTransportMap;
110
111 // TODO(pthatcher): Think of a better name for this. We already have
112 // a TransportStats in transport.h. Perhaps TransportsStats?
113 struct SessionStats {
114 ProxyTransportMap proxy_to_transport;
115 TransportStatsMap transport_stats;
116 };
117
118 // A WebRtcSession manages general session state. This includes negotiation
119 // of both the application-level and network-level protocols: the former
120 // defines what will be sent and the latter defines how it will be sent. Each
121 // network-level protocol is represented by a Transport object. Each Transport
122 // participates in the network-level negotiation. The individual streams of
123 // packets are represented by TransportChannels. The application-level protocol
124 // is represented by SessionDecription objects.
125 class WebRtcSession :
126
127 public DtmfProviderInterface,
128 public DataChannelProviderInterface,
129 public sigslot::has_slots<> {
130 public:
131 enum State {
132 STATE_INIT = 0,
133 STATE_SENTOFFER, // Sent offer, waiting for answer.
134 STATE_RECEIVEDOFFER, // Received an offer. Need to send answer.
135 STATE_SENTPRANSWER, // Sent provisional answer. Need to send answer.
136 STATE_RECEIVEDPRANSWER, // Received provisional answer, waiting for answer.
137 STATE_INPROGRESS, // Offer/answer exchange completed.
138 STATE_CLOSED, // Close() was called.
139 };
140
141 enum Error {
142 ERROR_NONE = 0, // no error
143 ERROR_CONTENT = 1, // channel errors in SetLocalContent/SetRemoteContent
144 ERROR_TRANSPORT = 2, // transport error of some kind
145 };
146
147 WebRtcSession(
148 webrtc::MediaControllerInterface* media_controller,
149 rtc::Thread* network_thread,
150 rtc::Thread* worker_thread,
151 rtc::Thread* signaling_thread,
152 cricket::PortAllocator* port_allocator,
153 std::unique_ptr<cricket::TransportController> transport_controller);
154 virtual ~WebRtcSession();
155
156 // These are const to allow them to be called from const methods.
157 rtc::Thread* network_thread() const { return network_thread_; }
158 rtc::Thread* worker_thread() const { return worker_thread_; }
159 rtc::Thread* signaling_thread() const { return signaling_thread_; }
160
161 // The ID of this session.
162 const std::string& id() const { return sid_; }
163
164 bool Initialize(
165 const PeerConnectionFactoryInterface::Options& options,
166 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
167 const PeerConnectionInterface::RTCConfiguration& rtc_configuration);
168 // Deletes the voice, video and data channel and changes the session state
169 // to STATE_CLOSED.
170 void Close();
171
172 // Returns true if we were the initial offerer.
173 bool initial_offerer() const { return initial_offerer_; }
174
175 // Returns the current state of the session. See the enum above for details.
176 // Each time the state changes, we will fire this signal.
177 State state() const { return state_; }
178 sigslot::signal2<WebRtcSession*, State> SignalState;
179
180 // Returns the last error in the session. See the enum above for details.
181 Error error() const { return error_; }
182 const std::string& error_desc() const { return error_desc_; }
183
184 void RegisterIceObserver(IceObserver* observer) {
185 ice_observer_ = observer;
186 }
187
188 virtual cricket::VoiceChannel* voice_channel() {
189 return voice_channel_.get();
190 }
191 virtual cricket::VideoChannel* video_channel() {
192 return video_channel_.get();
193 }
194 virtual cricket::DataChannel* data_channel() {
195 return data_channel_.get();
196 }
197
198 cricket::BaseChannel* GetChannel(const std::string& content_name);
199
200 void SetSdesPolicy(cricket::SecurePolicy secure_policy);
201 cricket::SecurePolicy SdesPolicy() const;
202
203 // Get current ssl role from transport.
204 bool GetSslRole(const std::string& transport_name, rtc::SSLRole* role);
205
206 // Get current SSL role for this channel's transport.
207 // If |transport| is null, returns false.
208 bool GetSslRole(const cricket::BaseChannel* channel, rtc::SSLRole* role);
209
210 void CreateOffer(
211 CreateSessionDescriptionObserver* observer,
212 const PeerConnectionInterface::RTCOfferAnswerOptions& options,
213 const cricket::MediaSessionOptions& session_options);
214 void CreateAnswer(CreateSessionDescriptionObserver* observer,
215 const cricket::MediaSessionOptions& session_options);
216 // The ownership of |desc| will be transferred after this call.
217 bool SetLocalDescription(SessionDescriptionInterface* desc,
218 std::string* err_desc);
219 // The ownership of |desc| will be transferred after this call.
220 bool SetRemoteDescription(SessionDescriptionInterface* desc,
221 std::string* err_desc);
222 bool ProcessIceMessage(const IceCandidateInterface* ice_candidate);
223
224 bool RemoveRemoteIceCandidates(
225 const std::vector<cricket::Candidate>& candidates);
226
227 cricket::IceConfig ParseIceConfig(
228 const PeerConnectionInterface::RTCConfiguration& config) const;
229
230 void SetIceConfig(const cricket::IceConfig& ice_config);
231
232 // Start gathering candidates for any new transports, or transports doing an
233 // ICE restart.
234 void MaybeStartGathering();
235
236 const SessionDescriptionInterface* local_description() const {
237 return local_desc_.get();
238 }
239 const SessionDescriptionInterface* remote_description() const {
240 return remote_desc_.get();
241 }
242
243 // Get the id used as a media stream track's "id" field from ssrc.
244 virtual bool GetLocalTrackIdBySsrc(uint32_t ssrc, std::string* track_id);
245 virtual bool GetRemoteTrackIdBySsrc(uint32_t ssrc, std::string* track_id);
246
247 // Implements DtmfProviderInterface.
248 bool CanInsertDtmf(const std::string& track_id) override;
249 bool InsertDtmf(const std::string& track_id,
250 int code, int duration) override;
251 sigslot::signal0<>* GetOnDestroyedSignal() override;
252
253 // Implements DataChannelProviderInterface.
254 bool SendData(const cricket::SendDataParams& params,
255 const rtc::CopyOnWriteBuffer& payload,
256 cricket::SendDataResult* result) override;
257 bool ConnectDataChannel(DataChannel* webrtc_data_channel) override;
258 void DisconnectDataChannel(DataChannel* webrtc_data_channel) override;
259 void AddSctpDataStream(int sid) override;
260 void RemoveSctpDataStream(int sid) override;
261 bool ReadyToSendData() const override;
262
263 // Returns stats for all channels of all transports.
264 // This avoids exposing the internal structures used to track them.
265 virtual bool GetTransportStats(SessionStats* stats);
266
267 // Get stats for a specific channel
268 bool GetChannelTransportStats(cricket::BaseChannel* ch, SessionStats* stats);
269
270 // virtual so it can be mocked in unit tests
271 virtual bool GetLocalCertificate(
272 const std::string& transport_name,
273 rtc::scoped_refptr<rtc::RTCCertificate>* certificate);
274
275 // Caller owns returned certificate
276 virtual std::unique_ptr<rtc::SSLCertificate> GetRemoteSSLCertificate(
277 const std::string& transport_name);
278
279 cricket::DataChannelType data_channel_type() const;
280
281 bool IceRestartPending(const std::string& content_name) const;
282
283 // Called when an RTCCertificate is generated or retrieved by
284 // WebRTCSessionDescriptionFactory. Should happen before setLocalDescription.
285 void OnCertificateReady(
286 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate);
287 void OnDtlsSetupFailure(cricket::BaseChannel*, bool rtcp);
288
289 // For unit test.
290 bool waiting_for_certificate_for_testing() const;
291 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate_for_testing();
292
293 void set_metrics_observer(
294 webrtc::MetricsObserverInterface* metrics_observer) {
295 metrics_observer_ = metrics_observer;
296 transport_controller_->SetMetricsObserver(metrics_observer);
297 }
298
299 // Called when voice_channel_, video_channel_ and data_channel_ are created
300 // and destroyed. As a result of, for example, setting a new description.
301 sigslot::signal0<> SignalVoiceChannelCreated;
302 sigslot::signal0<> SignalVoiceChannelDestroyed;
303 sigslot::signal0<> SignalVideoChannelCreated;
304 sigslot::signal0<> SignalVideoChannelDestroyed;
305 sigslot::signal0<> SignalDataChannelCreated;
306 sigslot::signal0<> SignalDataChannelDestroyed;
307 // Called when the whole session is destroyed.
308 sigslot::signal0<> SignalDestroyed;
309
310 // Called when a valid data channel OPEN message is received.
311 // std::string represents the data channel label.
312 sigslot::signal2<const std::string&, const InternalDataChannelInit&>
313 SignalDataChannelOpenMessage;
314 #ifdef HAVE_QUIC
315 QuicDataTransport* quic_data_transport() {
316 return quic_data_transport_.get();
317 }
318 #endif // HAVE_QUIC
319
320 private:
321 // Indicates the type of SessionDescription in a call to SetLocalDescription
322 // and SetRemoteDescription.
323 enum Action {
324 kOffer,
325 kPrAnswer,
326 kAnswer,
327 };
328
329 // Log session state.
330 void LogState(State old_state, State new_state);
331
332 // Updates the state, signaling if necessary.
333 virtual void SetState(State state);
334
335 // Updates the error state, signaling if necessary.
336 // TODO(ronghuawu): remove the SetError method that doesn't take |error_desc|.
337 virtual void SetError(Error error, const std::string& error_desc);
338
339 bool UpdateSessionState(Action action, cricket::ContentSource source,
340 std::string* err_desc);
341 static Action GetAction(const std::string& type);
342 // Push the media parts of the local or remote session description
343 // down to all of the channels.
344 bool PushdownMediaDescription(cricket::ContentAction action,
345 cricket::ContentSource source,
346 std::string* error_desc);
347
348 bool PushdownTransportDescription(cricket::ContentSource source,
349 cricket::ContentAction action,
350 std::string* error_desc);
351
352 // Helper methods to push local and remote transport descriptions.
353 bool PushdownLocalTransportDescription(
354 const cricket::SessionDescription* sdesc,
355 cricket::ContentAction action,
356 std::string* error_desc);
357 bool PushdownRemoteTransportDescription(
358 const cricket::SessionDescription* sdesc,
359 cricket::ContentAction action,
360 std::string* error_desc);
361
362 // Returns true and the TransportInfo of the given |content_name|
363 // from |description|. Returns false if it's not available.
364 static bool GetTransportDescription(
365 const cricket::SessionDescription* description,
366 const std::string& content_name,
367 cricket::TransportDescription* info);
368
369 // Returns the name of the transport channel when BUNDLE is enabled, or
370 // nullptr if the channel is not part of any bundle.
371 const std::string* GetBundleTransportName(
372 const cricket::ContentInfo* content,
373 const cricket::ContentGroup* bundle);
374
375 // Cause all the BaseChannels in the bundle group to have the same
376 // transport channel.
377 bool EnableBundle(const cricket::ContentGroup& bundle);
378
379 // Enables media channels to allow sending of media.
380 void EnableChannels();
381 // Returns the media index for a local ice candidate given the content name.
382 // Returns false if the local session description does not have a media
383 // content called |content_name|.
384 bool GetLocalCandidateMediaIndex(const std::string& content_name,
385 int* sdp_mline_index);
386 // Uses all remote candidates in |remote_desc| in this session.
387 bool UseCandidatesInSessionDescription(
388 const SessionDescriptionInterface* remote_desc);
389 // Uses |candidate| in this session.
390 bool UseCandidate(const IceCandidateInterface* candidate);
391 // Deletes the corresponding channel of contents that don't exist in |desc|.
392 // |desc| can be null. This means that all channels are deleted.
393 void RemoveUnusedChannels(const cricket::SessionDescription* desc);
394
395 // Allocates media channels based on the |desc|. If |desc| doesn't have
396 // the BUNDLE option, this method will disable BUNDLE in PortAllocator.
397 // This method will also delete any existing media channels before creating.
398 bool CreateChannels(const cricket::SessionDescription* desc);
399
400 // Helper methods to create media channels.
401 bool CreateVoiceChannel(const cricket::ContentInfo* content,
402 const std::string* bundle_transport);
403 bool CreateVideoChannel(const cricket::ContentInfo* content,
404 const std::string* bundle_transport);
405 bool CreateDataChannel(const cricket::ContentInfo* content,
406 const std::string* bundle_transport);
407
408 // Listens to SCTP CONTROL messages on unused SIDs and process them as OPEN
409 // messages.
410 void OnDataChannelMessageReceived(cricket::DataChannel* channel,
411 const cricket::ReceiveDataParams& params,
412 const rtc::CopyOnWriteBuffer& payload);
413
414 std::string BadStateErrMsg(State state);
415 void SetIceConnectionState(PeerConnectionInterface::IceConnectionState state);
416 void SetIceConnectionReceiving(bool receiving);
417
418 bool ValidateBundleSettings(const cricket::SessionDescription* desc);
419 bool HasRtcpMuxEnabled(const cricket::ContentInfo* content);
420 // Below methods are helper methods which verifies SDP.
421 bool ValidateSessionDescription(const SessionDescriptionInterface* sdesc,
422 cricket::ContentSource source,
423 std::string* err_desc);
424
425 // Check if a call to SetLocalDescription is acceptable with |action|.
426 bool ExpectSetLocalDescription(Action action);
427 // Check if a call to SetRemoteDescription is acceptable with |action|.
428 bool ExpectSetRemoteDescription(Action action);
429 // Verifies a=setup attribute as per RFC 5763.
430 bool ValidateDtlsSetupAttribute(const cricket::SessionDescription* desc,
431 Action action);
432
433 // Returns true if we are ready to push down the remote candidate.
434 // |remote_desc| is the new remote description, or NULL if the current remote
435 // description should be used. Output |valid| is true if the candidate media
436 // index is valid.
437 bool ReadyToUseRemoteCandidate(const IceCandidateInterface* candidate,
438 const SessionDescriptionInterface* remote_desc,
439 bool* valid);
440
441 void OnTransportControllerConnectionState(cricket::IceConnectionState state);
442 void OnTransportControllerReceiving(bool receiving);
443 void OnTransportControllerGatheringState(cricket::IceGatheringState state);
444 void OnTransportControllerCandidatesGathered(
445 const std::string& transport_name,
446 const std::vector<cricket::Candidate>& candidates);
447 void OnTransportControllerCandidatesRemoved(
448 const std::vector<cricket::Candidate>& candidates);
449
450 std::string GetSessionErrorMsg();
451
452 // Invoked when TransportController connection completion is signaled.
453 // Reports stats for all transports in use.
454 void ReportTransportStats();
455
456 // Gather the usage of IPv4/IPv6 as best connection.
457 void ReportBestConnectionState(const cricket::TransportStats& stats);
458
459 void ReportNegotiatedCiphers(const cricket::TransportStats& stats);
460
461 void OnSentPacket_w(const rtc::SentPacket& sent_packet);
462
463 const std::string GetTransportName(const std::string& content_name);
464
465 void OnDtlsHandshakeError(rtc::SSLHandshakeError error);
466
467 rtc::Thread* const network_thread_;
468 rtc::Thread* const worker_thread_;
469 rtc::Thread* const signaling_thread_;
470
471 State state_ = STATE_INIT;
472 Error error_ = ERROR_NONE;
473 std::string error_desc_;
474
475 const std::string sid_;
476 bool initial_offerer_ = false;
477
478 std::unique_ptr<cricket::TransportController> transport_controller_;
479 MediaControllerInterface* media_controller_;
480 std::unique_ptr<cricket::VoiceChannel> voice_channel_;
481 std::unique_ptr<cricket::VideoChannel> video_channel_;
482 std::unique_ptr<cricket::DataChannel> data_channel_;
483 cricket::ChannelManager* channel_manager_;
484 IceObserver* ice_observer_;
485 PeerConnectionInterface::IceConnectionState ice_connection_state_;
486 bool ice_connection_receiving_;
487 std::unique_ptr<SessionDescriptionInterface> local_desc_;
488 std::unique_ptr<SessionDescriptionInterface> remote_desc_;
489 // If the remote peer is using a older version of implementation.
490 bool older_version_remote_peer_;
491 bool dtls_enabled_;
492 // Specifies which kind of data channel is allowed. This is controlled
493 // by the chrome command-line flag and constraints:
494 // 1. If chrome command-line switch 'enable-sctp-data-channels' is enabled,
495 // constraint kEnableDtlsSrtp is true, and constaint kEnableRtpDataChannels is
496 // not set or false, SCTP is allowed (DCT_SCTP);
497 // 2. If constraint kEnableRtpDataChannels is true, RTP is allowed (DCT_RTP);
498 // 3. If both 1&2 are false, data channel is not allowed (DCT_NONE).
499 // The data channel type could be DCT_QUIC if the QUIC data channel is
500 // enabled.
501 cricket::DataChannelType data_channel_type_;
502 // List of content names for which the remote side triggered an ICE restart.
503 std::set<std::string> pending_ice_restarts_;
504
505 std::unique_ptr<WebRtcSessionDescriptionFactory> webrtc_session_desc_factory_;
506
507 // Member variables for caching global options.
508 cricket::AudioOptions audio_options_;
509 cricket::VideoOptions video_options_;
510 MetricsObserverInterface* metrics_observer_;
511
512 // Declares the bundle policy for the WebRTCSession.
513 PeerConnectionInterface::BundlePolicy bundle_policy_;
514
515 // Declares the RTCP mux policy for the WebRTCSession.
516 PeerConnectionInterface::RtcpMuxPolicy rtcp_mux_policy_;
517
518 bool received_first_video_packet_ = false;
519 bool received_first_audio_packet_ = false;
520
521 #ifdef HAVE_QUIC
522 std::unique_ptr<QuicDataTransport> quic_data_transport_;
523 #endif // HAVE_QUIC
524
525 RTC_DISALLOW_COPY_AND_ASSIGN(WebRtcSession);
526 };
527 } // namespace webrtc
528
529 #endif // WEBRTC_API_WEBRTCSESSION_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698