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