blob: 231f718ad43232e14dba854ccab2d4828ceb1676 [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>
15
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "api/peerconnectioninterface.h"
17#include "p2p/base/transportdescriptionfactory.h"
18#include "pc/mediasession.h"
19#include "rtc_base/constructormagic.h"
20#include "rtc_base/messagehandler.h"
21#include "rtc_base/rtccertificate.h"
22#include "rtc_base/rtccertificategenerator.h"
wu@webrtc.org91053e72013-08-10 07:18:04 +000023
24namespace cricket {
wu@webrtc.org91053e72013-08-10 07:18:04 +000025class ChannelManager;
26class TransportDescriptionFactory;
wu@webrtc.org91053e72013-08-10 07:18:04 +000027} // namespace cricket
28
29namespace webrtc {
wu@webrtc.org91053e72013-08-10 07:18:04 +000030class CreateSessionDescriptionObserver;
31class MediaConstraintsInterface;
wu@webrtc.org91053e72013-08-10 07:18:04 +000032class SessionDescriptionInterface;
33class WebRtcSession;
34
Henrik Boströmd03c23b2016-06-01 11:44:18 +020035// DTLS certificate request callback class.
36class WebRtcCertificateGeneratorCallback
37 : public rtc::RTCCertificateGeneratorCallback,
38 public sigslot::has_slots<> {
wu@webrtc.org91053e72013-08-10 07:18:04 +000039 public:
Henrik Boströmd03c23b2016-06-01 11:44:18 +020040 // |rtc::RTCCertificateGeneratorCallback| overrides.
41 void OnSuccess(
42 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) override;
43 void OnFailure() override;
wu@webrtc.org91053e72013-08-10 07:18:04 +000044
Henrik Boströmd03c23b2016-06-01 11:44:18 +020045 sigslot::signal0<> SignalRequestFailed;
Henrik Boströmd8281982015-08-27 10:12:24 +020046 sigslot::signal1<const rtc::scoped_refptr<rtc::RTCCertificate>&>
47 SignalCertificateReady;
wu@webrtc.org91053e72013-08-10 07:18:04 +000048};
49
50struct CreateSessionDescriptionRequest {
51 enum Type {
52 kOffer,
53 kAnswer,
54 };
55
56 CreateSessionDescriptionRequest(
57 Type type,
58 CreateSessionDescriptionObserver* observer,
59 const cricket::MediaSessionOptions& options)
60 : type(type),
61 observer(observer),
62 options(options) {}
63
64 Type type;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000065 rtc::scoped_refptr<CreateSessionDescriptionObserver> observer;
wu@webrtc.org91053e72013-08-10 07:18:04 +000066 cricket::MediaSessionOptions options;
67};
68
Henrik Boströmd03c23b2016-06-01 11:44:18 +020069// This class is used to create offer/answer session description. Certificates
70// for WebRtcSession/DTLS are either supplied at construction or generated
71// asynchronously. It queues the create offer/answer request until the
72// certificate generation has completed, i.e. when OnCertificateRequestFailed or
73// OnCertificateReady is called.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000074class WebRtcSessionDescriptionFactory : public rtc::MessageHandler,
Henrik Boström5e56c592015-08-11 10:33:13 +020075 public sigslot::has_slots<> {
wu@webrtc.org91053e72013-08-10 07:18:04 +000076 public:
Henrik Boströmd03c23b2016-06-01 11:44:18 +020077 // If |certificate_generator| is not null, DTLS is enabled and a default
78 // certificate is generated asynchronously; otherwise DTLS is disabled.
wu@webrtc.org91053e72013-08-10 07:18:04 +000079 WebRtcSessionDescriptionFactory(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000080 rtc::Thread* signaling_thread,
wu@webrtc.org91053e72013-08-10 07:18:04 +000081 cricket::ChannelManager* channel_manager,
wu@webrtc.org91053e72013-08-10 07:18:04 +000082 WebRtcSession* session,
Henrik Boströmd03c23b2016-06-01 11:44:18 +020083 const std::string& session_id,
84 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator);
85 // Construct with DTLS enabled using the specified |certificate|.
Henrik Boström87713d02015-08-25 09:53:21 +020086 WebRtcSessionDescriptionFactory(
87 rtc::Thread* signaling_thread,
88 cricket::ChannelManager* channel_manager,
Henrik Boström87713d02015-08-25 09:53:21 +020089 WebRtcSession* session,
Henrik Boströmd03c23b2016-06-01 11:44:18 +020090 const std::string& session_id,
91 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate);
wu@webrtc.org91053e72013-08-10 07:18:04 +000092 virtual ~WebRtcSessionDescriptionFactory();
93
94 static void CopyCandidatesFromSessionDescription(
deadbeef0ed85b22016-02-23 17:24:52 -080095 const SessionDescriptionInterface* source_desc,
96 const std::string& content_name,
97 SessionDescriptionInterface* dest_desc);
wu@webrtc.org91053e72013-08-10 07:18:04 +000098
99 void CreateOffer(
100 CreateSessionDescriptionObserver* observer,
deadbeefab9b2d12015-10-14 11:33:11 -0700101 const PeerConnectionInterface::RTCOfferAnswerOptions& options,
102 const cricket::MediaSessionOptions& session_options);
103 void CreateAnswer(CreateSessionDescriptionObserver* observer,
deadbeefab9b2d12015-10-14 11:33:11 -0700104 const cricket::MediaSessionOptions& session_options);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000105
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000106 void SetSdesPolicy(cricket::SecurePolicy secure_policy);
107 cricket::SecurePolicy SdesPolicy() const;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000108
jbauch5869f502017-06-29 12:31:36 -0700109 void set_enable_encrypted_rtp_header_extensions(bool enable) {
110 session_desc_factory_.set_enable_encrypted_rtp_header_extensions(enable);
111 }
112
Henrik Boströmd8281982015-08-27 10:12:24 +0200113 sigslot::signal1<const rtc::scoped_refptr<rtc::RTCCertificate>&>
114 SignalCertificateReady;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000115
116 // For testing.
Henrik Boström87713d02015-08-25 09:53:21 +0200117 bool waiting_for_certificate_for_testing() const {
118 return certificate_request_state_ == CERTIFICATE_WAITING;
wu@webrtc.org364f2042013-11-20 21:49:41 +0000119 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000120
121 private:
Henrik Boström87713d02015-08-25 09:53:21 +0200122 enum CertificateRequestState {
123 CERTIFICATE_NOT_NEEDED,
124 CERTIFICATE_WAITING,
125 CERTIFICATE_SUCCEEDED,
126 CERTIFICATE_FAILED,
wu@webrtc.org91053e72013-08-10 07:18:04 +0000127 };
128
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200129 // If |certificate_generator| or |certificate| is not null DTLS is enabled,
130 // otherwise DTLS is disabled.
Henrik Boström87713d02015-08-25 09:53:21 +0200131 WebRtcSessionDescriptionFactory(
132 rtc::Thread* signaling_thread,
133 cricket::ChannelManager* channel_manager,
Henrik Boström87713d02015-08-25 09:53:21 +0200134 WebRtcSession* session,
135 const std::string& session_id,
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200136 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
137 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate);
Henrik Boström87713d02015-08-25 09:53:21 +0200138
wu@webrtc.org91053e72013-08-10 07:18:04 +0000139 // MessageHandler implementation.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000140 virtual void OnMessage(rtc::Message* msg);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000141
142 void InternalCreateOffer(CreateSessionDescriptionRequest request);
143 void InternalCreateAnswer(CreateSessionDescriptionRequest request);
tommi0f620f42015-07-09 03:25:02 -0700144 // Posts failure notifications for all pending session description requests.
145 void FailPendingRequests(const std::string& reason);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000146 void PostCreateSessionDescriptionFailed(
147 CreateSessionDescriptionObserver* observer,
148 const std::string& error);
149 void PostCreateSessionDescriptionSucceeded(
150 CreateSessionDescriptionObserver* observer,
151 SessionDescriptionInterface* description);
152
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200153 void OnCertificateRequestFailed();
Henrik Boströmd8281982015-08-27 10:12:24 +0200154 void SetCertificate(
155 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000156
157 std::queue<CreateSessionDescriptionRequest>
158 create_session_description_requests_;
tommi0f620f42015-07-09 03:25:02 -0700159 rtc::Thread* const signaling_thread_;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000160 cricket::TransportDescriptionFactory transport_desc_factory_;
161 cricket::MediaSessionDescriptionFactory session_desc_factory_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200162 uint64_t session_version_;
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200163 const std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator_;
Henrik Boström87713d02015-08-25 09:53:21 +0200164 // TODO(jiayl): remove the dependency on session once bug 2264 is fixed.
tommi0f620f42015-07-09 03:25:02 -0700165 WebRtcSession* const session_;
166 const std::string session_id_;
Henrik Boström87713d02015-08-25 09:53:21 +0200167 CertificateRequestState certificate_request_state_;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000168
henrikg3c089d72015-09-16 05:37:44 -0700169 RTC_DISALLOW_COPY_AND_ASSIGN(WebRtcSessionDescriptionFactory);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000170};
wu@webrtc.org91053e72013-08-10 07:18:04 +0000171} // namespace webrtc
172
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200173#endif // PC_WEBRTCSESSIONDESCRIPTIONFACTORY_H_