blob: c0c45b6ee50a1e5137f210bd19670750a39648e5 [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
Henrik Kjellander15583c12016-02-10 10:53:12 +010011#ifndef WEBRTC_API_WEBRTCSESSIONDESCRIPTIONFACTORY_H_
12#define WEBRTC_API_WEBRTCSESSIONDESCRIPTIONFACTORY_H_
wu@webrtc.org91053e72013-08-10 07:18:04 +000013
jbauch555604a2016-04-26 03:13:22 -070014#include <memory>
15
Henrik Kjellander15583c12016-02-10 10:53:12 +010016#include "webrtc/api/dtlsidentitystore.h"
17#include "webrtc/api/peerconnectioninterface.h"
kwiberg4485ffb2016-04-26 08:14:39 -070018#include "webrtc/base/constructormagic.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000019#include "webrtc/base/messagehandler.h"
Henrik Boström87713d02015-08-25 09:53:21 +020020#include "webrtc/base/rtccertificate.h"
Henrik Boströmd03c23b2016-06-01 11:44:18 +020021#include "webrtc/base/rtccertificategenerator.h"
kjellandera96e2d72016-02-04 23:52:28 -080022#include "webrtc/p2p/base/transportdescriptionfactory.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010023#include "webrtc/pc/mediasession.h"
wu@webrtc.org91053e72013-08-10 07:18:04 +000024
25namespace cricket {
wu@webrtc.org91053e72013-08-10 07:18:04 +000026class ChannelManager;
27class TransportDescriptionFactory;
wu@webrtc.org91053e72013-08-10 07:18:04 +000028} // namespace cricket
29
30namespace webrtc {
wu@webrtc.org91053e72013-08-10 07:18:04 +000031class CreateSessionDescriptionObserver;
32class MediaConstraintsInterface;
wu@webrtc.org91053e72013-08-10 07:18:04 +000033class SessionDescriptionInterface;
34class WebRtcSession;
35
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
57 CreateSessionDescriptionRequest(
58 Type type,
59 CreateSessionDescriptionObserver* observer,
60 const cricket::MediaSessionOptions& options)
61 : type(type),
62 observer(observer),
63 options(options) {}
64
65 Type type;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000066 rtc::scoped_refptr<CreateSessionDescriptionObserver> observer;
wu@webrtc.org91053e72013-08-10 07:18:04 +000067 cricket::MediaSessionOptions options;
68};
69
Henrik Boströmd03c23b2016-06-01 11:44:18 +020070// This class is used to create offer/answer session description. Certificates
71// for WebRtcSession/DTLS are either supplied at construction or generated
72// asynchronously. It queues the create offer/answer request until the
73// certificate generation has completed, i.e. when OnCertificateRequestFailed or
74// OnCertificateReady is called.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000075class WebRtcSessionDescriptionFactory : public rtc::MessageHandler,
Henrik Boström5e56c592015-08-11 10:33:13 +020076 public sigslot::has_slots<> {
wu@webrtc.org91053e72013-08-10 07:18:04 +000077 public:
Henrik Boströmd03c23b2016-06-01 11:44:18 +020078 // If |certificate_generator| is not null, DTLS is enabled and a default
79 // certificate is generated asynchronously; otherwise DTLS is disabled.
wu@webrtc.org91053e72013-08-10 07:18:04 +000080 WebRtcSessionDescriptionFactory(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000081 rtc::Thread* signaling_thread,
wu@webrtc.org91053e72013-08-10 07:18:04 +000082 cricket::ChannelManager* channel_manager,
wu@webrtc.org91053e72013-08-10 07:18:04 +000083 WebRtcSession* session,
Henrik Boströmd03c23b2016-06-01 11:44:18 +020084 const std::string& session_id,
85 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator);
86 // Construct with DTLS enabled using the specified |certificate|.
Henrik Boström87713d02015-08-25 09:53:21 +020087 WebRtcSessionDescriptionFactory(
88 rtc::Thread* signaling_thread,
89 cricket::ChannelManager* channel_manager,
Henrik Boström87713d02015-08-25 09:53:21 +020090 WebRtcSession* session,
Henrik Boströmd03c23b2016-06-01 11:44:18 +020091 const std::string& session_id,
92 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate);
wu@webrtc.org91053e72013-08-10 07:18:04 +000093 virtual ~WebRtcSessionDescriptionFactory();
94
95 static void CopyCandidatesFromSessionDescription(
deadbeef0ed85b22016-02-23 17:24:52 -080096 const SessionDescriptionInterface* source_desc,
97 const std::string& content_name,
98 SessionDescriptionInterface* dest_desc);
wu@webrtc.org91053e72013-08-10 07:18:04 +000099
100 void CreateOffer(
101 CreateSessionDescriptionObserver* observer,
deadbeefab9b2d12015-10-14 11:33:11 -0700102 const PeerConnectionInterface::RTCOfferAnswerOptions& options,
103 const cricket::MediaSessionOptions& session_options);
104 void CreateAnswer(CreateSessionDescriptionObserver* observer,
deadbeefab9b2d12015-10-14 11:33:11 -0700105 const cricket::MediaSessionOptions& session_options);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000106
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000107 void SetSdesPolicy(cricket::SecurePolicy secure_policy);
108 cricket::SecurePolicy SdesPolicy() const;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000109
Henrik Boströmd8281982015-08-27 10:12:24 +0200110 sigslot::signal1<const rtc::scoped_refptr<rtc::RTCCertificate>&>
111 SignalCertificateReady;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000112
113 // For testing.
Henrik Boström87713d02015-08-25 09:53:21 +0200114 bool waiting_for_certificate_for_testing() const {
115 return certificate_request_state_ == CERTIFICATE_WAITING;
wu@webrtc.org364f2042013-11-20 21:49:41 +0000116 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000117
118 private:
Henrik Boström87713d02015-08-25 09:53:21 +0200119 enum CertificateRequestState {
120 CERTIFICATE_NOT_NEEDED,
121 CERTIFICATE_WAITING,
122 CERTIFICATE_SUCCEEDED,
123 CERTIFICATE_FAILED,
wu@webrtc.org91053e72013-08-10 07:18:04 +0000124 };
125
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200126 // If |certificate_generator| or |certificate| is not null DTLS is enabled,
127 // otherwise DTLS is disabled.
Henrik Boström87713d02015-08-25 09:53:21 +0200128 WebRtcSessionDescriptionFactory(
129 rtc::Thread* signaling_thread,
130 cricket::ChannelManager* channel_manager,
Henrik Boström87713d02015-08-25 09:53:21 +0200131 WebRtcSession* session,
132 const std::string& session_id,
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200133 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
134 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate);
Henrik Boström87713d02015-08-25 09:53:21 +0200135
wu@webrtc.org91053e72013-08-10 07:18:04 +0000136 // MessageHandler implementation.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000137 virtual void OnMessage(rtc::Message* msg);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000138
139 void InternalCreateOffer(CreateSessionDescriptionRequest request);
140 void InternalCreateAnswer(CreateSessionDescriptionRequest request);
tommi0f620f42015-07-09 03:25:02 -0700141 // Posts failure notifications for all pending session description requests.
142 void FailPendingRequests(const std::string& reason);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000143 void PostCreateSessionDescriptionFailed(
144 CreateSessionDescriptionObserver* observer,
145 const std::string& error);
146 void PostCreateSessionDescriptionSucceeded(
147 CreateSessionDescriptionObserver* observer,
148 SessionDescriptionInterface* description);
149
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200150 void OnCertificateRequestFailed();
Henrik Boströmd8281982015-08-27 10:12:24 +0200151 void SetCertificate(
152 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000153
154 std::queue<CreateSessionDescriptionRequest>
155 create_session_description_requests_;
tommi0f620f42015-07-09 03:25:02 -0700156 rtc::Thread* const signaling_thread_;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000157 cricket::TransportDescriptionFactory transport_desc_factory_;
158 cricket::MediaSessionDescriptionFactory session_desc_factory_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200159 uint64_t session_version_;
Henrik Boströmd03c23b2016-06-01 11:44:18 +0200160 const std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator_;
Henrik Boström87713d02015-08-25 09:53:21 +0200161 // TODO(jiayl): remove the dependency on session once bug 2264 is fixed.
tommi0f620f42015-07-09 03:25:02 -0700162 WebRtcSession* const session_;
163 const std::string session_id_;
Henrik Boström87713d02015-08-25 09:53:21 +0200164 CertificateRequestState certificate_request_state_;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000165
henrikg3c089d72015-09-16 05:37:44 -0700166 RTC_DISALLOW_COPY_AND_ASSIGN(WebRtcSessionDescriptionFactory);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000167};
wu@webrtc.org91053e72013-08-10 07:18:04 +0000168} // namespace webrtc
169
Henrik Kjellander15583c12016-02-10 10:53:12 +0100170#endif // WEBRTC_API_WEBRTCSESSIONDESCRIPTIONFACTORY_H_