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