blob: 7d2cdee42d95485af974b3f14e0e3f7dac535d36 [file] [log] [blame]
wu@webrtc.org91053e72013-08-10 07:18:04 +00001/*
2 * libjingle
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +00003 * Copyright 2013 Google Inc.
wu@webrtc.org91053e72013-08-10 07:18:04 +00004 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
Henrik Kjellander15583c12016-02-10 10:53:12 +010028#ifndef WEBRTC_API_WEBRTCSESSIONDESCRIPTIONFACTORY_H_
29#define WEBRTC_API_WEBRTCSESSIONDESCRIPTIONFACTORY_H_
wu@webrtc.org91053e72013-08-10 07:18:04 +000030
wu@webrtc.org91053e72013-08-10 07:18:04 +000031#include "talk/session/media/mediasession.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010032#include "webrtc/api/dtlsidentitystore.h"
33#include "webrtc/api/peerconnectioninterface.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000034#include "webrtc/base/messagehandler.h"
Henrik Boström87713d02015-08-25 09:53:21 +020035#include "webrtc/base/rtccertificate.h"
kjellandera96e2d72016-02-04 23:52:28 -080036#include "webrtc/p2p/base/transportdescriptionfactory.h"
wu@webrtc.org91053e72013-08-10 07:18:04 +000037
38namespace cricket {
wu@webrtc.org91053e72013-08-10 07:18:04 +000039class ChannelManager;
40class TransportDescriptionFactory;
wu@webrtc.org91053e72013-08-10 07:18:04 +000041} // namespace cricket
42
43namespace webrtc {
wu@webrtc.org91053e72013-08-10 07:18:04 +000044class CreateSessionDescriptionObserver;
45class MediaConstraintsInterface;
wu@webrtc.org91053e72013-08-10 07:18:04 +000046class SessionDescriptionInterface;
47class WebRtcSession;
48
wu@webrtc.org91053e72013-08-10 07:18:04 +000049// DTLS identity request callback class.
Henrik Boström5e56c592015-08-11 10:33:13 +020050class WebRtcIdentityRequestObserver : public DtlsIdentityRequestObserver,
wu@webrtc.org91053e72013-08-10 07:18:04 +000051 public sigslot::has_slots<> {
52 public:
Henrik Boström5e56c592015-08-11 10:33:13 +020053 // DtlsIdentityRequestObserver overrides.
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +000054 void OnFailure(int error) override;
55 void OnSuccess(const std::string& der_cert,
56 const std::string& der_private_key) override;
Henrik Boström5e56c592015-08-11 10:33:13 +020057 void OnSuccess(rtc::scoped_ptr<rtc::SSLIdentity> identity) override;
wu@webrtc.org91053e72013-08-10 07:18:04 +000058
59 sigslot::signal1<int> SignalRequestFailed;
Henrik Boströmd8281982015-08-27 10:12:24 +020060 sigslot::signal1<const rtc::scoped_refptr<rtc::RTCCertificate>&>
61 SignalCertificateReady;
wu@webrtc.org91053e72013-08-10 07:18:04 +000062};
63
64struct CreateSessionDescriptionRequest {
65 enum Type {
66 kOffer,
67 kAnswer,
68 };
69
70 CreateSessionDescriptionRequest(
71 Type type,
72 CreateSessionDescriptionObserver* observer,
73 const cricket::MediaSessionOptions& options)
74 : type(type),
75 observer(observer),
76 options(options) {}
77
78 Type type;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000079 rtc::scoped_refptr<CreateSessionDescriptionObserver> observer;
wu@webrtc.org91053e72013-08-10 07:18:04 +000080 cricket::MediaSessionOptions options;
81};
82
83// This class is used to create offer/answer session description with regards to
84// the async DTLS identity generation for WebRtcSession.
85// It queues the create offer/answer request until the DTLS identity
86// request has completed, i.e. when OnIdentityRequestFailed or OnIdentityReady
87// is called.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000088class WebRtcSessionDescriptionFactory : public rtc::MessageHandler,
Henrik Boström5e56c592015-08-11 10:33:13 +020089 public sigslot::has_slots<> {
wu@webrtc.org91053e72013-08-10 07:18:04 +000090 public:
Henrik Boström87713d02015-08-25 09:53:21 +020091 // Construct with DTLS disabled.
deadbeefcbecd352015-09-23 11:50:27 -070092 WebRtcSessionDescriptionFactory(rtc::Thread* signaling_thread,
93 cricket::ChannelManager* channel_manager,
deadbeefcbecd352015-09-23 11:50:27 -070094 WebRtcSession* session,
deadbeefab9b2d12015-10-14 11:33:11 -070095 const std::string& session_id);
Henrik Boström87713d02015-08-25 09:53:21 +020096
97 // Construct with DTLS enabled using the specified |dtls_identity_store| to
98 // generate a certificate.
wu@webrtc.org91053e72013-08-10 07:18:04 +000099 WebRtcSessionDescriptionFactory(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000100 rtc::Thread* signaling_thread,
wu@webrtc.org91053e72013-08-10 07:18:04 +0000101 cricket::ChannelManager* channel_manager,
Henrik Boström5e56c592015-08-11 10:33:13 +0200102 rtc::scoped_ptr<DtlsIdentityStoreInterface> dtls_identity_store,
wu@webrtc.org91053e72013-08-10 07:18:04 +0000103 WebRtcSession* session,
deadbeefab9b2d12015-10-14 11:33:11 -0700104 const std::string& session_id);
Henrik Boström87713d02015-08-25 09:53:21 +0200105
106 // Construct with DTLS enabled using the specified (already generated)
107 // |certificate|.
108 WebRtcSessionDescriptionFactory(
109 rtc::Thread* signaling_thread,
110 cricket::ChannelManager* channel_manager,
Henrik Boström87713d02015-08-25 09:53:21 +0200111 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate,
112 WebRtcSession* session,
deadbeefab9b2d12015-10-14 11:33:11 -0700113 const std::string& session_id);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000114 virtual ~WebRtcSessionDescriptionFactory();
115
116 static void CopyCandidatesFromSessionDescription(
117 const SessionDescriptionInterface* source_desc,
118 SessionDescriptionInterface* dest_desc);
119
120 void CreateOffer(
121 CreateSessionDescriptionObserver* observer,
deadbeefab9b2d12015-10-14 11:33:11 -0700122 const PeerConnectionInterface::RTCOfferAnswerOptions& options,
123 const cricket::MediaSessionOptions& session_options);
124 void CreateAnswer(CreateSessionDescriptionObserver* observer,
125 const MediaConstraintsInterface* constraints,
126 const cricket::MediaSessionOptions& session_options);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000127
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000128 void SetSdesPolicy(cricket::SecurePolicy secure_policy);
129 cricket::SecurePolicy SdesPolicy() const;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000130
Henrik Boströmd8281982015-08-27 10:12:24 +0200131 sigslot::signal1<const rtc::scoped_refptr<rtc::RTCCertificate>&>
132 SignalCertificateReady;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000133
134 // For testing.
Henrik Boström87713d02015-08-25 09:53:21 +0200135 bool waiting_for_certificate_for_testing() const {
136 return certificate_request_state_ == CERTIFICATE_WAITING;
wu@webrtc.org364f2042013-11-20 21:49:41 +0000137 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000138
139 private:
Henrik Boström87713d02015-08-25 09:53:21 +0200140 enum CertificateRequestState {
141 CERTIFICATE_NOT_NEEDED,
142 CERTIFICATE_WAITING,
143 CERTIFICATE_SUCCEEDED,
144 CERTIFICATE_FAILED,
wu@webrtc.org91053e72013-08-10 07:18:04 +0000145 };
146
Henrik Boström87713d02015-08-25 09:53:21 +0200147 WebRtcSessionDescriptionFactory(
148 rtc::Thread* signaling_thread,
149 cricket::ChannelManager* channel_manager,
Henrik Boström87713d02015-08-25 09:53:21 +0200150 rtc::scoped_ptr<DtlsIdentityStoreInterface> dtls_identity_store,
151 const rtc::scoped_refptr<WebRtcIdentityRequestObserver>&
152 identity_request_observer,
153 WebRtcSession* session,
154 const std::string& session_id,
Henrik Boström87713d02015-08-25 09:53:21 +0200155 bool dtls_enabled);
156
wu@webrtc.org91053e72013-08-10 07:18:04 +0000157 // MessageHandler implementation.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000158 virtual void OnMessage(rtc::Message* msg);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000159
160 void InternalCreateOffer(CreateSessionDescriptionRequest request);
161 void InternalCreateAnswer(CreateSessionDescriptionRequest request);
tommi0f620f42015-07-09 03:25:02 -0700162 // Posts failure notifications for all pending session description requests.
163 void FailPendingRequests(const std::string& reason);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000164 void PostCreateSessionDescriptionFailed(
165 CreateSessionDescriptionObserver* observer,
166 const std::string& error);
167 void PostCreateSessionDescriptionSucceeded(
168 CreateSessionDescriptionObserver* observer,
169 SessionDescriptionInterface* description);
170
171 void OnIdentityRequestFailed(int error);
Henrik Boströmd8281982015-08-27 10:12:24 +0200172 void SetCertificate(
173 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000174
175 std::queue<CreateSessionDescriptionRequest>
176 create_session_description_requests_;
tommi0f620f42015-07-09 03:25:02 -0700177 rtc::Thread* const signaling_thread_;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000178 cricket::TransportDescriptionFactory transport_desc_factory_;
179 cricket::MediaSessionDescriptionFactory session_desc_factory_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200180 uint64_t session_version_;
Henrik Boström87713d02015-08-25 09:53:21 +0200181 const rtc::scoped_ptr<DtlsIdentityStoreInterface> dtls_identity_store_;
182 const rtc::scoped_refptr<WebRtcIdentityRequestObserver>
183 identity_request_observer_;
184 // TODO(jiayl): remove the dependency on session once bug 2264 is fixed.
tommi0f620f42015-07-09 03:25:02 -0700185 WebRtcSession* const session_;
186 const std::string session_id_;
Henrik Boström87713d02015-08-25 09:53:21 +0200187 CertificateRequestState certificate_request_state_;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000188
henrikg3c089d72015-09-16 05:37:44 -0700189 RTC_DISALLOW_COPY_AND_ASSIGN(WebRtcSessionDescriptionFactory);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000190};
wu@webrtc.org91053e72013-08-10 07:18:04 +0000191} // namespace webrtc
192
Henrik Kjellander15583c12016-02-10 10:53:12 +0100193#endif // WEBRTC_API_WEBRTCSESSIONDESCRIPTIONFACTORY_H_