wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 1 | /* |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame^] | 2 | * Copyright 2013 The WebRTC project authors. All Rights Reserved. |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 3 | * |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame^] | 4 | * 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.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Henrik Kjellander | 15583c1 | 2016-02-10 10:53:12 +0100 | [diff] [blame] | 11 | #include "webrtc/api/webrtcsessiondescriptionfactory.h" |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 12 | |
kwiberg | 0eb15ed | 2015-12-17 03:04:15 -0800 | [diff] [blame] | 13 | #include <utility> |
| 14 | |
Henrik Kjellander | 15583c1 | 2016-02-10 10:53:12 +0100 | [diff] [blame] | 15 | #include "webrtc/api/dtlsidentitystore.h" |
| 16 | #include "webrtc/api/jsep.h" |
| 17 | #include "webrtc/api/jsepsessiondescription.h" |
| 18 | #include "webrtc/api/mediaconstraintsinterface.h" |
| 19 | #include "webrtc/api/webrtcsession.h" |
Henrik Boström | 5e56c59 | 2015-08-11 10:33:13 +0200 | [diff] [blame] | 20 | #include "webrtc/base/sslidentity.h" |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 21 | |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 22 | using cricket::MediaSessionOptions; |
| 23 | |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 24 | namespace webrtc { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 25 | namespace { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 26 | static const char kFailedDueToIdentityFailed[] = |
| 27 | " failed because DTLS identity request failed"; |
tommi | 0f620f4 | 2015-07-09 03:25:02 -0700 | [diff] [blame] | 28 | static const char kFailedDueToSessionShutdown[] = |
| 29 | " failed because the session was shut down"; |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 30 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 31 | static const uint64_t kInitSessionVersion = 2; |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 32 | |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 33 | static bool CompareStream(const MediaSessionOptions::Stream& stream1, |
| 34 | const MediaSessionOptions::Stream& stream2) { |
| 35 | return stream1.id < stream2.id; |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 36 | } |
| 37 | |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 38 | static bool SameId(const MediaSessionOptions::Stream& stream1, |
| 39 | const MediaSessionOptions::Stream& stream2) { |
| 40 | return stream1.id == stream2.id; |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | // Checks if each Stream within the |streams| has unique id. |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 44 | static bool ValidStreams(const MediaSessionOptions::Streams& streams) { |
| 45 | MediaSessionOptions::Streams sorted_streams = streams; |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 46 | std::sort(sorted_streams.begin(), sorted_streams.end(), CompareStream); |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 47 | MediaSessionOptions::Streams::iterator it = |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 48 | std::adjacent_find(sorted_streams.begin(), sorted_streams.end(), |
| 49 | SameId); |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 50 | return it == sorted_streams.end(); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | enum { |
| 54 | MSG_CREATE_SESSIONDESCRIPTION_SUCCESS, |
Henrik Boström | 87713d0 | 2015-08-25 09:53:21 +0200 | [diff] [blame] | 55 | MSG_CREATE_SESSIONDESCRIPTION_FAILED, |
| 56 | MSG_USE_CONSTRUCTOR_CERTIFICATE |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 57 | }; |
| 58 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 59 | struct CreateSessionDescriptionMsg : public rtc::MessageData { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 60 | explicit CreateSessionDescriptionMsg( |
| 61 | webrtc::CreateSessionDescriptionObserver* observer) |
| 62 | : observer(observer) { |
| 63 | } |
| 64 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 65 | rtc::scoped_refptr<webrtc::CreateSessionDescriptionObserver> observer; |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 66 | std::string error; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 67 | rtc::scoped_ptr<webrtc::SessionDescriptionInterface> description; |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 68 | }; |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 69 | } // namespace |
| 70 | |
jiayl@webrtc.org | 61e00b0 | 2015-03-04 22:17:38 +0000 | [diff] [blame] | 71 | void WebRtcIdentityRequestObserver::OnFailure(int error) { |
| 72 | SignalRequestFailed(error); |
| 73 | } |
| 74 | |
| 75 | void WebRtcIdentityRequestObserver::OnSuccess( |
| 76 | const std::string& der_cert, const std::string& der_private_key) { |
| 77 | std::string pem_cert = rtc::SSLIdentity::DerToPem( |
| 78 | rtc::kPemTypeCertificate, |
| 79 | reinterpret_cast<const unsigned char*>(der_cert.data()), |
| 80 | der_cert.length()); |
| 81 | std::string pem_key = rtc::SSLIdentity::DerToPem( |
| 82 | rtc::kPemTypeRsaPrivateKey, |
| 83 | reinterpret_cast<const unsigned char*>(der_private_key.data()), |
| 84 | der_private_key.length()); |
Henrik Boström | d828198 | 2015-08-27 10:12:24 +0200 | [diff] [blame] | 85 | rtc::scoped_ptr<rtc::SSLIdentity> identity( |
| 86 | rtc::SSLIdentity::FromPEMStrings(pem_key, pem_cert)); |
kwiberg | 0eb15ed | 2015-12-17 03:04:15 -0800 | [diff] [blame] | 87 | SignalCertificateReady(rtc::RTCCertificate::Create(std::move(identity))); |
jiayl@webrtc.org | 61e00b0 | 2015-03-04 22:17:38 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Henrik Boström | 5e56c59 | 2015-08-11 10:33:13 +0200 | [diff] [blame] | 90 | void WebRtcIdentityRequestObserver::OnSuccess( |
jiayl@webrtc.org | 61e00b0 | 2015-03-04 22:17:38 +0000 | [diff] [blame] | 91 | rtc::scoped_ptr<rtc::SSLIdentity> identity) { |
kwiberg | 0eb15ed | 2015-12-17 03:04:15 -0800 | [diff] [blame] | 92 | SignalCertificateReady(rtc::RTCCertificate::Create(std::move(identity))); |
jiayl@webrtc.org | 61e00b0 | 2015-03-04 22:17:38 +0000 | [diff] [blame] | 93 | } |
| 94 | |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 95 | // static |
| 96 | void WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription( |
| 97 | const SessionDescriptionInterface* source_desc, |
| 98 | SessionDescriptionInterface* dest_desc) { |
| 99 | if (!source_desc) |
| 100 | return; |
| 101 | for (size_t m = 0; m < source_desc->number_of_mediasections() && |
| 102 | m < dest_desc->number_of_mediasections(); ++m) { |
| 103 | const IceCandidateCollection* source_candidates = |
| 104 | source_desc->candidates(m); |
| 105 | const IceCandidateCollection* dest_candidates = dest_desc->candidates(m); |
| 106 | for (size_t n = 0; n < source_candidates->count(); ++n) { |
| 107 | const IceCandidateInterface* new_candidate = source_candidates->at(n); |
| 108 | if (!dest_candidates->HasCandidate(new_candidate)) |
| 109 | dest_desc->AddCandidate(source_candidates->at(n)); |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
Henrik Boström | 87713d0 | 2015-08-25 09:53:21 +0200 | [diff] [blame] | 114 | // Private constructor called by other constructors. |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 115 | WebRtcSessionDescriptionFactory::WebRtcSessionDescriptionFactory( |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 116 | rtc::Thread* signaling_thread, |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 117 | cricket::ChannelManager* channel_manager, |
Henrik Boström | 5e56c59 | 2015-08-11 10:33:13 +0200 | [diff] [blame] | 118 | rtc::scoped_ptr<DtlsIdentityStoreInterface> dtls_identity_store, |
Henrik Boström | 87713d0 | 2015-08-25 09:53:21 +0200 | [diff] [blame] | 119 | const rtc::scoped_refptr<WebRtcIdentityRequestObserver>& |
| 120 | identity_request_observer, |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 121 | WebRtcSession* session, |
| 122 | const std::string& session_id, |
sergeyu@chromium.org | a59696b | 2013-09-13 23:48:58 +0000 | [diff] [blame] | 123 | bool dtls_enabled) |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 124 | : signaling_thread_(signaling_thread), |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 125 | session_desc_factory_(channel_manager, &transport_desc_factory_), |
| 126 | // RFC 4566 suggested a Network Time Protocol (NTP) format timestamp |
| 127 | // as the session id and session version. To simplify, it should be fine |
| 128 | // to just use a random number as session id and start version from |
| 129 | // |kInitSessionVersion|. |
| 130 | session_version_(kInitSessionVersion), |
kwiberg | 0eb15ed | 2015-12-17 03:04:15 -0800 | [diff] [blame] | 131 | dtls_identity_store_(std::move(dtls_identity_store)), |
Henrik Boström | 87713d0 | 2015-08-25 09:53:21 +0200 | [diff] [blame] | 132 | identity_request_observer_(identity_request_observer), |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 133 | session_(session), |
| 134 | session_id_(session_id), |
Henrik Boström | 87713d0 | 2015-08-25 09:53:21 +0200 | [diff] [blame] | 135 | certificate_request_state_(CERTIFICATE_NOT_NEEDED) { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 136 | session_desc_factory_.set_add_legacy_streams(false); |
henrike@webrtc.org | b90991d | 2014-03-04 19:54:57 +0000 | [diff] [blame] | 137 | // SRTP-SDES is disabled if DTLS is on. |
| 138 | SetSdesPolicy(dtls_enabled ? cricket::SEC_DISABLED : cricket::SEC_REQUIRED); |
Henrik Boström | 87713d0 | 2015-08-25 09:53:21 +0200 | [diff] [blame] | 139 | } |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 140 | |
Henrik Boström | 87713d0 | 2015-08-25 09:53:21 +0200 | [diff] [blame] | 141 | WebRtcSessionDescriptionFactory::WebRtcSessionDescriptionFactory( |
| 142 | rtc::Thread* signaling_thread, |
| 143 | cricket::ChannelManager* channel_manager, |
Henrik Boström | 87713d0 | 2015-08-25 09:53:21 +0200 | [diff] [blame] | 144 | WebRtcSession* session, |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 145 | const std::string& session_id) |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 146 | : WebRtcSessionDescriptionFactory(signaling_thread, |
| 147 | channel_manager, |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 148 | nullptr, |
| 149 | nullptr, |
| 150 | session, |
| 151 | session_id, |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 152 | false) { |
Henrik Boström | 87713d0 | 2015-08-25 09:53:21 +0200 | [diff] [blame] | 153 | LOG(LS_VERBOSE) << "DTLS-SRTP disabled."; |
| 154 | } |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 155 | |
Henrik Boström | 87713d0 | 2015-08-25 09:53:21 +0200 | [diff] [blame] | 156 | WebRtcSessionDescriptionFactory::WebRtcSessionDescriptionFactory( |
| 157 | rtc::Thread* signaling_thread, |
| 158 | cricket::ChannelManager* channel_manager, |
Henrik Boström | 87713d0 | 2015-08-25 09:53:21 +0200 | [diff] [blame] | 159 | rtc::scoped_ptr<DtlsIdentityStoreInterface> dtls_identity_store, |
| 160 | WebRtcSession* session, |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 161 | const std::string& session_id) |
Henrik Boström | 87713d0 | 2015-08-25 09:53:21 +0200 | [diff] [blame] | 162 | : WebRtcSessionDescriptionFactory( |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 163 | signaling_thread, |
| 164 | channel_manager, |
kwiberg | 0eb15ed | 2015-12-17 03:04:15 -0800 | [diff] [blame] | 165 | std::move(dtls_identity_store), |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 166 | new rtc::RefCountedObject<WebRtcIdentityRequestObserver>(), |
| 167 | session, |
| 168 | session_id, |
| 169 | true) { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 170 | RTC_DCHECK(dtls_identity_store_); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 171 | |
Henrik Boström | 87713d0 | 2015-08-25 09:53:21 +0200 | [diff] [blame] | 172 | certificate_request_state_ = CERTIFICATE_WAITING; |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 173 | |
Henrik Boström | 87713d0 | 2015-08-25 09:53:21 +0200 | [diff] [blame] | 174 | identity_request_observer_->SignalRequestFailed.connect( |
| 175 | this, &WebRtcSessionDescriptionFactory::OnIdentityRequestFailed); |
Henrik Boström | d828198 | 2015-08-27 10:12:24 +0200 | [diff] [blame] | 176 | identity_request_observer_->SignalCertificateReady.connect( |
| 177 | this, &WebRtcSessionDescriptionFactory::SetCertificate); |
Henrik Boström | 87713d0 | 2015-08-25 09:53:21 +0200 | [diff] [blame] | 178 | |
| 179 | rtc::KeyType key_type = rtc::KT_DEFAULT; |
| 180 | LOG(LS_VERBOSE) << "DTLS-SRTP enabled; sending DTLS identity request (key " |
| 181 | << "type: " << key_type << ")."; |
| 182 | |
| 183 | // Request identity. This happens asynchronously, so the caller will have a |
| 184 | // chance to connect to SignalIdentityReady. |
| 185 | dtls_identity_store_->RequestIdentity(key_type, identity_request_observer_); |
| 186 | } |
| 187 | |
| 188 | WebRtcSessionDescriptionFactory::WebRtcSessionDescriptionFactory( |
| 189 | rtc::Thread* signaling_thread, |
| 190 | cricket::ChannelManager* channel_manager, |
Henrik Boström | 87713d0 | 2015-08-25 09:53:21 +0200 | [diff] [blame] | 191 | const rtc::scoped_refptr<rtc::RTCCertificate>& certificate, |
| 192 | WebRtcSession* session, |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 193 | const std::string& session_id) |
| 194 | : WebRtcSessionDescriptionFactory(signaling_thread, |
| 195 | channel_manager, |
| 196 | nullptr, |
| 197 | nullptr, |
| 198 | session, |
| 199 | session_id, |
| 200 | true) { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 201 | RTC_DCHECK(certificate); |
Henrik Boström | 87713d0 | 2015-08-25 09:53:21 +0200 | [diff] [blame] | 202 | |
| 203 | certificate_request_state_ = CERTIFICATE_WAITING; |
| 204 | |
| 205 | LOG(LS_VERBOSE) << "DTLS-SRTP enabled; has certificate parameter."; |
| 206 | // We already have a certificate but we wait to do SetIdentity; if we do |
| 207 | // it in the constructor then the caller has not had a chance to connect to |
| 208 | // SignalIdentityReady. |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 209 | signaling_thread_->Post( |
| 210 | this, MSG_USE_CONSTRUCTOR_CERTIFICATE, |
| 211 | new rtc::ScopedRefMessageData<rtc::RTCCertificate>(certificate)); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | WebRtcSessionDescriptionFactory::~WebRtcSessionDescriptionFactory() { |
tommi | 0f620f4 | 2015-07-09 03:25:02 -0700 | [diff] [blame] | 215 | ASSERT(signaling_thread_->IsCurrent()); |
| 216 | |
| 217 | // Fail any requests that were asked for before identity generation completed. |
| 218 | FailPendingRequests(kFailedDueToSessionShutdown); |
| 219 | |
| 220 | // Process all pending notifications in the message queue. If we don't do |
| 221 | // this, requests will linger and not know they succeeded or failed. |
| 222 | rtc::MessageList list; |
| 223 | signaling_thread_->Clear(this, rtc::MQID_ANY, &list); |
Henrik Boström | 87713d0 | 2015-08-25 09:53:21 +0200 | [diff] [blame] | 224 | for (auto& msg : list) { |
| 225 | if (msg.message_id != MSG_USE_CONSTRUCTOR_CERTIFICATE) { |
| 226 | OnMessage(&msg); |
| 227 | } else { |
| 228 | // Skip MSG_USE_CONSTRUCTOR_CERTIFICATE because we don't want to trigger |
| 229 | // SetIdentity-related callbacks in the destructor. This can be a problem |
| 230 | // when WebRtcSession listens to the callback but it was the WebRtcSession |
| 231 | // destructor that caused WebRtcSessionDescriptionFactory's destruction. |
| 232 | // The callback is then ignored, leaking memory allocated by OnMessage for |
| 233 | // MSG_USE_CONSTRUCTOR_CERTIFICATE. |
| 234 | delete msg.pdata; |
| 235 | } |
| 236 | } |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | void WebRtcSessionDescriptionFactory::CreateOffer( |
| 240 | CreateSessionDescriptionObserver* observer, |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 241 | const PeerConnectionInterface::RTCOfferAnswerOptions& options, |
| 242 | const cricket::MediaSessionOptions& session_options) { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 243 | std::string error = "CreateOffer"; |
Henrik Boström | 87713d0 | 2015-08-25 09:53:21 +0200 | [diff] [blame] | 244 | if (certificate_request_state_ == CERTIFICATE_FAILED) { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 245 | error += kFailedDueToIdentityFailed; |
| 246 | LOG(LS_ERROR) << error; |
| 247 | PostCreateSessionDescriptionFailed(observer, error); |
| 248 | return; |
| 249 | } |
| 250 | |
jiayl@webrtc.org | b18bf5e | 2014-08-04 18:34:16 +0000 | [diff] [blame] | 251 | if (!ValidStreams(session_options.streams)) { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 252 | error += " called with invalid media streams."; |
| 253 | LOG(LS_ERROR) << error; |
| 254 | PostCreateSessionDescriptionFailed(observer, error); |
| 255 | return; |
| 256 | } |
| 257 | |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 258 | CreateSessionDescriptionRequest request( |
jiayl@webrtc.org | b18bf5e | 2014-08-04 18:34:16 +0000 | [diff] [blame] | 259 | CreateSessionDescriptionRequest::kOffer, observer, session_options); |
Henrik Boström | 87713d0 | 2015-08-25 09:53:21 +0200 | [diff] [blame] | 260 | if (certificate_request_state_ == CERTIFICATE_WAITING) { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 261 | create_session_description_requests_.push(request); |
| 262 | } else { |
Henrik Boström | 87713d0 | 2015-08-25 09:53:21 +0200 | [diff] [blame] | 263 | ASSERT(certificate_request_state_ == CERTIFICATE_SUCCEEDED || |
| 264 | certificate_request_state_ == CERTIFICATE_NOT_NEEDED); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 265 | InternalCreateOffer(request); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | void WebRtcSessionDescriptionFactory::CreateAnswer( |
| 270 | CreateSessionDescriptionObserver* observer, |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 271 | const MediaConstraintsInterface* constraints, |
| 272 | const cricket::MediaSessionOptions& session_options) { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 273 | std::string error = "CreateAnswer"; |
Henrik Boström | 87713d0 | 2015-08-25 09:53:21 +0200 | [diff] [blame] | 274 | if (certificate_request_state_ == CERTIFICATE_FAILED) { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 275 | error += kFailedDueToIdentityFailed; |
| 276 | LOG(LS_ERROR) << error; |
| 277 | PostCreateSessionDescriptionFailed(observer, error); |
| 278 | return; |
| 279 | } |
| 280 | if (!session_->remote_description()) { |
| 281 | error += " can't be called before SetRemoteDescription."; |
| 282 | LOG(LS_ERROR) << error; |
| 283 | PostCreateSessionDescriptionFailed(observer, error); |
| 284 | return; |
| 285 | } |
| 286 | if (session_->remote_description()->type() != |
| 287 | JsepSessionDescription::kOffer) { |
| 288 | error += " failed because remote_description is not an offer."; |
| 289 | LOG(LS_ERROR) << error; |
| 290 | PostCreateSessionDescriptionFailed(observer, error); |
| 291 | return; |
| 292 | } |
| 293 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 294 | if (!ValidStreams(session_options.streams)) { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 295 | error += " called with invalid media streams."; |
| 296 | LOG(LS_ERROR) << error; |
| 297 | PostCreateSessionDescriptionFailed(observer, error); |
| 298 | return; |
| 299 | } |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 300 | |
| 301 | CreateSessionDescriptionRequest request( |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 302 | CreateSessionDescriptionRequest::kAnswer, observer, session_options); |
Henrik Boström | 87713d0 | 2015-08-25 09:53:21 +0200 | [diff] [blame] | 303 | if (certificate_request_state_ == CERTIFICATE_WAITING) { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 304 | create_session_description_requests_.push(request); |
| 305 | } else { |
Henrik Boström | 87713d0 | 2015-08-25 09:53:21 +0200 | [diff] [blame] | 306 | ASSERT(certificate_request_state_ == CERTIFICATE_SUCCEEDED || |
| 307 | certificate_request_state_ == CERTIFICATE_NOT_NEEDED); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 308 | InternalCreateAnswer(request); |
| 309 | } |
| 310 | } |
| 311 | |
henrike@webrtc.org | b90991d | 2014-03-04 19:54:57 +0000 | [diff] [blame] | 312 | void WebRtcSessionDescriptionFactory::SetSdesPolicy( |
| 313 | cricket::SecurePolicy secure_policy) { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 314 | session_desc_factory_.set_secure(secure_policy); |
| 315 | } |
| 316 | |
henrike@webrtc.org | b90991d | 2014-03-04 19:54:57 +0000 | [diff] [blame] | 317 | cricket::SecurePolicy WebRtcSessionDescriptionFactory::SdesPolicy() const { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 318 | return session_desc_factory_.secure(); |
| 319 | } |
| 320 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 321 | void WebRtcSessionDescriptionFactory::OnMessage(rtc::Message* msg) { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 322 | switch (msg->message_id) { |
| 323 | case MSG_CREATE_SESSIONDESCRIPTION_SUCCESS: { |
| 324 | CreateSessionDescriptionMsg* param = |
| 325 | static_cast<CreateSessionDescriptionMsg*>(msg->pdata); |
| 326 | param->observer->OnSuccess(param->description.release()); |
| 327 | delete param; |
| 328 | break; |
| 329 | } |
| 330 | case MSG_CREATE_SESSIONDESCRIPTION_FAILED: { |
| 331 | CreateSessionDescriptionMsg* param = |
| 332 | static_cast<CreateSessionDescriptionMsg*>(msg->pdata); |
| 333 | param->observer->OnFailure(param->error); |
| 334 | delete param; |
| 335 | break; |
| 336 | } |
Henrik Boström | 87713d0 | 2015-08-25 09:53:21 +0200 | [diff] [blame] | 337 | case MSG_USE_CONSTRUCTOR_CERTIFICATE: { |
| 338 | rtc::ScopedRefMessageData<rtc::RTCCertificate>* param = |
| 339 | static_cast<rtc::ScopedRefMessageData<rtc::RTCCertificate>*>( |
| 340 | msg->pdata); |
| 341 | LOG(LS_INFO) << "Using certificate supplied to the constructor."; |
Henrik Boström | d828198 | 2015-08-27 10:12:24 +0200 | [diff] [blame] | 342 | SetCertificate(param->data()); |
Henrik Boström | 87713d0 | 2015-08-25 09:53:21 +0200 | [diff] [blame] | 343 | delete param; |
| 344 | break; |
| 345 | } |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 346 | default: |
| 347 | ASSERT(false); |
| 348 | break; |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | void WebRtcSessionDescriptionFactory::InternalCreateOffer( |
| 353 | CreateSessionDescriptionRequest request) { |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 354 | cricket::SessionDescription* desc(session_desc_factory_.CreateOffer( |
| 355 | request.options, session_->local_description() |
| 356 | ? session_->local_description()->description() |
| 357 | : nullptr)); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 358 | // RFC 3264 |
| 359 | // When issuing an offer that modifies the session, |
| 360 | // the "o=" line of the new SDP MUST be identical to that in the |
| 361 | // previous SDP, except that the version in the origin field MUST |
| 362 | // increment by one from the previous SDP. |
| 363 | |
| 364 | // Just increase the version number by one each time when a new offer |
| 365 | // is created regardless if it's identical to the previous one or not. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 366 | // The |session_version_| is a uint64_t, the wrap around should not happen. |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 367 | ASSERT(session_version_ + 1 > session_version_); |
| 368 | JsepSessionDescription* offer(new JsepSessionDescription( |
| 369 | JsepSessionDescription::kOffer)); |
| 370 | if (!offer->Initialize(desc, session_id_, |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 371 | rtc::ToString(session_version_++))) { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 372 | delete offer; |
henrike@webrtc.org | b90991d | 2014-03-04 19:54:57 +0000 | [diff] [blame] | 373 | PostCreateSessionDescriptionFailed(request.observer, |
| 374 | "Failed to initialize the offer."); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 375 | return; |
| 376 | } |
| 377 | if (session_->local_description() && |
Taylor Brandstetter | f475d36 | 2016-01-08 15:35:57 -0800 | [diff] [blame] | 378 | !request.options.audio_transport_options.ice_restart && |
| 379 | !request.options.video_transport_options.ice_restart && |
| 380 | !request.options.data_transport_options.ice_restart) { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 381 | // Include all local ice candidates in the SessionDescription unless |
| 382 | // the an ice restart has been requested. |
| 383 | CopyCandidatesFromSessionDescription(session_->local_description(), offer); |
| 384 | } |
| 385 | PostCreateSessionDescriptionSucceeded(request.observer, offer); |
| 386 | } |
| 387 | |
| 388 | void WebRtcSessionDescriptionFactory::InternalCreateAnswer( |
| 389 | CreateSessionDescriptionRequest request) { |
| 390 | // According to http://tools.ietf.org/html/rfc5245#section-9.2.1.1 |
| 391 | // an answer should also contain new ice ufrag and password if an offer has |
| 392 | // been received with new ufrag and password. |
Taylor Brandstetter | f475d36 | 2016-01-08 15:35:57 -0800 | [diff] [blame] | 393 | request.options.audio_transport_options.ice_restart = |
| 394 | session_->IceRestartPending(); |
| 395 | request.options.video_transport_options.ice_restart = |
| 396 | session_->IceRestartPending(); |
| 397 | request.options.data_transport_options.ice_restart = |
| 398 | session_->IceRestartPending(); |
sergeyu@chromium.org | 0be6aa0 | 2013-08-23 23:21:25 +0000 | [diff] [blame] | 399 | // We should pass current ssl role to the transport description factory, if |
| 400 | // there is already an existing ongoing session. |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 401 | rtc::SSLRole ssl_role; |
Taylor Brandstetter | f475d36 | 2016-01-08 15:35:57 -0800 | [diff] [blame] | 402 | if (session_->GetSslRole(session_->voice_channel(), &ssl_role)) { |
| 403 | request.options.audio_transport_options.prefer_passive_role = |
| 404 | (rtc::SSL_SERVER == ssl_role); |
| 405 | } |
| 406 | if (session_->GetSslRole(session_->video_channel(), &ssl_role)) { |
| 407 | request.options.video_transport_options.prefer_passive_role = |
| 408 | (rtc::SSL_SERVER == ssl_role); |
| 409 | } |
| 410 | if (session_->GetSslRole(session_->data_channel(), &ssl_role)) { |
| 411 | request.options.data_transport_options.prefer_passive_role = |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 412 | (rtc::SSL_SERVER == ssl_role); |
sergeyu@chromium.org | 0be6aa0 | 2013-08-23 23:21:25 +0000 | [diff] [blame] | 413 | } |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 414 | |
| 415 | cricket::SessionDescription* desc(session_desc_factory_.CreateAnswer( |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 416 | session_->remote_description() |
| 417 | ? session_->remote_description()->description() |
| 418 | : nullptr, |
| 419 | request.options, session_->local_description() |
| 420 | ? session_->local_description()->description() |
| 421 | : nullptr)); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 422 | // RFC 3264 |
| 423 | // If the answer is different from the offer in any way (different IP |
| 424 | // addresses, ports, etc.), the origin line MUST be different in the answer. |
| 425 | // In that case, the version number in the "o=" line of the answer is |
| 426 | // unrelated to the version number in the o line of the offer. |
| 427 | // Get a new version number by increasing the |session_version_answer_|. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 428 | // The |session_version_| is a uint64_t, the wrap around should not happen. |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 429 | ASSERT(session_version_ + 1 > session_version_); |
| 430 | JsepSessionDescription* answer(new JsepSessionDescription( |
| 431 | JsepSessionDescription::kAnswer)); |
| 432 | if (!answer->Initialize(desc, session_id_, |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 433 | rtc::ToString(session_version_++))) { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 434 | delete answer; |
| 435 | PostCreateSessionDescriptionFailed(request.observer, |
henrike@webrtc.org | b90991d | 2014-03-04 19:54:57 +0000 | [diff] [blame] | 436 | "Failed to initialize the answer."); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 437 | return; |
| 438 | } |
| 439 | if (session_->local_description() && |
Taylor Brandstetter | f475d36 | 2016-01-08 15:35:57 -0800 | [diff] [blame] | 440 | !request.options.audio_transport_options.ice_restart && |
| 441 | !request.options.video_transport_options.ice_restart && |
| 442 | !request.options.data_transport_options.ice_restart) { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 443 | // Include all local ice candidates in the SessionDescription unless |
| 444 | // the remote peer has requested an ice restart. |
| 445 | CopyCandidatesFromSessionDescription(session_->local_description(), answer); |
| 446 | } |
| 447 | session_->ResetIceRestartLatch(); |
| 448 | PostCreateSessionDescriptionSucceeded(request.observer, answer); |
| 449 | } |
| 450 | |
tommi | 0f620f4 | 2015-07-09 03:25:02 -0700 | [diff] [blame] | 451 | void WebRtcSessionDescriptionFactory::FailPendingRequests( |
| 452 | const std::string& reason) { |
| 453 | ASSERT(signaling_thread_->IsCurrent()); |
| 454 | while (!create_session_description_requests_.empty()) { |
| 455 | const CreateSessionDescriptionRequest& request = |
| 456 | create_session_description_requests_.front(); |
| 457 | PostCreateSessionDescriptionFailed(request.observer, |
| 458 | ((request.type == CreateSessionDescriptionRequest::kOffer) ? |
| 459 | "CreateOffer" : "CreateAnswer") + reason); |
| 460 | create_session_description_requests_.pop(); |
| 461 | } |
| 462 | } |
| 463 | |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 464 | void WebRtcSessionDescriptionFactory::PostCreateSessionDescriptionFailed( |
| 465 | CreateSessionDescriptionObserver* observer, const std::string& error) { |
| 466 | CreateSessionDescriptionMsg* msg = new CreateSessionDescriptionMsg(observer); |
| 467 | msg->error = error; |
| 468 | signaling_thread_->Post(this, MSG_CREATE_SESSIONDESCRIPTION_FAILED, msg); |
henrike@webrtc.org | b90991d | 2014-03-04 19:54:57 +0000 | [diff] [blame] | 469 | LOG(LS_ERROR) << "Create SDP failed: " << error; |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | void WebRtcSessionDescriptionFactory::PostCreateSessionDescriptionSucceeded( |
| 473 | CreateSessionDescriptionObserver* observer, |
| 474 | SessionDescriptionInterface* description) { |
| 475 | CreateSessionDescriptionMsg* msg = new CreateSessionDescriptionMsg(observer); |
| 476 | msg->description.reset(description); |
| 477 | signaling_thread_->Post(this, MSG_CREATE_SESSIONDESCRIPTION_SUCCESS, msg); |
| 478 | } |
| 479 | |
| 480 | void WebRtcSessionDescriptionFactory::OnIdentityRequestFailed(int error) { |
| 481 | ASSERT(signaling_thread_->IsCurrent()); |
| 482 | |
| 483 | LOG(LS_ERROR) << "Async identity request failed: error = " << error; |
Henrik Boström | 87713d0 | 2015-08-25 09:53:21 +0200 | [diff] [blame] | 484 | certificate_request_state_ = CERTIFICATE_FAILED; |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 485 | |
tommi | 0f620f4 | 2015-07-09 03:25:02 -0700 | [diff] [blame] | 486 | FailPendingRequests(kFailedDueToIdentityFailed); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 487 | } |
| 488 | |
Henrik Boström | d828198 | 2015-08-27 10:12:24 +0200 | [diff] [blame] | 489 | void WebRtcSessionDescriptionFactory::SetCertificate( |
| 490 | const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 491 | RTC_DCHECK(certificate); |
Henrik Boström | d828198 | 2015-08-27 10:12:24 +0200 | [diff] [blame] | 492 | LOG(LS_VERBOSE) << "Setting new certificate"; |
jiayl@webrtc.org | 61e00b0 | 2015-03-04 22:17:38 +0000 | [diff] [blame] | 493 | |
Henrik Boström | 87713d0 | 2015-08-25 09:53:21 +0200 | [diff] [blame] | 494 | certificate_request_state_ = CERTIFICATE_SUCCEEDED; |
Henrik Boström | d828198 | 2015-08-27 10:12:24 +0200 | [diff] [blame] | 495 | SignalCertificateReady(certificate); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 496 | |
Henrik Boström | 3a14bf3 | 2015-08-31 09:27:58 +0200 | [diff] [blame] | 497 | transport_desc_factory_.set_certificate(certificate); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 498 | transport_desc_factory_.set_secure(cricket::SEC_ENABLED); |
| 499 | |
| 500 | while (!create_session_description_requests_.empty()) { |
| 501 | if (create_session_description_requests_.front().type == |
| 502 | CreateSessionDescriptionRequest::kOffer) { |
| 503 | InternalCreateOffer(create_session_description_requests_.front()); |
| 504 | } else { |
| 505 | InternalCreateAnswer(create_session_description_requests_.front()); |
| 506 | } |
| 507 | create_session_description_requests_.pop(); |
| 508 | } |
| 509 | } |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 510 | } // namespace webrtc |