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" |
Henrik Boström | 5e56c59 | 2015-08-11 10:33:13 +0200 | [diff] [blame] | 36 | #include "webrtc/base/sslidentity.h" |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 37 | |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 38 | using cricket::MediaSessionOptions; |
| 39 | |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 40 | namespace webrtc { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 41 | namespace { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 42 | static const char kFailedDueToIdentityFailed[] = |
| 43 | " failed because DTLS identity request failed"; |
tommi | 0f620f4 | 2015-07-09 03:25:02 -0700 | [diff] [blame] | 44 | static const char kFailedDueToSessionShutdown[] = |
| 45 | " failed because the session was shut down"; |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 46 | |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 47 | static const uint64 kInitSessionVersion = 2; |
| 48 | |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 49 | static bool CompareStream(const MediaSessionOptions::Stream& stream1, |
| 50 | const MediaSessionOptions::Stream& stream2) { |
| 51 | return stream1.id < stream2.id; |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 52 | } |
| 53 | |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 54 | static bool SameId(const MediaSessionOptions::Stream& stream1, |
| 55 | const MediaSessionOptions::Stream& stream2) { |
| 56 | return stream1.id == stream2.id; |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | // Checks if each Stream within the |streams| has unique id. |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 60 | static bool ValidStreams(const MediaSessionOptions::Streams& streams) { |
| 61 | MediaSessionOptions::Streams sorted_streams = streams; |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 62 | std::sort(sorted_streams.begin(), sorted_streams.end(), CompareStream); |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 63 | MediaSessionOptions::Streams::iterator it = |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 64 | std::adjacent_find(sorted_streams.begin(), sorted_streams.end(), |
| 65 | SameId); |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 66 | return it == sorted_streams.end(); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | enum { |
| 70 | MSG_CREATE_SESSIONDESCRIPTION_SUCCESS, |
Henrik Boström | 5e56c59 | 2015-08-11 10:33:13 +0200 | [diff] [blame] | 71 | MSG_CREATE_SESSIONDESCRIPTION_FAILED |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 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 | |
Henrik Boström | 5e56c59 | 2015-08-11 10:33:13 +0200 | [diff] [blame] | 105 | void WebRtcIdentityRequestObserver::OnSuccess( |
jiayl@webrtc.org | 61e00b0 | 2015-03-04 22:17:38 +0000 | [diff] [blame] | 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, |
Henrik Boström | 5e56c59 | 2015-08-11 10:33:13 +0200 | [diff] [blame] | 133 | rtc::scoped_ptr<DtlsIdentityStoreInterface> dtls_identity_store, |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 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), |
Henrik Boström | 5e56c59 | 2015-08-11 10:33:13 +0200 | [diff] [blame] | 146 | dtls_identity_store_(dtls_identity_store.Pass()), |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 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 | |
Henrik Boström | 5e56c59 | 2015-08-11 10:33:13 +0200 | [diff] [blame] | 155 | // If |dtls_enabled| we must have a |dtls_identity_store_|. |
| 156 | DCHECK(!dtls_enabled || dtls_identity_store_); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 157 | |
Henrik Boström | 5e56c59 | 2015-08-11 10:33:13 +0200 | [diff] [blame] | 158 | if (dtls_enabled && dtls_identity_store_) { |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 159 | identity_request_observer_ = |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 160 | new rtc::RefCountedObject<WebRtcIdentityRequestObserver>(); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 161 | |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 162 | identity_request_observer_->SignalRequestFailed.connect( |
| 163 | this, &WebRtcSessionDescriptionFactory::OnIdentityRequestFailed); |
| 164 | identity_request_observer_->SignalIdentityReady.connect( |
jiayl@webrtc.org | 61e00b0 | 2015-03-04 22:17:38 +0000 | [diff] [blame] | 165 | this, &WebRtcSessionDescriptionFactory::SetIdentity); |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 166 | |
Henrik Boström | 5e56c59 | 2015-08-11 10:33:13 +0200 | [diff] [blame] | 167 | LOG(LS_VERBOSE) << "DTLS-SRTP enabled; sending DTLS identity request."; |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 168 | identity_request_state_ = IDENTITY_WAITING; |
Henrik Boström | 5e56c59 | 2015-08-11 10:33:13 +0200 | [diff] [blame] | 169 | dtls_identity_store_->RequestIdentity(rtc::KT_DEFAULT, |
| 170 | identity_request_observer_); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | |
| 174 | WebRtcSessionDescriptionFactory::~WebRtcSessionDescriptionFactory() { |
tommi | 0f620f4 | 2015-07-09 03:25:02 -0700 | [diff] [blame] | 175 | ASSERT(signaling_thread_->IsCurrent()); |
| 176 | |
| 177 | // Fail any requests that were asked for before identity generation completed. |
| 178 | FailPendingRequests(kFailedDueToSessionShutdown); |
| 179 | |
| 180 | // Process all pending notifications in the message queue. If we don't do |
| 181 | // this, requests will linger and not know they succeeded or failed. |
| 182 | rtc::MessageList list; |
| 183 | signaling_thread_->Clear(this, rtc::MQID_ANY, &list); |
| 184 | for (auto& msg : list) |
| 185 | OnMessage(&msg); |
| 186 | |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 187 | transport_desc_factory_.set_identity(NULL); |
| 188 | } |
| 189 | |
| 190 | void WebRtcSessionDescriptionFactory::CreateOffer( |
| 191 | CreateSessionDescriptionObserver* observer, |
jiayl@webrtc.org | b18bf5e | 2014-08-04 18:34:16 +0000 | [diff] [blame] | 192 | const PeerConnectionInterface::RTCOfferAnswerOptions& options) { |
| 193 | cricket::MediaSessionOptions session_options; |
| 194 | |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 195 | std::string error = "CreateOffer"; |
| 196 | if (identity_request_state_ == IDENTITY_FAILED) { |
| 197 | error += kFailedDueToIdentityFailed; |
| 198 | LOG(LS_ERROR) << error; |
| 199 | PostCreateSessionDescriptionFailed(observer, error); |
| 200 | return; |
| 201 | } |
| 202 | |
jiayl@webrtc.org | b18bf5e | 2014-08-04 18:34:16 +0000 | [diff] [blame] | 203 | if (!mediastream_signaling_->GetOptionsForOffer(options, |
| 204 | &session_options)) { |
| 205 | error += " called with invalid options."; |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 206 | LOG(LS_ERROR) << error; |
| 207 | PostCreateSessionDescriptionFailed(observer, error); |
| 208 | return; |
| 209 | } |
| 210 | |
jiayl@webrtc.org | b18bf5e | 2014-08-04 18:34:16 +0000 | [diff] [blame] | 211 | if (!ValidStreams(session_options.streams)) { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 212 | error += " called with invalid media streams."; |
| 213 | LOG(LS_ERROR) << error; |
| 214 | PostCreateSessionDescriptionFailed(observer, error); |
| 215 | return; |
| 216 | } |
| 217 | |
sergeyu@chromium.org | a59696b | 2013-09-13 23:48:58 +0000 | [diff] [blame] | 218 | if (data_channel_type_ == cricket::DCT_SCTP && |
| 219 | mediastream_signaling_->HasDataChannels()) { |
jiayl@webrtc.org | b18bf5e | 2014-08-04 18:34:16 +0000 | [diff] [blame] | 220 | session_options.data_channel_type = cricket::DCT_SCTP; |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | CreateSessionDescriptionRequest request( |
jiayl@webrtc.org | b18bf5e | 2014-08-04 18:34:16 +0000 | [diff] [blame] | 224 | CreateSessionDescriptionRequest::kOffer, observer, session_options); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 225 | if (identity_request_state_ == IDENTITY_WAITING) { |
| 226 | create_session_description_requests_.push(request); |
| 227 | } else { |
| 228 | ASSERT(identity_request_state_ == IDENTITY_SUCCEEDED || |
| 229 | identity_request_state_ == IDENTITY_NOT_NEEDED); |
| 230 | InternalCreateOffer(request); |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | void WebRtcSessionDescriptionFactory::CreateAnswer( |
| 235 | CreateSessionDescriptionObserver* observer, |
| 236 | const MediaConstraintsInterface* constraints) { |
| 237 | std::string error = "CreateAnswer"; |
| 238 | if (identity_request_state_ == IDENTITY_FAILED) { |
| 239 | error += kFailedDueToIdentityFailed; |
| 240 | LOG(LS_ERROR) << error; |
| 241 | PostCreateSessionDescriptionFailed(observer, error); |
| 242 | return; |
| 243 | } |
| 244 | if (!session_->remote_description()) { |
| 245 | error += " can't be called before SetRemoteDescription."; |
| 246 | LOG(LS_ERROR) << error; |
| 247 | PostCreateSessionDescriptionFailed(observer, error); |
| 248 | return; |
| 249 | } |
| 250 | if (session_->remote_description()->type() != |
| 251 | JsepSessionDescription::kOffer) { |
| 252 | error += " failed because remote_description is not an offer."; |
| 253 | LOG(LS_ERROR) << error; |
| 254 | PostCreateSessionDescriptionFailed(observer, error); |
| 255 | return; |
| 256 | } |
| 257 | |
| 258 | cricket::MediaSessionOptions options; |
| 259 | if (!mediastream_signaling_->GetOptionsForAnswer(constraints, &options)) { |
| 260 | error += " called with invalid constraints."; |
| 261 | LOG(LS_ERROR) << error; |
| 262 | PostCreateSessionDescriptionFailed(observer, error); |
| 263 | return; |
| 264 | } |
| 265 | if (!ValidStreams(options.streams)) { |
| 266 | error += " called with invalid media streams."; |
| 267 | LOG(LS_ERROR) << error; |
| 268 | PostCreateSessionDescriptionFailed(observer, error); |
| 269 | return; |
| 270 | } |
mallinath@webrtc.org | 1112c30 | 2013-09-23 20:34:45 +0000 | [diff] [blame] | 271 | // RTP data channel is handled in MediaSessionOptions::AddStream. SCTP streams |
| 272 | // are not signaled in the SDP so does not go through that path and must be |
| 273 | // handled here. |
| 274 | if (data_channel_type_ == cricket::DCT_SCTP) { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 275 | options.data_channel_type = cricket::DCT_SCTP; |
| 276 | } |
| 277 | |
| 278 | CreateSessionDescriptionRequest request( |
| 279 | CreateSessionDescriptionRequest::kAnswer, observer, options); |
| 280 | if (identity_request_state_ == IDENTITY_WAITING) { |
| 281 | create_session_description_requests_.push(request); |
| 282 | } else { |
| 283 | ASSERT(identity_request_state_ == IDENTITY_SUCCEEDED || |
| 284 | identity_request_state_ == IDENTITY_NOT_NEEDED); |
| 285 | InternalCreateAnswer(request); |
| 286 | } |
| 287 | } |
| 288 | |
henrike@webrtc.org | b90991d | 2014-03-04 19:54:57 +0000 | [diff] [blame] | 289 | void WebRtcSessionDescriptionFactory::SetSdesPolicy( |
| 290 | cricket::SecurePolicy secure_policy) { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 291 | session_desc_factory_.set_secure(secure_policy); |
| 292 | } |
| 293 | |
henrike@webrtc.org | b90991d | 2014-03-04 19:54:57 +0000 | [diff] [blame] | 294 | cricket::SecurePolicy WebRtcSessionDescriptionFactory::SdesPolicy() const { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 295 | return session_desc_factory_.secure(); |
| 296 | } |
| 297 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 298 | void WebRtcSessionDescriptionFactory::OnMessage(rtc::Message* msg) { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 299 | switch (msg->message_id) { |
| 300 | case MSG_CREATE_SESSIONDESCRIPTION_SUCCESS: { |
| 301 | CreateSessionDescriptionMsg* param = |
| 302 | static_cast<CreateSessionDescriptionMsg*>(msg->pdata); |
| 303 | param->observer->OnSuccess(param->description.release()); |
| 304 | delete param; |
| 305 | break; |
| 306 | } |
| 307 | case MSG_CREATE_SESSIONDESCRIPTION_FAILED: { |
| 308 | CreateSessionDescriptionMsg* param = |
| 309 | static_cast<CreateSessionDescriptionMsg*>(msg->pdata); |
| 310 | param->observer->OnFailure(param->error); |
| 311 | delete param; |
| 312 | break; |
| 313 | } |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 314 | default: |
| 315 | ASSERT(false); |
| 316 | break; |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | void WebRtcSessionDescriptionFactory::InternalCreateOffer( |
| 321 | CreateSessionDescriptionRequest request) { |
| 322 | cricket::SessionDescription* desc( |
| 323 | session_desc_factory_.CreateOffer( |
| 324 | request.options, |
| 325 | static_cast<cricket::BaseSession*>(session_)->local_description())); |
| 326 | // RFC 3264 |
| 327 | // When issuing an offer that modifies the session, |
| 328 | // the "o=" line of the new SDP MUST be identical to that in the |
| 329 | // previous SDP, except that the version in the origin field MUST |
| 330 | // increment by one from the previous SDP. |
| 331 | |
| 332 | // Just increase the version number by one each time when a new offer |
| 333 | // is created regardless if it's identical to the previous one or not. |
| 334 | // The |session_version_| is a uint64, the wrap around should not happen. |
| 335 | ASSERT(session_version_ + 1 > session_version_); |
| 336 | JsepSessionDescription* offer(new JsepSessionDescription( |
| 337 | JsepSessionDescription::kOffer)); |
| 338 | if (!offer->Initialize(desc, session_id_, |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 339 | rtc::ToString(session_version_++))) { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 340 | delete offer; |
henrike@webrtc.org | b90991d | 2014-03-04 19:54:57 +0000 | [diff] [blame] | 341 | PostCreateSessionDescriptionFailed(request.observer, |
| 342 | "Failed to initialize the offer."); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 343 | return; |
| 344 | } |
| 345 | if (session_->local_description() && |
| 346 | !request.options.transport_options.ice_restart) { |
| 347 | // Include all local ice candidates in the SessionDescription unless |
| 348 | // the an ice restart has been requested. |
| 349 | CopyCandidatesFromSessionDescription(session_->local_description(), offer); |
| 350 | } |
| 351 | PostCreateSessionDescriptionSucceeded(request.observer, offer); |
| 352 | } |
| 353 | |
| 354 | void WebRtcSessionDescriptionFactory::InternalCreateAnswer( |
| 355 | CreateSessionDescriptionRequest request) { |
| 356 | // According to http://tools.ietf.org/html/rfc5245#section-9.2.1.1 |
| 357 | // an answer should also contain new ice ufrag and password if an offer has |
| 358 | // been received with new ufrag and password. |
| 359 | request.options.transport_options.ice_restart = session_->IceRestartPending(); |
sergeyu@chromium.org | 0be6aa0 | 2013-08-23 23:21:25 +0000 | [diff] [blame] | 360 | // We should pass current ssl role to the transport description factory, if |
| 361 | // there is already an existing ongoing session. |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 362 | rtc::SSLRole ssl_role; |
sergeyu@chromium.org | 0be6aa0 | 2013-08-23 23:21:25 +0000 | [diff] [blame] | 363 | if (session_->GetSslRole(&ssl_role)) { |
| 364 | request.options.transport_options.prefer_passive_role = |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 365 | (rtc::SSL_SERVER == ssl_role); |
sergeyu@chromium.org | 0be6aa0 | 2013-08-23 23:21:25 +0000 | [diff] [blame] | 366 | } |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 367 | |
| 368 | cricket::SessionDescription* desc(session_desc_factory_.CreateAnswer( |
| 369 | static_cast<cricket::BaseSession*>(session_)->remote_description(), |
| 370 | request.options, |
| 371 | static_cast<cricket::BaseSession*>(session_)->local_description())); |
| 372 | // RFC 3264 |
| 373 | // If the answer is different from the offer in any way (different IP |
| 374 | // addresses, ports, etc.), the origin line MUST be different in the answer. |
| 375 | // In that case, the version number in the "o=" line of the answer is |
| 376 | // unrelated to the version number in the o line of the offer. |
| 377 | // Get a new version number by increasing the |session_version_answer_|. |
| 378 | // The |session_version_| is a uint64, the wrap around should not happen. |
| 379 | ASSERT(session_version_ + 1 > session_version_); |
| 380 | JsepSessionDescription* answer(new JsepSessionDescription( |
| 381 | JsepSessionDescription::kAnswer)); |
| 382 | if (!answer->Initialize(desc, session_id_, |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 383 | rtc::ToString(session_version_++))) { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 384 | delete answer; |
| 385 | PostCreateSessionDescriptionFailed(request.observer, |
henrike@webrtc.org | b90991d | 2014-03-04 19:54:57 +0000 | [diff] [blame] | 386 | "Failed to initialize the answer."); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 387 | return; |
| 388 | } |
| 389 | if (session_->local_description() && |
| 390 | !request.options.transport_options.ice_restart) { |
| 391 | // Include all local ice candidates in the SessionDescription unless |
| 392 | // the remote peer has requested an ice restart. |
| 393 | CopyCandidatesFromSessionDescription(session_->local_description(), answer); |
| 394 | } |
| 395 | session_->ResetIceRestartLatch(); |
| 396 | PostCreateSessionDescriptionSucceeded(request.observer, answer); |
| 397 | } |
| 398 | |
tommi | 0f620f4 | 2015-07-09 03:25:02 -0700 | [diff] [blame] | 399 | void WebRtcSessionDescriptionFactory::FailPendingRequests( |
| 400 | const std::string& reason) { |
| 401 | ASSERT(signaling_thread_->IsCurrent()); |
| 402 | while (!create_session_description_requests_.empty()) { |
| 403 | const CreateSessionDescriptionRequest& request = |
| 404 | create_session_description_requests_.front(); |
| 405 | PostCreateSessionDescriptionFailed(request.observer, |
| 406 | ((request.type == CreateSessionDescriptionRequest::kOffer) ? |
| 407 | "CreateOffer" : "CreateAnswer") + reason); |
| 408 | create_session_description_requests_.pop(); |
| 409 | } |
| 410 | } |
| 411 | |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 412 | void WebRtcSessionDescriptionFactory::PostCreateSessionDescriptionFailed( |
| 413 | CreateSessionDescriptionObserver* observer, const std::string& error) { |
| 414 | CreateSessionDescriptionMsg* msg = new CreateSessionDescriptionMsg(observer); |
| 415 | msg->error = error; |
| 416 | signaling_thread_->Post(this, MSG_CREATE_SESSIONDESCRIPTION_FAILED, msg); |
henrike@webrtc.org | b90991d | 2014-03-04 19:54:57 +0000 | [diff] [blame] | 417 | LOG(LS_ERROR) << "Create SDP failed: " << error; |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | void WebRtcSessionDescriptionFactory::PostCreateSessionDescriptionSucceeded( |
| 421 | CreateSessionDescriptionObserver* observer, |
| 422 | SessionDescriptionInterface* description) { |
| 423 | CreateSessionDescriptionMsg* msg = new CreateSessionDescriptionMsg(observer); |
| 424 | msg->description.reset(description); |
| 425 | signaling_thread_->Post(this, MSG_CREATE_SESSIONDESCRIPTION_SUCCESS, msg); |
| 426 | } |
| 427 | |
| 428 | void WebRtcSessionDescriptionFactory::OnIdentityRequestFailed(int error) { |
| 429 | ASSERT(signaling_thread_->IsCurrent()); |
| 430 | |
| 431 | LOG(LS_ERROR) << "Async identity request failed: error = " << error; |
| 432 | identity_request_state_ = IDENTITY_FAILED; |
| 433 | |
tommi | 0f620f4 | 2015-07-09 03:25:02 -0700 | [diff] [blame] | 434 | FailPendingRequests(kFailedDueToIdentityFailed); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 435 | } |
| 436 | |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 437 | void WebRtcSessionDescriptionFactory::SetIdentity( |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 438 | rtc::SSLIdentity* identity) { |
jiayl@webrtc.org | 61e00b0 | 2015-03-04 22:17:38 +0000 | [diff] [blame] | 439 | LOG(LS_VERBOSE) << "Setting new identity"; |
| 440 | |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 441 | identity_request_state_ = IDENTITY_SUCCEEDED; |
| 442 | SignalIdentityReady(identity); |
| 443 | |
| 444 | transport_desc_factory_.set_identity(identity); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 445 | transport_desc_factory_.set_secure(cricket::SEC_ENABLED); |
| 446 | |
| 447 | while (!create_session_description_requests_.empty()) { |
| 448 | if (create_session_description_requests_.front().type == |
| 449 | CreateSessionDescriptionRequest::kOffer) { |
| 450 | InternalCreateOffer(create_session_description_requests_.front()); |
| 451 | } else { |
| 452 | InternalCreateAnswer(create_session_description_requests_.front()); |
| 453 | } |
| 454 | create_session_description_requests_.pop(); |
| 455 | } |
| 456 | } |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 457 | } // namespace webrtc |