blob: 08d012285855afbe6b283f137aea26edc802e1a0 [file] [log] [blame]
wu@webrtc.org91053e72013-08-10 07:18:04 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2013 The WebRTC project authors. All Rights Reserved.
wu@webrtc.org91053e72013-08-10 07:18:04 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * 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.
wu@webrtc.org91053e72013-08-10 07:18:04 +00009 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef PC_WEBRTCSESSIONDESCRIPTIONFACTORY_H_
12#define PC_WEBRTCSESSIONDESCRIPTIONFACTORY_H_
wu@webrtc.org91053e72013-08-10 07:18:04 +000013
jbauch555604a2016-04-26 03:13:22 -070014#include <memory>
Steve Anton36b29d12017-10-30 09:57:42 -070015#include <queue>
16#include <string>
jbauch555604a2016-04-26 03:13:22 -070017
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "p2p/base/transportdescriptionfactory.h"
19#include "pc/mediasession.h"
Steve Anton2d8609c2018-01-23 16:38:46 -080020#include "pc/peerconnectioninternal.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "rtc_base/constructormagic.h"
22#include "rtc_base/messagehandler.h"
23#include "rtc_base/rtccertificate.h"
24#include "rtc_base/rtccertificategenerator.h"
wu@webrtc.org91053e72013-08-10 07:18:04 +000025
wu@webrtc.org91053e72013-08-10 07:18:04 +000026namespace webrtc {
wu@webrtc.org91053e72013-08-10 07:18:04 +000027
Henrik Boströmd03c23b2016-06-01 11:44:18 +020028// DTLS certificate request callback class.
29class WebRtcCertificateGeneratorCallback
30 : public rtc::RTCCertificateGeneratorCallback,
31 public sigslot::has_slots<> {
wu@webrtc.org91053e72013-08-10 07:18:04 +000032 public:
Henrik Boströmd03c23b2016-06-01 11:44:18 +020033 // |rtc::RTCCertificateGeneratorCallback| overrides.
34 void OnSuccess(
35 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) override;
36 void OnFailure() override;
wu@webrtc.org91053e72013-08-10 07:18:04 +000037
Henrik Boströmd03c23b2016-06-01 11:44:18 +020038 sigslot::signal0<> SignalRequestFailed;
Henrik Boströmd8281982015-08-27 10:12:24 +020039 sigslot::signal1<const rtc::scoped_refptr<rtc::RTCCertificate>&>
40 SignalCertificateReady;
wu@webrtc.org91053e72013-08-10 07:18:04 +000041};
42
43struct CreateSessionDescriptionRequest {
44 enum Type {
45 kOffer,
46 kAnswer,
47 };
48
Yves Gerey665174f2018-06-19 15:03:05 +020049 CreateSessionDescriptionRequest(Type type,
50 CreateSessionDescriptionObserver* observer,
51 const cricket::MediaSessionOptions& options)
52 : type(type), observer(observer), options(options) {}
wu@webrtc.org91053e72013-08-10 07:18:04 +000053
54 Type type;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000055 rtc::scoped_refptr<CreateSessionDescriptionObserver> observer;
wu@webrtc.org91053e72013-08-10 07:18:04 +000056 cricket::MediaSessionOptions options;
57};
58
Henrik Boströmd03c23b2016-06-01 11:44:18 +020059// This class is used to create offer/answer session description. Certificates
60// for WebRtcSession/DTLS are either supplied at construction or generated
61// asynchronously. It queues the create offer/answer request until the
62// certificate generation has completed, i.e. when OnCertificateRequestFailed or
63// OnCertificateReady is called.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000064class WebRtcSessionDescriptionFactory : public rtc::MessageHandler,
Henrik Boström5e56c592015-08-11 10:33:13 +020065 public sigslot::has_slots<> {
wu@webrtc.org91053e72013-08-10 07:18:04 +000066 public:
Steve Antond5585ca2017-10-23 14:49:26 -070067 // Can specify either a |cert_generator| or |certificate| to enable DTLS. If
68 // a certificate generator is given, starts generating the certificate
69 // asynchronously. If a certificate is given, will use that for identifying
70 // over DTLS. If neither is specified, DTLS is disabled.
wu@webrtc.org91053e72013-08-10 07:18:04 +000071 WebRtcSessionDescriptionFactory(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000072 rtc::Thread* signaling_thread,
wu@webrtc.org91053e72013-08-10 07:18:04 +000073 cricket::ChannelManager* channel_manager,
Steve Anton2d8609c2018-01-23 16:38:46 -080074 PeerConnectionInternal* pc,
Henrik Boströmd03c23b2016-06-01 11:44:18 +020075 const std::string& session_id,
Steve Antond5585ca2017-10-23 14:49:26 -070076 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
Henrik Boströmd03c23b2016-06-01 11:44:18 +020077 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate);
wu@webrtc.org91053e72013-08-10 07:18:04 +000078 virtual ~WebRtcSessionDescriptionFactory();
79
80 static void CopyCandidatesFromSessionDescription(
deadbeef0ed85b22016-02-23 17:24:52 -080081 const SessionDescriptionInterface* source_desc,
82 const std::string& content_name,
83 SessionDescriptionInterface* dest_desc);
wu@webrtc.org91053e72013-08-10 07:18:04 +000084
85 void CreateOffer(
86 CreateSessionDescriptionObserver* observer,
deadbeefab9b2d12015-10-14 11:33:11 -070087 const PeerConnectionInterface::RTCOfferAnswerOptions& options,
88 const cricket::MediaSessionOptions& session_options);
89 void CreateAnswer(CreateSessionDescriptionObserver* observer,
deadbeefab9b2d12015-10-14 11:33:11 -070090 const cricket::MediaSessionOptions& session_options);
wu@webrtc.org91053e72013-08-10 07:18:04 +000091
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +000092 void SetSdesPolicy(cricket::SecurePolicy secure_policy);
93 cricket::SecurePolicy SdesPolicy() const;
wu@webrtc.org91053e72013-08-10 07:18:04 +000094
jbauch5869f502017-06-29 12:31:36 -070095 void set_enable_encrypted_rtp_header_extensions(bool enable) {
96 session_desc_factory_.set_enable_encrypted_rtp_header_extensions(enable);
97 }
98
Henrik Boströmd8281982015-08-27 10:12:24 +020099 sigslot::signal1<const rtc::scoped_refptr<rtc::RTCCertificate>&>
100 SignalCertificateReady;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000101
102 // For testing.
Henrik Boström87713d02015-08-25 09:53:21 +0200103 bool waiting_for_certificate_for_testing() const {
104 return certificate_request_state_ == CERTIFICATE_WAITING;
wu@webrtc.org364f2042013-11-20 21:49:41 +0000105 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000106
107 private:
Henrik Boström87713d02015-08-25 09:53:21 +0200108 enum CertificateRequestState {
109 CERTIFICATE_NOT_NEEDED,
110 CERTIFICATE_WAITING,
111 CERTIFICATE_SUCCEEDED,
112 CERTIFICATE_FAILED,
wu@webrtc.org91053e72013-08-10 07:18:04 +0000113 };
114
115 // MessageHandler implementation.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000116 virtual void OnMessage(rtc::Message* msg);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000117
118 void InternalCreateOffer(CreateSessionDescriptionRequest request);
119 void InternalCreateAnswer(CreateSessionDescriptionRequest request);
tommi0f620f42015-07-09 03:25:02 -0700120 // Posts failure notifications for all pending session description requests.
121 void FailPendingRequests(const std::string& reason);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000122 void PostCreateSessionDescriptionFailed(
123 CreateSessionDescriptionObserver* observer,
124 const std::string& error);
125 void PostCreateSessionDescriptionSucceeded(
126 CreateSessionDescriptionObserver* observer,
Steve Antona3a92c22017-12-07 10:27:41 -0800127 std::unique_ptr<SessionDescriptionInterface> description);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000128
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200129 void OnCertificateRequestFailed();
Henrik Boströmd8281982015-08-27 10:12:24 +0200130 void SetCertificate(
131 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000132
133 std::queue<CreateSessionDescriptionRequest>
134 create_session_description_requests_;
tommi0f620f42015-07-09 03:25:02 -0700135 rtc::Thread* const signaling_thread_;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000136 cricket::TransportDescriptionFactory transport_desc_factory_;
137 cricket::MediaSessionDescriptionFactory session_desc_factory_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200138 uint64_t session_version_;
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200139 const std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator_;
Steve Antond5585ca2017-10-23 14:49:26 -0700140 // TODO(jiayl): remove the dependency on peer connection once bug 2264 is
141 // fixed.
Steve Anton2d8609c2018-01-23 16:38:46 -0800142 PeerConnectionInternal* const pc_;
tommi0f620f42015-07-09 03:25:02 -0700143 const std::string session_id_;
Henrik Boström87713d02015-08-25 09:53:21 +0200144 CertificateRequestState certificate_request_state_;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000145
henrikg3c089d72015-09-16 05:37:44 -0700146 RTC_DISALLOW_COPY_AND_ASSIGN(WebRtcSessionDescriptionFactory);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000147};
wu@webrtc.org91053e72013-08-10 07:18:04 +0000148} // namespace webrtc
149
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200150#endif // PC_WEBRTCSESSIONDESCRIPTIONFACTORY_H_