blob: 8abf6aaa26c6d474ff4b41d30d1570231b555519 [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
28#ifndef TALK_APP_WEBRTC_WEBRTCSESSIONDESCRIPTIONFACTORY_H_
29#define TALK_APP_WEBRTC_WEBRTCSESSIONDESCRIPTIONFACTORY_H_
30
Henrik Boström5e56c592015-08-11 10:33:13 +020031#include "talk/app/webrtc/dtlsidentitystore.h"
wu@webrtc.org91053e72013-08-10 07:18:04 +000032#include "talk/app/webrtc/peerconnectioninterface.h"
wu@webrtc.org91053e72013-08-10 07:18:04 +000033#include "talk/session/media/mediasession.h"
Henrik Boström5e56c592015-08-11 10:33:13 +020034#include "webrtc/p2p/base/transportdescriptionfactory.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000035#include "webrtc/base/messagehandler.h"
Henrik Boström87713d02015-08-25 09:53:21 +020036#include "webrtc/base/rtccertificate.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;
46class MediaStreamSignaling;
47class SessionDescriptionInterface;
48class WebRtcSession;
49
wu@webrtc.org91053e72013-08-10 07:18:04 +000050// DTLS identity request callback class.
Henrik Boström5e56c592015-08-11 10:33:13 +020051class WebRtcIdentityRequestObserver : public DtlsIdentityRequestObserver,
wu@webrtc.org91053e72013-08-10 07:18:04 +000052 public sigslot::has_slots<> {
53 public:
Henrik Boström5e56c592015-08-11 10:33:13 +020054 // DtlsIdentityRequestObserver overrides.
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +000055 void OnFailure(int error) override;
56 void OnSuccess(const std::string& der_cert,
57 const std::string& der_private_key) override;
Henrik Boström5e56c592015-08-11 10:33:13 +020058 void OnSuccess(rtc::scoped_ptr<rtc::SSLIdentity> identity) override;
wu@webrtc.org91053e72013-08-10 07:18:04 +000059
60 sigslot::signal1<int> SignalRequestFailed;
Henrik Boströmd8281982015-08-27 10:12:24 +020061 sigslot::signal1<const rtc::scoped_refptr<rtc::RTCCertificate>&>
62 SignalCertificateReady;
wu@webrtc.org91053e72013-08-10 07:18:04 +000063};
64
65struct CreateSessionDescriptionRequest {
66 enum Type {
67 kOffer,
68 kAnswer,
69 };
70
71 CreateSessionDescriptionRequest(
72 Type type,
73 CreateSessionDescriptionObserver* observer,
74 const cricket::MediaSessionOptions& options)
75 : type(type),
76 observer(observer),
77 options(options) {}
78
79 Type type;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000080 rtc::scoped_refptr<CreateSessionDescriptionObserver> observer;
wu@webrtc.org91053e72013-08-10 07:18:04 +000081 cricket::MediaSessionOptions options;
82};
83
84// This class is used to create offer/answer session description with regards to
85// the async DTLS identity generation for WebRtcSession.
86// It queues the create offer/answer request until the DTLS identity
87// request has completed, i.e. when OnIdentityRequestFailed or OnIdentityReady
88// is called.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000089class WebRtcSessionDescriptionFactory : public rtc::MessageHandler,
Henrik Boström5e56c592015-08-11 10:33:13 +020090 public sigslot::has_slots<> {
wu@webrtc.org91053e72013-08-10 07:18:04 +000091 public:
Henrik Boström87713d02015-08-25 09:53:21 +020092 // Construct with DTLS disabled.
93 WebRtcSessionDescriptionFactory(
94 rtc::Thread* signaling_thread,
95 cricket::ChannelManager* channel_manager,
96 MediaStreamSignaling* mediastream_signaling,
97 WebRtcSession* session,
98 const std::string& session_id,
99 cricket::DataChannelType dct);
100
101 // Construct with DTLS enabled using the specified |dtls_identity_store| to
102 // generate a certificate.
wu@webrtc.org91053e72013-08-10 07:18:04 +0000103 WebRtcSessionDescriptionFactory(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000104 rtc::Thread* signaling_thread,
wu@webrtc.org91053e72013-08-10 07:18:04 +0000105 cricket::ChannelManager* channel_manager,
106 MediaStreamSignaling* mediastream_signaling,
Henrik Boström5e56c592015-08-11 10:33:13 +0200107 rtc::scoped_ptr<DtlsIdentityStoreInterface> dtls_identity_store,
wu@webrtc.org91053e72013-08-10 07:18:04 +0000108 WebRtcSession* session,
109 const std::string& session_id,
Henrik Boström87713d02015-08-25 09:53:21 +0200110 cricket::DataChannelType dct);
111
112 // Construct with DTLS enabled using the specified (already generated)
113 // |certificate|.
114 WebRtcSessionDescriptionFactory(
115 rtc::Thread* signaling_thread,
116 cricket::ChannelManager* channel_manager,
117 MediaStreamSignaling* mediastream_signaling,
118 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate,
119 WebRtcSession* session,
120 const std::string& session_id,
121 cricket::DataChannelType dct);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000122 virtual ~WebRtcSessionDescriptionFactory();
123
124 static void CopyCandidatesFromSessionDescription(
125 const SessionDescriptionInterface* source_desc,
126 SessionDescriptionInterface* dest_desc);
127
128 void CreateOffer(
129 CreateSessionDescriptionObserver* observer,
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +0000130 const PeerConnectionInterface::RTCOfferAnswerOptions& options);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000131 void CreateAnswer(
132 CreateSessionDescriptionObserver* observer,
133 const MediaConstraintsInterface* constraints);
134
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000135 void SetSdesPolicy(cricket::SecurePolicy secure_policy);
136 cricket::SecurePolicy SdesPolicy() const;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000137
Henrik Boströmd8281982015-08-27 10:12:24 +0200138 sigslot::signal1<const rtc::scoped_refptr<rtc::RTCCertificate>&>
139 SignalCertificateReady;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000140
141 // For testing.
Henrik Boström87713d02015-08-25 09:53:21 +0200142 bool waiting_for_certificate_for_testing() const {
143 return certificate_request_state_ == CERTIFICATE_WAITING;
wu@webrtc.org364f2042013-11-20 21:49:41 +0000144 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000145
146 private:
Henrik Boström87713d02015-08-25 09:53:21 +0200147 enum CertificateRequestState {
148 CERTIFICATE_NOT_NEEDED,
149 CERTIFICATE_WAITING,
150 CERTIFICATE_SUCCEEDED,
151 CERTIFICATE_FAILED,
wu@webrtc.org91053e72013-08-10 07:18:04 +0000152 };
153
Henrik Boström87713d02015-08-25 09:53:21 +0200154 WebRtcSessionDescriptionFactory(
155 rtc::Thread* signaling_thread,
156 cricket::ChannelManager* channel_manager,
157 MediaStreamSignaling* mediastream_signaling,
158 rtc::scoped_ptr<DtlsIdentityStoreInterface> dtls_identity_store,
159 const rtc::scoped_refptr<WebRtcIdentityRequestObserver>&
160 identity_request_observer,
161 WebRtcSession* session,
162 const std::string& session_id,
163 cricket::DataChannelType dct,
164 bool dtls_enabled);
165
wu@webrtc.org91053e72013-08-10 07:18:04 +0000166 // MessageHandler implementation.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000167 virtual void OnMessage(rtc::Message* msg);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000168
169 void InternalCreateOffer(CreateSessionDescriptionRequest request);
170 void InternalCreateAnswer(CreateSessionDescriptionRequest request);
tommi0f620f42015-07-09 03:25:02 -0700171 // Posts failure notifications for all pending session description requests.
172 void FailPendingRequests(const std::string& reason);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000173 void PostCreateSessionDescriptionFailed(
174 CreateSessionDescriptionObserver* observer,
175 const std::string& error);
176 void PostCreateSessionDescriptionSucceeded(
177 CreateSessionDescriptionObserver* observer,
178 SessionDescriptionInterface* description);
179
180 void OnIdentityRequestFailed(int error);
Henrik Boströmd8281982015-08-27 10:12:24 +0200181 void SetCertificate(
182 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000183
184 std::queue<CreateSessionDescriptionRequest>
185 create_session_description_requests_;
tommi0f620f42015-07-09 03:25:02 -0700186 rtc::Thread* const signaling_thread_;
187 MediaStreamSignaling* const mediastream_signaling_;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000188 cricket::TransportDescriptionFactory transport_desc_factory_;
189 cricket::MediaSessionDescriptionFactory session_desc_factory_;
190 uint64 session_version_;
Henrik Boström87713d02015-08-25 09:53:21 +0200191 const rtc::scoped_ptr<DtlsIdentityStoreInterface> dtls_identity_store_;
192 const rtc::scoped_refptr<WebRtcIdentityRequestObserver>
193 identity_request_observer_;
194 // TODO(jiayl): remove the dependency on session once bug 2264 is fixed.
tommi0f620f42015-07-09 03:25:02 -0700195 WebRtcSession* const session_;
196 const std::string session_id_;
197 const cricket::DataChannelType data_channel_type_;
Henrik Boström87713d02015-08-25 09:53:21 +0200198 CertificateRequestState certificate_request_state_;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000199
200 DISALLOW_COPY_AND_ASSIGN(WebRtcSessionDescriptionFactory);
201};
wu@webrtc.org91053e72013-08-10 07:18:04 +0000202} // namespace webrtc
203
204#endif // TALK_APP_WEBRTC_WEBRTCSESSIONDESCRIPTIONFACTORY_H_