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