blob: 8ba0ac2f4f6460d8da4fd94149af726174f2213b [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;
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +000061 sigslot::signal1<rtc::SSLIdentity*> SignalIdentityReady;
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.
92 WebRtcSessionDescriptionFactory(
93 rtc::Thread* signaling_thread,
94 cricket::ChannelManager* channel_manager,
95 MediaStreamSignaling* mediastream_signaling,
96 WebRtcSession* session,
97 const std::string& session_id,
98 cricket::DataChannelType dct);
99
100 // Construct with DTLS enabled using the specified |dtls_identity_store| to
101 // generate a certificate.
wu@webrtc.org91053e72013-08-10 07:18:04 +0000102 WebRtcSessionDescriptionFactory(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000103 rtc::Thread* signaling_thread,
wu@webrtc.org91053e72013-08-10 07:18:04 +0000104 cricket::ChannelManager* channel_manager,
105 MediaStreamSignaling* mediastream_signaling,
Henrik Boström5e56c592015-08-11 10:33:13 +0200106 rtc::scoped_ptr<DtlsIdentityStoreInterface> dtls_identity_store,
wu@webrtc.org91053e72013-08-10 07:18:04 +0000107 WebRtcSession* session,
108 const std::string& session_id,
Henrik Boström87713d02015-08-25 09:53:21 +0200109 cricket::DataChannelType dct);
110
111 // Construct with DTLS enabled using the specified (already generated)
112 // |certificate|.
113 WebRtcSessionDescriptionFactory(
114 rtc::Thread* signaling_thread,
115 cricket::ChannelManager* channel_manager,
116 MediaStreamSignaling* mediastream_signaling,
117 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate,
118 WebRtcSession* session,
119 const std::string& session_id,
120 cricket::DataChannelType dct);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000121 virtual ~WebRtcSessionDescriptionFactory();
122
123 static void CopyCandidatesFromSessionDescription(
124 const SessionDescriptionInterface* source_desc,
125 SessionDescriptionInterface* dest_desc);
126
127 void CreateOffer(
128 CreateSessionDescriptionObserver* observer,
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +0000129 const PeerConnectionInterface::RTCOfferAnswerOptions& options);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000130 void CreateAnswer(
131 CreateSessionDescriptionObserver* observer,
132 const MediaConstraintsInterface* constraints);
133
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000134 void SetSdesPolicy(cricket::SecurePolicy secure_policy);
135 cricket::SecurePolicy SdesPolicy() const;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000136
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000137 sigslot::signal1<rtc::SSLIdentity*> SignalIdentityReady;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000138
139 // For testing.
Henrik Boström87713d02015-08-25 09:53:21 +0200140 bool waiting_for_certificate_for_testing() const {
141 return certificate_request_state_ == CERTIFICATE_WAITING;
wu@webrtc.org364f2042013-11-20 21:49:41 +0000142 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000143
144 private:
Henrik Boström87713d02015-08-25 09:53:21 +0200145 enum CertificateRequestState {
146 CERTIFICATE_NOT_NEEDED,
147 CERTIFICATE_WAITING,
148 CERTIFICATE_SUCCEEDED,
149 CERTIFICATE_FAILED,
wu@webrtc.org91053e72013-08-10 07:18:04 +0000150 };
151
Henrik Boström87713d02015-08-25 09:53:21 +0200152 WebRtcSessionDescriptionFactory(
153 rtc::Thread* signaling_thread,
154 cricket::ChannelManager* channel_manager,
155 MediaStreamSignaling* mediastream_signaling,
156 rtc::scoped_ptr<DtlsIdentityStoreInterface> dtls_identity_store,
157 const rtc::scoped_refptr<WebRtcIdentityRequestObserver>&
158 identity_request_observer,
159 WebRtcSession* session,
160 const std::string& session_id,
161 cricket::DataChannelType dct,
162 bool dtls_enabled);
163
wu@webrtc.org91053e72013-08-10 07:18:04 +0000164 // MessageHandler implementation.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000165 virtual void OnMessage(rtc::Message* msg);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000166
167 void InternalCreateOffer(CreateSessionDescriptionRequest request);
168 void InternalCreateAnswer(CreateSessionDescriptionRequest request);
tommi0f620f42015-07-09 03:25:02 -0700169 // Posts failure notifications for all pending session description requests.
170 void FailPendingRequests(const std::string& reason);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000171 void PostCreateSessionDescriptionFailed(
172 CreateSessionDescriptionObserver* observer,
173 const std::string& error);
174 void PostCreateSessionDescriptionSucceeded(
175 CreateSessionDescriptionObserver* observer,
176 SessionDescriptionInterface* description);
177
178 void OnIdentityRequestFailed(int error);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000179 void SetIdentity(rtc::SSLIdentity* identity);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000180
181 std::queue<CreateSessionDescriptionRequest>
182 create_session_description_requests_;
tommi0f620f42015-07-09 03:25:02 -0700183 rtc::Thread* const signaling_thread_;
184 MediaStreamSignaling* const mediastream_signaling_;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000185 cricket::TransportDescriptionFactory transport_desc_factory_;
186 cricket::MediaSessionDescriptionFactory session_desc_factory_;
187 uint64 session_version_;
Henrik Boström87713d02015-08-25 09:53:21 +0200188 const rtc::scoped_ptr<DtlsIdentityStoreInterface> dtls_identity_store_;
189 const rtc::scoped_refptr<WebRtcIdentityRequestObserver>
190 identity_request_observer_;
191 // TODO(jiayl): remove the dependency on session once bug 2264 is fixed.
tommi0f620f42015-07-09 03:25:02 -0700192 WebRtcSession* const session_;
193 const std::string session_id_;
194 const cricket::DataChannelType data_channel_type_;
Henrik Boström87713d02015-08-25 09:53:21 +0200195 CertificateRequestState certificate_request_state_;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000196
197 DISALLOW_COPY_AND_ASSIGN(WebRtcSessionDescriptionFactory);
198};
wu@webrtc.org91053e72013-08-10 07:18:04 +0000199} // namespace webrtc
200
201#endif // TALK_APP_WEBRTC_WEBRTCSESSIONDESCRIPTIONFACTORY_H_