| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
| 5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
| 6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include "webrtc/p2p/base/session.h" | 11 // TODO(deadbeef): Remove this file when Chrome build files no longer reference |
| 12 | 12 // it. |
| 13 #include "webrtc/base/bind.h" | |
| 14 #include "webrtc/base/common.h" | |
| 15 #include "webrtc/base/helpers.h" | |
| 16 #include "webrtc/base/logging.h" | |
| 17 #include "webrtc/base/scoped_ptr.h" | |
| 18 #include "webrtc/base/stringencode.h" | |
| 19 #include "webrtc/base/sslstreamadapter.h" | |
| 20 #include "webrtc/p2p/base/transport.h" | |
| 21 #include "webrtc/p2p/base/transportinfo.h" | |
| 22 #include "webrtc/p2p/base/transportcontroller.h" | |
| 23 #include "webrtc/p2p/base/constants.h" | |
| 24 | |
| 25 namespace cricket { | |
| 26 | |
| 27 using rtc::Bind; | |
| 28 | |
| 29 std::string BaseSession::StateToString(State state) { | |
| 30 switch (state) { | |
| 31 case STATE_INIT: | |
| 32 return "STATE_INIT"; | |
| 33 case STATE_SENTINITIATE: | |
| 34 return "STATE_SENTINITIATE"; | |
| 35 case STATE_RECEIVEDINITIATE: | |
| 36 return "STATE_RECEIVEDINITIATE"; | |
| 37 case STATE_SENTPRACCEPT: | |
| 38 return "STATE_SENTPRACCEPT"; | |
| 39 case STATE_SENTACCEPT: | |
| 40 return "STATE_SENTACCEPT"; | |
| 41 case STATE_RECEIVEDPRACCEPT: | |
| 42 return "STATE_RECEIVEDPRACCEPT"; | |
| 43 case STATE_RECEIVEDACCEPT: | |
| 44 return "STATE_RECEIVEDACCEPT"; | |
| 45 case STATE_SENTMODIFY: | |
| 46 return "STATE_SENTMODIFY"; | |
| 47 case STATE_RECEIVEDMODIFY: | |
| 48 return "STATE_RECEIVEDMODIFY"; | |
| 49 case STATE_SENTREJECT: | |
| 50 return "STATE_SENTREJECT"; | |
| 51 case STATE_RECEIVEDREJECT: | |
| 52 return "STATE_RECEIVEDREJECT"; | |
| 53 case STATE_SENTREDIRECT: | |
| 54 return "STATE_SENTREDIRECT"; | |
| 55 case STATE_SENTTERMINATE: | |
| 56 return "STATE_SENTTERMINATE"; | |
| 57 case STATE_RECEIVEDTERMINATE: | |
| 58 return "STATE_RECEIVEDTERMINATE"; | |
| 59 case STATE_INPROGRESS: | |
| 60 return "STATE_INPROGRESS"; | |
| 61 case STATE_DEINIT: | |
| 62 return "STATE_DEINIT"; | |
| 63 default: | |
| 64 break; | |
| 65 } | |
| 66 return "STATE_" + rtc::ToString(state); | |
| 67 } | |
| 68 | |
| 69 BaseSession::BaseSession(rtc::Thread* signaling_thread, | |
| 70 rtc::Thread* worker_thread, | |
| 71 PortAllocator* port_allocator, | |
| 72 const std::string& sid, | |
| 73 bool initiator) | |
| 74 : state_(STATE_INIT), | |
| 75 error_(ERROR_NONE), | |
| 76 signaling_thread_(signaling_thread), | |
| 77 worker_thread_(worker_thread), | |
| 78 port_allocator_(port_allocator), | |
| 79 sid_(sid), | |
| 80 transport_controller_(new TransportController(signaling_thread, | |
| 81 worker_thread, | |
| 82 port_allocator)) { | |
| 83 ASSERT(signaling_thread->IsCurrent()); | |
| 84 set_initiator(initiator); | |
| 85 } | |
| 86 | |
| 87 BaseSession::~BaseSession() { | |
| 88 ASSERT(signaling_thread()->IsCurrent()); | |
| 89 | |
| 90 ASSERT(state_ != STATE_DEINIT); | |
| 91 LogState(state_, STATE_DEINIT); | |
| 92 state_ = STATE_DEINIT; | |
| 93 SignalState(this, state_); | |
| 94 } | |
| 95 | |
| 96 const SessionDescription* BaseSession::local_description() const { | |
| 97 // TODO(tommi): Assert on thread correctness. | |
| 98 return local_description_.get(); | |
| 99 } | |
| 100 | |
| 101 const SessionDescription* BaseSession::remote_description() const { | |
| 102 // TODO(tommi): Assert on thread correctness. | |
| 103 return remote_description_.get(); | |
| 104 } | |
| 105 | |
| 106 SessionDescription* BaseSession::remote_description() { | |
| 107 // TODO(tommi): Assert on thread correctness. | |
| 108 return remote_description_.get(); | |
| 109 } | |
| 110 | |
| 111 void BaseSession::set_local_description(const SessionDescription* sdesc) { | |
| 112 // TODO(tommi): Assert on thread correctness. | |
| 113 if (sdesc != local_description_.get()) | |
| 114 local_description_.reset(sdesc); | |
| 115 } | |
| 116 | |
| 117 void BaseSession::set_remote_description(SessionDescription* sdesc) { | |
| 118 // TODO(tommi): Assert on thread correctness. | |
| 119 if (sdesc != remote_description_) | |
| 120 remote_description_.reset(sdesc); | |
| 121 } | |
| 122 | |
| 123 void BaseSession::set_initiator(bool initiator) { | |
| 124 initiator_ = initiator; | |
| 125 | |
| 126 IceRole ice_role = initiator ? ICEROLE_CONTROLLING : ICEROLE_CONTROLLED; | |
| 127 transport_controller_->SetIceRole(ice_role); | |
| 128 } | |
| 129 | |
| 130 const SessionDescription* BaseSession::initiator_description() const { | |
| 131 // TODO(tommi): Assert on thread correctness. | |
| 132 return initiator_ ? local_description_.get() : remote_description_.get(); | |
| 133 } | |
| 134 | |
| 135 bool BaseSession::PushdownTransportDescription(ContentSource source, | |
| 136 ContentAction action, | |
| 137 std::string* error_desc) { | |
| 138 ASSERT(signaling_thread()->IsCurrent()); | |
| 139 | |
| 140 if (source == CS_LOCAL) { | |
| 141 return PushdownLocalTransportDescription(local_description(), | |
| 142 action, | |
| 143 error_desc); | |
| 144 } | |
| 145 return PushdownRemoteTransportDescription(remote_description(), | |
| 146 action, | |
| 147 error_desc); | |
| 148 } | |
| 149 | |
| 150 bool BaseSession::PushdownLocalTransportDescription( | |
| 151 const SessionDescription* sdesc, | |
| 152 ContentAction action, | |
| 153 std::string* err) { | |
| 154 ASSERT(signaling_thread()->IsCurrent()); | |
| 155 | |
| 156 if (!sdesc) { | |
| 157 return false; | |
| 158 } | |
| 159 | |
| 160 for (const TransportInfo& tinfo : sdesc->transport_infos()) { | |
| 161 if (!transport_controller_->SetLocalTransportDescription( | |
| 162 tinfo.content_name, tinfo.description, action, err)) { | |
| 163 return false; | |
| 164 } | |
| 165 } | |
| 166 | |
| 167 return true; | |
| 168 } | |
| 169 | |
| 170 bool BaseSession::PushdownRemoteTransportDescription( | |
| 171 const SessionDescription* sdesc, | |
| 172 ContentAction action, | |
| 173 std::string* err) { | |
| 174 ASSERT(signaling_thread()->IsCurrent()); | |
| 175 | |
| 176 if (!sdesc) { | |
| 177 return false; | |
| 178 } | |
| 179 | |
| 180 for (const TransportInfo& tinfo : sdesc->transport_infos()) { | |
| 181 if (!transport_controller_->SetRemoteTransportDescription( | |
| 182 tinfo.content_name, tinfo.description, action, err)) { | |
| 183 return false; | |
| 184 } | |
| 185 } | |
| 186 | |
| 187 return true; | |
| 188 } | |
| 189 | |
| 190 void BaseSession::SetState(State state) { | |
| 191 ASSERT(signaling_thread_->IsCurrent()); | |
| 192 if (state != state_) { | |
| 193 LogState(state_, state); | |
| 194 state_ = state; | |
| 195 SignalState(this, state_); | |
| 196 signaling_thread_->Post(this, MSG_STATE); | |
| 197 } | |
| 198 } | |
| 199 | |
| 200 void BaseSession::SetError(Error error, const std::string& error_desc) { | |
| 201 ASSERT(signaling_thread_->IsCurrent()); | |
| 202 if (error != error_) { | |
| 203 error_ = error; | |
| 204 error_desc_ = error_desc; | |
| 205 SignalError(this, error); | |
| 206 } | |
| 207 } | |
| 208 | |
| 209 void BaseSession::SetIceConfig(const IceConfig& config) { | |
| 210 transport_controller_->SetIceConfig(config); | |
| 211 } | |
| 212 | |
| 213 void BaseSession::MaybeStartGathering() { | |
| 214 transport_controller_->MaybeStartGathering(); | |
| 215 } | |
| 216 | |
| 217 void BaseSession::LogState(State old_state, State new_state) { | |
| 218 LOG(LS_INFO) << "Session:" << id() | |
| 219 << " Old state:" << StateToString(old_state) | |
| 220 << " New state:" << StateToString(new_state); | |
| 221 } | |
| 222 | |
| 223 // static | |
| 224 bool BaseSession::GetTransportDescription(const SessionDescription* description, | |
| 225 const std::string& content_name, | |
| 226 TransportDescription* tdesc) { | |
| 227 if (!description || !tdesc) { | |
| 228 return false; | |
| 229 } | |
| 230 const TransportInfo* transport_info = | |
| 231 description->GetTransportInfoByName(content_name); | |
| 232 if (!transport_info) { | |
| 233 return false; | |
| 234 } | |
| 235 *tdesc = transport_info->description; | |
| 236 return true; | |
| 237 } | |
| 238 | |
| 239 void BaseSession::OnMessage(rtc::Message *pmsg) { | |
| 240 switch (pmsg->message_id) { | |
| 241 case MSG_TIMEOUT: | |
| 242 // Session timeout has occured. | |
| 243 SetError(ERROR_TIME, "Session timeout has occured."); | |
| 244 break; | |
| 245 | |
| 246 case MSG_STATE: | |
| 247 switch (state_) { | |
| 248 case STATE_SENTACCEPT: | |
| 249 case STATE_RECEIVEDACCEPT: | |
| 250 SetState(STATE_INPROGRESS); | |
| 251 break; | |
| 252 | |
| 253 default: | |
| 254 // Explicitly ignoring some states here. | |
| 255 break; | |
| 256 } | |
| 257 break; | |
| 258 } | |
| 259 } | |
| 260 | |
| 261 } // namespace cricket | |
| OLD | NEW |