blob: a1bd0c7718caab67648bca1051d29118b8b74e33 [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
Steve Anton10542f22019-01-11 09:11:00 -080011#ifndef PC_WEBRTC_SESSION_DESCRIPTION_FACTORY_H_
12#define PC_WEBRTC_SESSION_DESCRIPTION_FACTORY_H_
wu@webrtc.org91053e72013-08-10 07:18:04 +000013
Yves Gerey3e707812018-11-28 16:47:49 +010014#include <stdint.h>
jbauch555604a2016-04-26 03:13:22 -070015#include <memory>
Steve Anton36b29d12017-10-30 09:57:42 -070016#include <queue>
17#include <string>
jbauch555604a2016-04-26 03:13:22 -070018
Yves Gerey3e707812018-11-28 16:47:49 +010019#include "api/jsep.h"
Steve Anton10542f22019-01-11 09:11:00 -080020#include "api/peer_connection_interface.h"
21#include "p2p/base/transport_description.h"
22#include "p2p/base/transport_description_factory.h"
23#include "pc/media_session.h"
24#include "pc/peer_connection_internal.h"
25#include "rtc_base/constructor_magic.h"
26#include "rtc_base/message_handler.h"
27#include "rtc_base/message_queue.h"
28#include "rtc_base/rtc_certificate.h"
29#include "rtc_base/rtc_certificate_generator.h"
Yves Gerey3e707812018-11-28 16:47:49 +010030#include "rtc_base/scoped_ref_ptr.h"
31#include "rtc_base/third_party/sigslot/sigslot.h"
32#include "rtc_base/thread.h"
wu@webrtc.org91053e72013-08-10 07:18:04 +000033
wu@webrtc.org91053e72013-08-10 07:18:04 +000034namespace webrtc {
wu@webrtc.org91053e72013-08-10 07:18:04 +000035
Henrik Boströmd03c23b2016-06-01 11:44:18 +020036// DTLS certificate request callback class.
37class WebRtcCertificateGeneratorCallback
38 : public rtc::RTCCertificateGeneratorCallback,
39 public sigslot::has_slots<> {
wu@webrtc.org91053e72013-08-10 07:18:04 +000040 public:
Henrik Boströmd03c23b2016-06-01 11:44:18 +020041 // |rtc::RTCCertificateGeneratorCallback| overrides.
42 void OnSuccess(
43 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) override;
44 void OnFailure() override;
wu@webrtc.org91053e72013-08-10 07:18:04 +000045
Henrik Boströmd03c23b2016-06-01 11:44:18 +020046 sigslot::signal0<> SignalRequestFailed;
Henrik Boströmd8281982015-08-27 10:12:24 +020047 sigslot::signal1<const rtc::scoped_refptr<rtc::RTCCertificate>&>
48 SignalCertificateReady;
wu@webrtc.org91053e72013-08-10 07:18:04 +000049};
50
51struct CreateSessionDescriptionRequest {
52 enum Type {
53 kOffer,
54 kAnswer,
55 };
56
Yves Gerey665174f2018-06-19 15:03:05 +020057 CreateSessionDescriptionRequest(Type type,
58 CreateSessionDescriptionObserver* observer,
59 const cricket::MediaSessionOptions& options)
60 : type(type), observer(observer), options(options) {}
wu@webrtc.org91053e72013-08-10 07:18:04 +000061
62 Type type;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000063 rtc::scoped_refptr<CreateSessionDescriptionObserver> observer;
wu@webrtc.org91053e72013-08-10 07:18:04 +000064 cricket::MediaSessionOptions options;
65};
66
Henrik Boströmd03c23b2016-06-01 11:44:18 +020067// This class is used to create offer/answer session description. Certificates
68// for WebRtcSession/DTLS are either supplied at construction or generated
69// asynchronously. It queues the create offer/answer request until the
70// certificate generation has completed, i.e. when OnCertificateRequestFailed or
71// OnCertificateReady is called.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000072class WebRtcSessionDescriptionFactory : public rtc::MessageHandler,
Henrik Boström5e56c592015-08-11 10:33:13 +020073 public sigslot::has_slots<> {
wu@webrtc.org91053e72013-08-10 07:18:04 +000074 public:
Steve Antond5585ca2017-10-23 14:49:26 -070075 // Can specify either a |cert_generator| or |certificate| to enable DTLS. If
76 // a certificate generator is given, starts generating the certificate
77 // asynchronously. If a certificate is given, will use that for identifying
78 // over DTLS. If neither is specified, 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,
Steve Anton2d8609c2018-01-23 16:38:46 -080082 PeerConnectionInternal* pc,
Henrik Boströmd03c23b2016-06-01 11:44:18 +020083 const std::string& session_id,
Steve Antond5585ca2017-10-23 14:49:26 -070084 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
Henrik Boströmd03c23b2016-06-01 11:44:18 +020085 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate);
wu@webrtc.org91053e72013-08-10 07:18:04 +000086 virtual ~WebRtcSessionDescriptionFactory();
87
88 static void CopyCandidatesFromSessionDescription(
deadbeef0ed85b22016-02-23 17:24:52 -080089 const SessionDescriptionInterface* source_desc,
90 const std::string& content_name,
91 SessionDescriptionInterface* dest_desc);
wu@webrtc.org91053e72013-08-10 07:18:04 +000092
93 void CreateOffer(
94 CreateSessionDescriptionObserver* observer,
deadbeefab9b2d12015-10-14 11:33:11 -070095 const PeerConnectionInterface::RTCOfferAnswerOptions& options,
96 const cricket::MediaSessionOptions& session_options);
97 void CreateAnswer(CreateSessionDescriptionObserver* observer,
deadbeefab9b2d12015-10-14 11:33:11 -070098 const cricket::MediaSessionOptions& session_options);
wu@webrtc.org91053e72013-08-10 07:18:04 +000099
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000100 void SetSdesPolicy(cricket::SecurePolicy secure_policy);
101 cricket::SecurePolicy SdesPolicy() const;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000102
jbauch5869f502017-06-29 12:31:36 -0700103 void set_enable_encrypted_rtp_header_extensions(bool enable) {
104 session_desc_factory_.set_enable_encrypted_rtp_header_extensions(enable);
105 }
106
Steve Anton8f66ddb2018-12-10 16:08:05 -0800107 void set_is_unified_plan(bool is_unified_plan) {
108 session_desc_factory_.set_is_unified_plan(is_unified_plan);
109 }
110
Henrik Boströmd8281982015-08-27 10:12:24 +0200111 sigslot::signal1<const rtc::scoped_refptr<rtc::RTCCertificate>&>
112 SignalCertificateReady;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000113
114 // For testing.
Henrik Boström87713d02015-08-25 09:53:21 +0200115 bool waiting_for_certificate_for_testing() const {
116 return certificate_request_state_ == CERTIFICATE_WAITING;
wu@webrtc.org364f2042013-11-20 21:49:41 +0000117 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000118
119 private:
Henrik Boström87713d02015-08-25 09:53:21 +0200120 enum CertificateRequestState {
121 CERTIFICATE_NOT_NEEDED,
122 CERTIFICATE_WAITING,
123 CERTIFICATE_SUCCEEDED,
124 CERTIFICATE_FAILED,
wu@webrtc.org91053e72013-08-10 07:18:04 +0000125 };
126
127 // MessageHandler implementation.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000128 virtual void OnMessage(rtc::Message* msg);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000129
130 void InternalCreateOffer(CreateSessionDescriptionRequest request);
131 void InternalCreateAnswer(CreateSessionDescriptionRequest request);
tommi0f620f42015-07-09 03:25:02 -0700132 // Posts failure notifications for all pending session description requests.
133 void FailPendingRequests(const std::string& reason);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000134 void PostCreateSessionDescriptionFailed(
135 CreateSessionDescriptionObserver* observer,
136 const std::string& error);
137 void PostCreateSessionDescriptionSucceeded(
138 CreateSessionDescriptionObserver* observer,
Steve Antona3a92c22017-12-07 10:27:41 -0800139 std::unique_ptr<SessionDescriptionInterface> description);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000140
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200141 void OnCertificateRequestFailed();
Henrik Boströmd8281982015-08-27 10:12:24 +0200142 void SetCertificate(
143 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000144
145 std::queue<CreateSessionDescriptionRequest>
146 create_session_description_requests_;
tommi0f620f42015-07-09 03:25:02 -0700147 rtc::Thread* const signaling_thread_;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000148 cricket::TransportDescriptionFactory transport_desc_factory_;
149 cricket::MediaSessionDescriptionFactory session_desc_factory_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200150 uint64_t session_version_;
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200151 const std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator_;
Steve Antond5585ca2017-10-23 14:49:26 -0700152 // TODO(jiayl): remove the dependency on peer connection once bug 2264 is
153 // fixed.
Steve Anton2d8609c2018-01-23 16:38:46 -0800154 PeerConnectionInternal* const pc_;
tommi0f620f42015-07-09 03:25:02 -0700155 const std::string session_id_;
Henrik Boström87713d02015-08-25 09:53:21 +0200156 CertificateRequestState certificate_request_state_;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000157
henrikg3c089d72015-09-16 05:37:44 -0700158 RTC_DISALLOW_COPY_AND_ASSIGN(WebRtcSessionDescriptionFactory);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000159};
wu@webrtc.org91053e72013-08-10 07:18:04 +0000160} // namespace webrtc
161
Steve Anton10542f22019-01-11 09:11:00 -0800162#endif // PC_WEBRTC_SESSION_DESCRIPTION_FACTORY_H_