henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * libjingle |
| 3 | * Copyright 2004--2005, Google Inc. |
| 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 | #ifndef TALK_P2P_BASE_SESSION_H_ |
| 29 | #define TALK_P2P_BASE_SESSION_H_ |
| 30 | |
| 31 | #include <list> |
| 32 | #include <map> |
| 33 | #include <string> |
| 34 | #include <vector> |
| 35 | |
| 36 | #include "talk/base/refcount.h" |
| 37 | #include "talk/base/scoped_ptr.h" |
| 38 | #include "talk/base/scoped_ref_ptr.h" |
| 39 | #include "talk/base/socketaddress.h" |
| 40 | #include "talk/p2p/base/parsing.h" |
| 41 | #include "talk/p2p/base/port.h" |
| 42 | #include "talk/p2p/base/sessionclient.h" |
| 43 | #include "talk/p2p/base/sessionmanager.h" |
| 44 | #include "talk/p2p/base/sessionmessages.h" |
| 45 | #include "talk/p2p/base/transport.h" |
| 46 | #include "talk/xmllite/xmlelement.h" |
| 47 | #include "talk/xmpp/constants.h" |
| 48 | |
| 49 | namespace cricket { |
| 50 | |
| 51 | class BaseSession; |
| 52 | class P2PTransportChannel; |
| 53 | class Transport; |
| 54 | class TransportChannel; |
| 55 | class TransportChannelProxy; |
| 56 | class TransportChannelImpl; |
| 57 | |
| 58 | typedef talk_base::RefCountedObject<talk_base::scoped_ptr<Transport> > |
| 59 | TransportWrapper; |
| 60 | |
| 61 | // Used for errors that will send back a specific error message to the |
| 62 | // remote peer. We add "type" to the errors because it's needed for |
| 63 | // SignalErrorMessage. |
| 64 | struct MessageError : ParseError { |
| 65 | buzz::QName type; |
| 66 | |
| 67 | // if unset, assume type is a parse error |
| 68 | MessageError() : ParseError(), type(buzz::QN_STANZA_BAD_REQUEST) {} |
| 69 | |
| 70 | void SetType(const buzz::QName type) { |
| 71 | this->type = type; |
| 72 | } |
| 73 | }; |
| 74 | |
| 75 | // Used for errors that may be returned by public session methods that |
| 76 | // can fail. |
| 77 | // TODO: Use this error in Session::Initiate and |
| 78 | // Session::Accept. |
| 79 | struct SessionError : WriteError { |
| 80 | }; |
| 81 | |
| 82 | // Bundles a Transport and ChannelMap together. ChannelMap is used to |
| 83 | // create transport channels before receiving or sending a session |
| 84 | // initiate, and for speculatively connecting channels. Previously, a |
| 85 | // session had one ChannelMap and transport. Now, with multiple |
| 86 | // transports per session, we need multiple ChannelMaps as well. |
| 87 | |
| 88 | typedef std::map<int, TransportChannelProxy*> ChannelMap; |
| 89 | |
| 90 | class TransportProxy : public sigslot::has_slots<>, |
| 91 | public CandidateTranslator { |
| 92 | public: |
| 93 | TransportProxy( |
mallinath@webrtc.org | a27be8e | 2013-09-27 23:04:10 +0000 | [diff] [blame] | 94 | talk_base::Thread* worker_thread, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 95 | const std::string& sid, |
| 96 | const std::string& content_name, |
| 97 | TransportWrapper* transport) |
mallinath@webrtc.org | a27be8e | 2013-09-27 23:04:10 +0000 | [diff] [blame] | 98 | : worker_thread_(worker_thread), |
| 99 | sid_(sid), |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 100 | content_name_(content_name), |
| 101 | transport_(transport), |
| 102 | connecting_(false), |
| 103 | negotiated_(false), |
| 104 | sent_candidates_(false), |
| 105 | candidates_allocated_(false) { |
| 106 | transport_->get()->SignalCandidatesReady.connect( |
| 107 | this, &TransportProxy::OnTransportCandidatesReady); |
| 108 | } |
| 109 | ~TransportProxy(); |
| 110 | |
| 111 | std::string content_name() const { return content_name_; } |
| 112 | // TODO(juberti): It's not good form to expose the object you're wrapping, |
| 113 | // since callers can mutate it. Can we make this return a const Transport*? |
| 114 | Transport* impl() const { return transport_->get(); } |
| 115 | |
| 116 | std::string type() const; |
| 117 | bool negotiated() const { return negotiated_; } |
| 118 | const Candidates& sent_candidates() const { return sent_candidates_; } |
| 119 | const Candidates& unsent_candidates() const { return unsent_candidates_; } |
| 120 | bool candidates_allocated() const { return candidates_allocated_; } |
| 121 | void set_candidates_allocated(bool allocated) { |
| 122 | candidates_allocated_ = allocated; |
| 123 | } |
| 124 | |
| 125 | TransportChannel* GetChannel(int component); |
| 126 | TransportChannel* CreateChannel(const std::string& channel_name, |
| 127 | int component); |
| 128 | bool HasChannel(int component); |
| 129 | void DestroyChannel(int component); |
| 130 | |
| 131 | void AddSentCandidates(const Candidates& candidates); |
| 132 | void AddUnsentCandidates(const Candidates& candidates); |
| 133 | void ClearSentCandidates() { sent_candidates_.clear(); } |
| 134 | void ClearUnsentCandidates() { unsent_candidates_.clear(); } |
| 135 | |
| 136 | // Start the connection process for any channels, creating impls if needed. |
| 137 | void ConnectChannels(); |
| 138 | // Hook up impls to the proxy channels. Doesn't change connect state. |
| 139 | void CompleteNegotiation(); |
| 140 | |
| 141 | // Mux this proxy onto the specified proxy's transport. |
| 142 | bool SetupMux(TransportProxy* proxy); |
| 143 | |
| 144 | // Simple functions that thunk down to the same functions on Transport. |
mallinath@webrtc.org | a550669 | 2013-08-12 21:18:15 +0000 | [diff] [blame] | 145 | void SetIceRole(IceRole role); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 146 | void SetIdentity(talk_base::SSLIdentity* identity); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 147 | bool SetLocalTransportDescription(const TransportDescription& description, |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame^] | 148 | ContentAction action, |
| 149 | std::string* error_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 150 | bool SetRemoteTransportDescription(const TransportDescription& description, |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame^] | 151 | ContentAction action, |
| 152 | std::string* error_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 153 | void OnSignalingReady(); |
| 154 | bool OnRemoteCandidates(const Candidates& candidates, std::string* error); |
| 155 | |
| 156 | // CandidateTranslator methods. |
| 157 | virtual bool GetChannelNameFromComponent( |
| 158 | int component, std::string* channel_name) const; |
| 159 | virtual bool GetComponentFromChannelName( |
| 160 | const std::string& channel_name, int* component) const; |
| 161 | |
| 162 | // Called when a transport signals that it has new candidates. |
| 163 | void OnTransportCandidatesReady(cricket::Transport* transport, |
| 164 | const Candidates& candidates) { |
| 165 | SignalCandidatesReady(this, candidates); |
| 166 | } |
| 167 | |
| 168 | // Handles sending of ready candidates and receiving of remote candidates. |
| 169 | sigslot::signal2<TransportProxy*, |
| 170 | const std::vector<Candidate>&> SignalCandidatesReady; |
| 171 | |
| 172 | private: |
| 173 | TransportChannelProxy* GetChannelProxy(int component) const; |
| 174 | TransportChannelProxy* GetChannelProxyByName(const std::string& name) const; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 175 | |
mallinath@webrtc.org | a27be8e | 2013-09-27 23:04:10 +0000 | [diff] [blame] | 176 | TransportChannelImpl* GetOrCreateChannelProxyImpl(int component); |
| 177 | TransportChannelImpl* GetOrCreateChannelProxyImpl_w(int component); |
| 178 | |
| 179 | // Manipulators of transportchannelimpl in channel proxy. |
| 180 | void SetupChannelProxy(int component, |
| 181 | TransportChannelProxy* proxy); |
| 182 | void SetupChannelProxy_w(int component, |
| 183 | TransportChannelProxy* proxy); |
| 184 | void ReplaceChannelProxyImpl(TransportChannelProxy* proxy, |
| 185 | TransportChannelImpl* impl); |
| 186 | void ReplaceChannelProxyImpl_w(TransportChannelProxy* proxy, |
| 187 | TransportChannelImpl* impl); |
| 188 | |
| 189 | talk_base::Thread* worker_thread_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 190 | std::string sid_; |
| 191 | std::string content_name_; |
| 192 | talk_base::scoped_refptr<TransportWrapper> transport_; |
| 193 | bool connecting_; |
| 194 | bool negotiated_; |
| 195 | ChannelMap channels_; |
| 196 | Candidates sent_candidates_; |
| 197 | Candidates unsent_candidates_; |
| 198 | bool candidates_allocated_; |
| 199 | }; |
| 200 | |
| 201 | typedef std::map<std::string, TransportProxy*> TransportMap; |
| 202 | |
| 203 | // Statistics for all the transports of this session. |
| 204 | typedef std::map<std::string, TransportStats> TransportStatsMap; |
| 205 | typedef std::map<std::string, std::string> ProxyTransportMap; |
| 206 | |
| 207 | struct SessionStats { |
| 208 | ProxyTransportMap proxy_to_transport; |
| 209 | TransportStatsMap transport_stats; |
| 210 | }; |
| 211 | |
| 212 | // A BaseSession manages general session state. This includes negotiation |
| 213 | // of both the application-level and network-level protocols: the former |
| 214 | // defines what will be sent and the latter defines how it will be sent. Each |
| 215 | // network-level protocol is represented by a Transport object. Each Transport |
| 216 | // participates in the network-level negotiation. The individual streams of |
| 217 | // packets are represented by TransportChannels. The application-level protocol |
| 218 | // is represented by SessionDecription objects. |
| 219 | class BaseSession : public sigslot::has_slots<>, |
| 220 | public talk_base::MessageHandler { |
| 221 | public: |
| 222 | enum { |
| 223 | MSG_TIMEOUT = 0, |
| 224 | MSG_ERROR, |
| 225 | MSG_STATE, |
| 226 | }; |
| 227 | |
| 228 | enum State { |
| 229 | STATE_INIT = 0, |
| 230 | STATE_SENTINITIATE, // sent initiate, waiting for Accept or Reject |
| 231 | STATE_RECEIVEDINITIATE, // received an initiate. Call Accept or Reject |
| 232 | STATE_SENTPRACCEPT, // sent provisional Accept |
| 233 | STATE_SENTACCEPT, // sent accept. begin connecting transport |
| 234 | STATE_RECEIVEDPRACCEPT, // received provisional Accept, waiting for Accept |
| 235 | STATE_RECEIVEDACCEPT, // received accept. begin connecting transport |
| 236 | STATE_SENTMODIFY, // sent modify, waiting for Accept or Reject |
| 237 | STATE_RECEIVEDMODIFY, // received modify, call Accept or Reject |
| 238 | STATE_SENTREJECT, // sent reject after receiving initiate |
| 239 | STATE_RECEIVEDREJECT, // received reject after sending initiate |
| 240 | STATE_SENTREDIRECT, // sent direct after receiving initiate |
| 241 | STATE_SENTTERMINATE, // sent terminate (any time / either side) |
| 242 | STATE_RECEIVEDTERMINATE, // received terminate (any time / either side) |
| 243 | STATE_INPROGRESS, // session accepted and in progress |
| 244 | STATE_DEINIT, // session is being destroyed |
| 245 | }; |
| 246 | |
| 247 | enum Error { |
| 248 | ERROR_NONE = 0, // no error |
| 249 | ERROR_TIME = 1, // no response to signaling |
| 250 | ERROR_RESPONSE = 2, // error during signaling |
| 251 | ERROR_NETWORK = 3, // network error, could not allocate network resources |
| 252 | ERROR_CONTENT = 4, // channel errors in SetLocalContent/SetRemoteContent |
| 253 | ERROR_TRANSPORT = 5, // transport error of some kind |
| 254 | }; |
| 255 | |
| 256 | // Convert State to a readable string. |
| 257 | static std::string StateToString(State state); |
| 258 | |
| 259 | BaseSession(talk_base::Thread* signaling_thread, |
| 260 | talk_base::Thread* worker_thread, |
| 261 | PortAllocator* port_allocator, |
| 262 | const std::string& sid, |
| 263 | const std::string& content_type, |
| 264 | bool initiator); |
| 265 | virtual ~BaseSession(); |
| 266 | |
| 267 | talk_base::Thread* signaling_thread() { return signaling_thread_; } |
| 268 | talk_base::Thread* worker_thread() { return worker_thread_; } |
| 269 | PortAllocator* port_allocator() { return port_allocator_; } |
| 270 | |
| 271 | // The ID of this session. |
| 272 | const std::string& id() const { return sid_; } |
| 273 | |
| 274 | // TODO(juberti): This data is largely redundant, as it can now be obtained |
| 275 | // from local/remote_description(). Remove these functions and members. |
| 276 | // Returns the XML namespace identifying the type of this session. |
| 277 | const std::string& content_type() const { return content_type_; } |
| 278 | // Returns the XML namespace identifying the transport used for this session. |
| 279 | const std::string& transport_type() const { return transport_type_; } |
| 280 | |
| 281 | // Indicates whether we initiated this session. |
| 282 | bool initiator() const { return initiator_; } |
| 283 | |
| 284 | // Returns the application-level description given by our client. |
| 285 | // If we are the recipient, this will be NULL until we send an accept. |
| 286 | const SessionDescription* local_description() const { |
| 287 | return local_description_; |
| 288 | } |
| 289 | // Returns the application-level description given by the other client. |
| 290 | // If we are the initiator, this will be NULL until we receive an accept. |
| 291 | const SessionDescription* remote_description() const { |
| 292 | return remote_description_; |
| 293 | } |
| 294 | SessionDescription* remote_description() { |
| 295 | return remote_description_; |
| 296 | } |
| 297 | |
| 298 | // Takes ownership of SessionDescription* |
| 299 | bool set_local_description(const SessionDescription* sdesc) { |
| 300 | if (sdesc != local_description_) { |
| 301 | delete local_description_; |
| 302 | local_description_ = sdesc; |
| 303 | } |
| 304 | return true; |
| 305 | } |
| 306 | |
| 307 | // Takes ownership of SessionDescription* |
| 308 | bool set_remote_description(SessionDescription* sdesc) { |
| 309 | if (sdesc != remote_description_) { |
| 310 | delete remote_description_; |
| 311 | remote_description_ = sdesc; |
| 312 | } |
| 313 | return true; |
| 314 | } |
| 315 | |
| 316 | const SessionDescription* initiator_description() const { |
| 317 | if (initiator_) { |
| 318 | return local_description_; |
| 319 | } else { |
| 320 | return remote_description_; |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | // Returns the current state of the session. See the enum above for details. |
| 325 | // Each time the state changes, we will fire this signal. |
| 326 | State state() const { return state_; } |
| 327 | sigslot::signal2<BaseSession* , State> SignalState; |
| 328 | |
| 329 | // Returns the last error in the session. See the enum above for details. |
| 330 | // Each time the an error occurs, we will fire this signal. |
| 331 | Error error() const { return error_; } |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame^] | 332 | const std::string& error_desc() const { return error_desc_; } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 333 | sigslot::signal2<BaseSession* , Error> SignalError; |
| 334 | |
| 335 | // Updates the state, signaling if necessary. |
| 336 | virtual void SetState(State state); |
| 337 | |
| 338 | // Updates the error state, signaling if necessary. |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame^] | 339 | // TODO(ronghuawu): remove the SetError method that doesn't take |error_desc|. |
| 340 | virtual void SetError(Error error, const std::string& error_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 341 | |
| 342 | // Fired when the remote description is updated, with the updated |
| 343 | // contents. |
| 344 | sigslot::signal2<BaseSession* , const ContentInfos&> |
| 345 | SignalRemoteDescriptionUpdate; |
| 346 | |
| 347 | // Fired when SetState is called (regardless if there's a state change), which |
| 348 | // indicates the session description might have be updated. |
| 349 | sigslot::signal2<BaseSession*, ContentAction> SignalNewLocalDescription; |
| 350 | |
| 351 | // Fired when SetState is called (regardless if there's a state change), which |
| 352 | // indicates the session description might have be updated. |
| 353 | sigslot::signal2<BaseSession*, ContentAction> SignalNewRemoteDescription; |
| 354 | |
| 355 | // Returns the transport that has been negotiated or NULL if |
| 356 | // negotiation is still in progress. |
wu@webrtc.org | 4551b79 | 2013-10-09 15:37:36 +0000 | [diff] [blame] | 357 | virtual Transport* GetTransport(const std::string& content_name); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 358 | |
| 359 | // Creates a new channel with the given names. This method may be called |
| 360 | // immediately after creating the session. However, the actual |
| 361 | // implementation may not be fixed until transport negotiation completes. |
| 362 | // This will usually be called from the worker thread, but that |
| 363 | // shouldn't be an issue since the main thread will be blocked in |
| 364 | // Send when doing so. |
| 365 | virtual TransportChannel* CreateChannel(const std::string& content_name, |
| 366 | const std::string& channel_name, |
| 367 | int component); |
| 368 | |
| 369 | // Returns the channel with the given names. |
| 370 | virtual TransportChannel* GetChannel(const std::string& content_name, |
| 371 | int component); |
| 372 | |
| 373 | // Destroys the channel with the given names. |
| 374 | // This will usually be called from the worker thread, but that |
| 375 | // shouldn't be an issue since the main thread will be blocked in |
| 376 | // Send when doing so. |
| 377 | virtual void DestroyChannel(const std::string& content_name, |
| 378 | int component); |
| 379 | |
| 380 | // Returns stats for all channels of all transports. |
| 381 | // This avoids exposing the internal structures used to track them. |
| 382 | virtual bool GetStats(SessionStats* stats); |
| 383 | |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 384 | talk_base::SSLIdentity* identity() { return identity_; } |
| 385 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 386 | protected: |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 387 | // Specifies the identity to use in this session. |
| 388 | bool SetIdentity(talk_base::SSLIdentity* identity); |
| 389 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 390 | bool PushdownTransportDescription(ContentSource source, |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame^] | 391 | ContentAction action, |
| 392 | std::string* error_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 393 | void set_initiator(bool initiator) { initiator_ = initiator; } |
| 394 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 395 | const TransportMap& transport_proxies() const { return transports_; } |
| 396 | // Get a TransportProxy by content_name or transport. NULL if not found. |
| 397 | TransportProxy* GetTransportProxy(const std::string& content_name); |
| 398 | TransportProxy* GetTransportProxy(const Transport* transport); |
| 399 | TransportProxy* GetFirstTransportProxy(); |
| 400 | void DestroyTransportProxy(const std::string& content_name); |
| 401 | // TransportProxy is owned by session. Return proxy just for convenience. |
| 402 | TransportProxy* GetOrCreateTransportProxy(const std::string& content_name); |
| 403 | // Creates the actual transport object. Overridable for testing. |
| 404 | virtual Transport* CreateTransport(const std::string& content_name); |
| 405 | |
| 406 | void OnSignalingReady(); |
| 407 | void SpeculativelyConnectAllTransportChannels(); |
| 408 | // Helper method to provide remote candidates to the transport. |
| 409 | bool OnRemoteCandidates(const std::string& content_name, |
| 410 | const Candidates& candidates, |
| 411 | std::string* error); |
| 412 | |
| 413 | // This method will mux transport channels by content_name. |
| 414 | // First content is used for muxing. |
| 415 | bool MaybeEnableMuxingSupport(); |
| 416 | |
| 417 | // Called when a transport requests signaling. |
| 418 | virtual void OnTransportRequestSignaling(Transport* transport) { |
| 419 | } |
| 420 | |
| 421 | // Called when the first channel of a transport begins connecting. We use |
| 422 | // this to start a timer, to make sure that the connection completes in a |
| 423 | // reasonable amount of time. |
| 424 | virtual void OnTransportConnecting(Transport* transport) { |
| 425 | } |
| 426 | |
| 427 | // Called when a transport changes its writable state. We track this to make |
| 428 | // sure that the transport becomes writable within a reasonable amount of |
| 429 | // time. If this does not occur, we signal an error. |
| 430 | virtual void OnTransportWritable(Transport* transport) { |
| 431 | } |
| 432 | virtual void OnTransportReadable(Transport* transport) { |
| 433 | } |
| 434 | |
| 435 | // Called when a transport signals that it has new candidates. |
| 436 | virtual void OnTransportProxyCandidatesReady(TransportProxy* proxy, |
| 437 | const Candidates& candidates) { |
| 438 | } |
| 439 | |
| 440 | // Called when a transport signals that it found an error in an incoming |
| 441 | // message. |
| 442 | virtual void OnTransportSendError(Transport* transport, |
| 443 | const buzz::XmlElement* stanza, |
| 444 | const buzz::QName& name, |
| 445 | const std::string& type, |
| 446 | const std::string& text, |
| 447 | const buzz::XmlElement* extra_info) { |
| 448 | } |
| 449 | |
| 450 | virtual void OnTransportRouteChange( |
| 451 | Transport* transport, |
| 452 | int component, |
| 453 | const cricket::Candidate& remote_candidate) { |
| 454 | } |
| 455 | |
| 456 | virtual void OnTransportCandidatesAllocationDone(Transport* transport); |
| 457 | |
| 458 | // Called when all transport channels allocated required candidates. |
| 459 | // This method should be used as an indication of candidates gathering process |
| 460 | // is completed and application can now send local candidates list to remote. |
| 461 | virtual void OnCandidatesAllocationDone() { |
| 462 | } |
| 463 | |
| 464 | // Handles the ice role change callback from Transport. This must be |
| 465 | // propagated to all the transports. |
| 466 | virtual void OnRoleConflict(); |
| 467 | |
| 468 | // Handles messages posted to us. |
| 469 | virtual void OnMessage(talk_base::Message *pmsg); |
| 470 | |
| 471 | protected: |
| 472 | State state_; |
| 473 | Error error_; |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame^] | 474 | std::string error_desc_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 475 | |
| 476 | private: |
| 477 | // Helper methods to push local and remote transport descriptions. |
| 478 | bool PushdownLocalTransportDescription( |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame^] | 479 | const SessionDescription* sdesc, ContentAction action, |
| 480 | std::string* error_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 481 | bool PushdownRemoteTransportDescription( |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame^] | 482 | const SessionDescription* sdesc, ContentAction action, |
| 483 | std::string* error_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 484 | |
| 485 | bool IsCandidateAllocationDone() const; |
| 486 | void MaybeCandidateAllocationDone(); |
| 487 | |
| 488 | // This method will delete the Transport and TransportChannelImpls and |
| 489 | // replace those with the selected Transport objects. Selection is done |
| 490 | // based on the content_name and in this case first MediaContent information |
| 491 | // is used for mux. |
| 492 | bool SetSelectedProxy(const std::string& content_name, |
| 493 | const ContentGroup* muxed_group); |
| 494 | // Log session state. |
| 495 | void LogState(State old_state, State new_state); |
| 496 | |
| 497 | // Returns true and the TransportInfo of the given |content_name| |
| 498 | // from |description|. Returns false if it's not available. |
| 499 | bool GetTransportDescription(const SessionDescription* description, |
| 500 | const std::string& content_name, |
| 501 | TransportDescription* info); |
| 502 | |
| 503 | // Fires the new description signal according to the current state. |
| 504 | void SignalNewDescription(); |
| 505 | |
| 506 | // Gets the ContentAction and ContentSource according to the session state. |
| 507 | bool GetContentAction(ContentAction* action, ContentSource* source); |
| 508 | |
| 509 | talk_base::Thread* signaling_thread_; |
| 510 | talk_base::Thread* worker_thread_; |
| 511 | PortAllocator* port_allocator_; |
| 512 | std::string sid_; |
| 513 | std::string content_type_; |
| 514 | std::string transport_type_; |
| 515 | bool initiator_; |
| 516 | talk_base::SSLIdentity* identity_; |
| 517 | const SessionDescription* local_description_; |
| 518 | SessionDescription* remote_description_; |
| 519 | uint64 ice_tiebreaker_; |
| 520 | // This flag will be set to true after the first role switch. This flag |
| 521 | // will enable us to stop any role switch during the call. |
| 522 | bool role_switch_; |
| 523 | TransportMap transports_; |
| 524 | }; |
| 525 | |
| 526 | // A specific Session created by the SessionManager, using XMPP for protocol. |
| 527 | class Session : public BaseSession { |
| 528 | public: |
| 529 | // Returns the manager that created and owns this session. |
| 530 | SessionManager* session_manager() const { return session_manager_; } |
| 531 | |
| 532 | // Returns the client that is handling the application data of this session. |
| 533 | SessionClient* client() const { return client_; } |
| 534 | |
| 535 | // Returns the JID of this client. |
| 536 | const std::string& local_name() const { return local_name_; } |
| 537 | |
| 538 | // Returns the JID of the other peer in this session. |
| 539 | const std::string& remote_name() const { return remote_name_; } |
| 540 | |
| 541 | // Set the JID of the other peer in this session. |
| 542 | // Typically the remote_name_ is set when the session is initiated. |
| 543 | // However, sometimes (e.g when a proxy is used) the peer name is |
| 544 | // known after the BaseSession has been initiated and it must be updated |
| 545 | // explicitly. |
| 546 | void set_remote_name(const std::string& name) { remote_name_ = name; } |
| 547 | |
| 548 | // Set the JID of the initiator of this session. Allows for the overriding |
| 549 | // of the initiator to be a third-party, eg. the MUC JID when creating p2p |
| 550 | // sessions. |
| 551 | void set_initiator_name(const std::string& name) { initiator_name_ = name; } |
| 552 | |
| 553 | // Indicates the JID of the entity who initiated this session. |
| 554 | // In special cases, may be different than both local_name and remote_name. |
| 555 | const std::string& initiator_name() const { return initiator_name_; } |
| 556 | |
| 557 | SignalingProtocol current_protocol() const { return current_protocol_; } |
| 558 | |
| 559 | void set_current_protocol(SignalingProtocol protocol) { |
| 560 | current_protocol_ = protocol; |
| 561 | } |
| 562 | |
| 563 | // Updates the error state, signaling if necessary. |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame^] | 564 | virtual void SetError(Error error, const std::string& error_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 565 | |
| 566 | // When the session needs to send signaling messages, it beings by requesting |
| 567 | // signaling. The client should handle this by calling OnSignalingReady once |
| 568 | // it is ready to send the messages. |
| 569 | // (These are called only by SessionManager.) |
| 570 | sigslot::signal1<Session*> SignalRequestSignaling; |
| 571 | void OnSignalingReady() { BaseSession::OnSignalingReady(); } |
| 572 | |
| 573 | // Takes ownership of session description. |
| 574 | // TODO: Add an error argument to pass back to the caller. |
| 575 | bool Initiate(const std::string& to, |
| 576 | const SessionDescription* sdesc); |
| 577 | |
| 578 | // When we receive an initiate, we create a session in the |
| 579 | // RECEIVEDINITIATE state and respond by accepting or rejecting. |
| 580 | // Takes ownership of session description. |
| 581 | // TODO: Add an error argument to pass back to the caller. |
| 582 | bool Accept(const SessionDescription* sdesc); |
| 583 | bool Reject(const std::string& reason); |
| 584 | bool Terminate() { |
| 585 | return TerminateWithReason(STR_TERMINATE_SUCCESS); |
| 586 | } |
| 587 | bool TerminateWithReason(const std::string& reason); |
| 588 | // Fired whenever we receive a terminate message along with a reason |
| 589 | sigslot::signal2<Session*, const std::string&> SignalReceivedTerminateReason; |
| 590 | |
| 591 | // The two clients in the session may also send one another |
| 592 | // arbitrary XML messages, which are called "info" messages. Sending |
| 593 | // takes ownership of the given elements. The signal does not; the |
| 594 | // parent element will be deleted after the signal. |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 595 | bool SendInfoMessage(const XmlElements& elems, |
| 596 | const std::string& remote_name); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 597 | bool SendDescriptionInfoMessage(const ContentInfos& contents); |
| 598 | sigslot::signal2<Session*, const buzz::XmlElement*> SignalInfoMessage; |
| 599 | |
| 600 | private: |
| 601 | // Creates or destroys a session. (These are called only SessionManager.) |
| 602 | Session(SessionManager *session_manager, |
| 603 | const std::string& local_name, const std::string& initiator_name, |
| 604 | const std::string& sid, const std::string& content_type, |
| 605 | SessionClient* client); |
| 606 | ~Session(); |
| 607 | // For each transport info, create a transport proxy. Can fail for |
| 608 | // incompatible transport types. |
| 609 | bool CreateTransportProxies(const TransportInfos& tinfos, |
| 610 | SessionError* error); |
| 611 | bool OnRemoteCandidates(const TransportInfos& tinfos, |
| 612 | ParseError* error); |
| 613 | // Returns a TransportInfo without candidates for each content name. |
| 614 | // Uses the transport_type_ of the session. |
| 615 | TransportInfos GetEmptyTransportInfos(const ContentInfos& contents) const; |
| 616 | |
| 617 | // Maps passed to serialization functions. |
| 618 | TransportParserMap GetTransportParsers(); |
| 619 | ContentParserMap GetContentParsers(); |
| 620 | CandidateTranslatorMap GetCandidateTranslators(); |
| 621 | |
| 622 | virtual void OnTransportRequestSignaling(Transport* transport); |
| 623 | virtual void OnTransportConnecting(Transport* transport); |
| 624 | virtual void OnTransportWritable(Transport* transport); |
| 625 | virtual void OnTransportProxyCandidatesReady(TransportProxy* proxy, |
| 626 | const Candidates& candidates); |
| 627 | virtual void OnTransportSendError(Transport* transport, |
| 628 | const buzz::XmlElement* stanza, |
| 629 | const buzz::QName& name, |
| 630 | const std::string& type, |
| 631 | const std::string& text, |
| 632 | const buzz::XmlElement* extra_info); |
| 633 | virtual void OnMessage(talk_base::Message *pmsg); |
| 634 | |
| 635 | // Send various kinds of session messages. |
| 636 | bool SendInitiateMessage(const SessionDescription* sdesc, |
| 637 | SessionError* error); |
| 638 | bool SendAcceptMessage(const SessionDescription* sdesc, SessionError* error); |
| 639 | bool SendRejectMessage(const std::string& reason, SessionError* error); |
| 640 | bool SendTerminateMessage(const std::string& reason, SessionError* error); |
| 641 | bool SendTransportInfoMessage(const TransportInfo& tinfo, |
| 642 | SessionError* error); |
| 643 | bool SendTransportInfoMessage(const TransportProxy* transproxy, |
| 644 | const Candidates& candidates, |
| 645 | SessionError* error); |
| 646 | |
| 647 | bool ResendAllTransportInfoMessages(SessionError* error); |
| 648 | bool SendAllUnsentTransportInfoMessages(SessionError* error); |
| 649 | |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 650 | // All versions of SendMessage send a message of the given type to |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 651 | // the other client. Can pass either a set of elements or an |
| 652 | // "action", which must have a WriteSessionAction method to go along |
| 653 | // with it. Sending with an action supports sending a "hybrid" |
| 654 | // message. Sending with elements must be sent as Jingle or Gingle. |
| 655 | |
| 656 | // When passing elems, must be either Jingle or Gingle protocol. |
| 657 | // Takes ownership of action_elems. |
| 658 | bool SendMessage(ActionType type, const XmlElements& action_elems, |
| 659 | SessionError* error); |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 660 | // Sends a messge, but overrides the remote name. |
| 661 | bool SendMessage(ActionType type, const XmlElements& action_elems, |
| 662 | const std::string& remote_name, |
| 663 | SessionError* error); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 664 | // When passing an action, may be Hybrid protocol. |
| 665 | template <typename Action> |
| 666 | bool SendMessage(ActionType type, const Action& action, |
| 667 | SessionError* error); |
| 668 | |
| 669 | // Helper methods to write the session message stanza. |
| 670 | template <typename Action> |
| 671 | bool WriteActionMessage(ActionType type, const Action& action, |
| 672 | buzz::XmlElement* stanza, WriteError* error); |
| 673 | template <typename Action> |
| 674 | bool WriteActionMessage(SignalingProtocol protocol, |
| 675 | ActionType type, const Action& action, |
| 676 | buzz::XmlElement* stanza, WriteError* error); |
| 677 | |
| 678 | // Sending messages in hybrid form requires being able to write them |
| 679 | // on a per-protocol basis with a common method signature, which all |
| 680 | // of these have. |
| 681 | bool WriteSessionAction(SignalingProtocol protocol, |
| 682 | const SessionInitiate& init, |
| 683 | XmlElements* elems, WriteError* error); |
| 684 | bool WriteSessionAction(SignalingProtocol protocol, |
| 685 | const TransportInfo& tinfo, |
| 686 | XmlElements* elems, WriteError* error); |
| 687 | bool WriteSessionAction(SignalingProtocol protocol, |
| 688 | const SessionTerminate& term, |
| 689 | XmlElements* elems, WriteError* error); |
| 690 | |
| 691 | // Sends a message back to the other client indicating that we have received |
| 692 | // and accepted their message. |
| 693 | void SendAcknowledgementMessage(const buzz::XmlElement* stanza); |
| 694 | |
| 695 | // Once signaling is ready, the session will use this signal to request the |
| 696 | // sending of each message. When messages are received by the other client, |
| 697 | // they should be handed to OnIncomingMessage. |
| 698 | // (These are called only by SessionManager.) |
| 699 | sigslot::signal2<Session* , const buzz::XmlElement*> SignalOutgoingMessage; |
| 700 | void OnIncomingMessage(const SessionMessage& msg); |
| 701 | |
| 702 | void OnIncomingResponse(const buzz::XmlElement* orig_stanza, |
| 703 | const buzz::XmlElement* response_stanza, |
| 704 | const SessionMessage& msg); |
| 705 | void OnInitiateAcked(); |
| 706 | void OnFailedSend(const buzz::XmlElement* orig_stanza, |
| 707 | const buzz::XmlElement* error_stanza); |
| 708 | |
| 709 | // Invoked when an error is found in an incoming message. This is translated |
| 710 | // into the appropriate XMPP response by SessionManager. |
| 711 | sigslot::signal6<BaseSession*, |
| 712 | const buzz::XmlElement*, |
| 713 | const buzz::QName&, |
| 714 | const std::string&, |
| 715 | const std::string&, |
| 716 | const buzz::XmlElement*> SignalErrorMessage; |
| 717 | |
| 718 | // Handlers for the various types of messages. These functions may take |
| 719 | // pointers to the whole stanza or to just the session element. |
| 720 | bool OnInitiateMessage(const SessionMessage& msg, MessageError* error); |
| 721 | bool OnAcceptMessage(const SessionMessage& msg, MessageError* error); |
| 722 | bool OnRejectMessage(const SessionMessage& msg, MessageError* error); |
| 723 | bool OnInfoMessage(const SessionMessage& msg); |
| 724 | bool OnTerminateMessage(const SessionMessage& msg, MessageError* error); |
| 725 | bool OnTransportInfoMessage(const SessionMessage& msg, MessageError* error); |
| 726 | bool OnTransportAcceptMessage(const SessionMessage& msg, MessageError* error); |
| 727 | bool OnDescriptionInfoMessage(const SessionMessage& msg, MessageError* error); |
| 728 | bool OnRedirectError(const SessionRedirect& redirect, SessionError* error); |
| 729 | |
| 730 | // Verifies that we are in the appropriate state to receive this message. |
| 731 | bool CheckState(State state, MessageError* error); |
| 732 | |
| 733 | SessionManager* session_manager_; |
| 734 | bool initiate_acked_; |
| 735 | std::string local_name_; |
| 736 | std::string initiator_name_; |
| 737 | std::string remote_name_; |
| 738 | SessionClient* client_; |
| 739 | TransportParser* transport_parser_; |
| 740 | // Keeps track of what protocol we are speaking. |
| 741 | SignalingProtocol current_protocol_; |
| 742 | |
| 743 | friend class SessionManager; // For access to constructor, destructor, |
| 744 | // and signaling related methods. |
| 745 | }; |
| 746 | |
| 747 | } // namespace cricket |
| 748 | |
| 749 | #endif // TALK_P2P_BASE_SESSION_H_ |