henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 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 |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #ifndef WEBRTC_P2P_BASE_SESSION_H_ |
| 12 | #define WEBRTC_P2P_BASE_SESSION_H_ |
| 13 | |
| 14 | #include <list> |
| 15 | #include <map> |
| 16 | #include <string> |
| 17 | #include <vector> |
| 18 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 19 | #include "webrtc/base/refcount.h" |
Henrik Boström | d828198 | 2015-08-27 10:12:24 +0200 | [diff] [blame] | 20 | #include "webrtc/base/rtccertificate.h" |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 21 | #include "webrtc/base/scoped_ptr.h" |
| 22 | #include "webrtc/base/scoped_ref_ptr.h" |
| 23 | #include "webrtc/base/socketaddress.h" |
deadbeef | 47ee2f3 | 2015-09-22 15:08:23 -0700 | [diff] [blame^] | 24 | #include "webrtc/p2p/base/candidate.h" |
| 25 | #include "webrtc/p2p/base/port.h" |
| 26 | #include "webrtc/p2p/base/transport.h" |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 27 | |
| 28 | namespace cricket { |
| 29 | |
| 30 | class BaseSession; |
| 31 | class P2PTransportChannel; |
| 32 | class Transport; |
| 33 | class TransportChannel; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 34 | class TransportChannelImpl; |
deadbeef | 47ee2f3 | 2015-09-22 15:08:23 -0700 | [diff] [blame^] | 35 | class TransportController; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 36 | |
| 37 | // Statistics for all the transports of this session. |
| 38 | typedef std::map<std::string, TransportStats> TransportStatsMap; |
| 39 | typedef std::map<std::string, std::string> ProxyTransportMap; |
| 40 | |
pthatcher@webrtc.org | c04a97f | 2015-03-16 19:31:40 +0000 | [diff] [blame] | 41 | // TODO(pthatcher): Think of a better name for this. We already have |
| 42 | // a TransportStats in transport.h. Perhaps TransportsStats? |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 43 | struct SessionStats { |
| 44 | ProxyTransportMap proxy_to_transport; |
| 45 | TransportStatsMap transport_stats; |
| 46 | }; |
| 47 | |
| 48 | // A BaseSession manages general session state. This includes negotiation |
| 49 | // of both the application-level and network-level protocols: the former |
| 50 | // defines what will be sent and the latter defines how it will be sent. Each |
| 51 | // network-level protocol is represented by a Transport object. Each Transport |
| 52 | // participates in the network-level negotiation. The individual streams of |
| 53 | // packets are represented by TransportChannels. The application-level protocol |
| 54 | // is represented by SessionDecription objects. |
| 55 | class BaseSession : public sigslot::has_slots<>, |
| 56 | public rtc::MessageHandler { |
| 57 | public: |
| 58 | enum { |
| 59 | MSG_TIMEOUT = 0, |
| 60 | MSG_ERROR, |
| 61 | MSG_STATE, |
| 62 | }; |
| 63 | |
| 64 | enum State { |
| 65 | STATE_INIT = 0, |
| 66 | STATE_SENTINITIATE, // sent initiate, waiting for Accept or Reject |
| 67 | STATE_RECEIVEDINITIATE, // received an initiate. Call Accept or Reject |
| 68 | STATE_SENTPRACCEPT, // sent provisional Accept |
| 69 | STATE_SENTACCEPT, // sent accept. begin connecting transport |
| 70 | STATE_RECEIVEDPRACCEPT, // received provisional Accept, waiting for Accept |
| 71 | STATE_RECEIVEDACCEPT, // received accept. begin connecting transport |
| 72 | STATE_SENTMODIFY, // sent modify, waiting for Accept or Reject |
| 73 | STATE_RECEIVEDMODIFY, // received modify, call Accept or Reject |
| 74 | STATE_SENTREJECT, // sent reject after receiving initiate |
| 75 | STATE_RECEIVEDREJECT, // received reject after sending initiate |
| 76 | STATE_SENTREDIRECT, // sent direct after receiving initiate |
| 77 | STATE_SENTTERMINATE, // sent terminate (any time / either side) |
| 78 | STATE_RECEIVEDTERMINATE, // received terminate (any time / either side) |
| 79 | STATE_INPROGRESS, // session accepted and in progress |
| 80 | STATE_DEINIT, // session is being destroyed |
| 81 | }; |
| 82 | |
| 83 | enum Error { |
| 84 | ERROR_NONE = 0, // no error |
| 85 | ERROR_TIME = 1, // no response to signaling |
| 86 | ERROR_RESPONSE = 2, // error during signaling |
| 87 | ERROR_NETWORK = 3, // network error, could not allocate network resources |
| 88 | ERROR_CONTENT = 4, // channel errors in SetLocalContent/SetRemoteContent |
| 89 | ERROR_TRANSPORT = 5, // transport error of some kind |
| 90 | }; |
| 91 | |
| 92 | // Convert State to a readable string. |
| 93 | static std::string StateToString(State state); |
| 94 | |
| 95 | BaseSession(rtc::Thread* signaling_thread, |
| 96 | rtc::Thread* worker_thread, |
| 97 | PortAllocator* port_allocator, |
| 98 | const std::string& sid, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 99 | bool initiator); |
| 100 | virtual ~BaseSession(); |
| 101 | |
| 102 | // These are const to allow them to be called from const methods. |
| 103 | rtc::Thread* signaling_thread() const { return signaling_thread_; } |
| 104 | rtc::Thread* worker_thread() const { return worker_thread_; } |
| 105 | PortAllocator* port_allocator() const { return port_allocator_; } |
| 106 | |
| 107 | // The ID of this session. |
| 108 | const std::string& id() const { return sid_; } |
| 109 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 110 | // Returns the application-level description given by our client. |
| 111 | // If we are the recipient, this will be NULL until we send an accept. |
| 112 | const SessionDescription* local_description() const; |
| 113 | |
| 114 | // Returns the application-level description given by the other client. |
| 115 | // If we are the initiator, this will be NULL until we receive an accept. |
| 116 | const SessionDescription* remote_description() const; |
| 117 | |
| 118 | SessionDescription* remote_description(); |
| 119 | |
| 120 | // Takes ownership of SessionDescription* |
| 121 | void set_local_description(const SessionDescription* sdesc); |
| 122 | |
| 123 | // Takes ownership of SessionDescription* |
| 124 | void set_remote_description(SessionDescription* sdesc); |
| 125 | |
deadbeef | 47ee2f3 | 2015-09-22 15:08:23 -0700 | [diff] [blame^] | 126 | void set_initiator(bool initiator); |
| 127 | bool initiator() const { return initiator_; } |
| 128 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 129 | const SessionDescription* initiator_description() const; |
| 130 | |
| 131 | // Returns the current state of the session. See the enum above for details. |
| 132 | // Each time the state changes, we will fire this signal. |
| 133 | State state() const { return state_; } |
| 134 | sigslot::signal2<BaseSession* , State> SignalState; |
| 135 | |
| 136 | // Returns the last error in the session. See the enum above for details. |
| 137 | // Each time the an error occurs, we will fire this signal. |
| 138 | Error error() const { return error_; } |
| 139 | const std::string& error_desc() const { return error_desc_; } |
| 140 | sigslot::signal2<BaseSession* , Error> SignalError; |
| 141 | |
| 142 | // Updates the state, signaling if necessary. |
| 143 | virtual void SetState(State state); |
| 144 | |
| 145 | // Updates the error state, signaling if necessary. |
| 146 | // TODO(ronghuawu): remove the SetError method that doesn't take |error_desc|. |
| 147 | virtual void SetError(Error error, const std::string& error_desc); |
| 148 | |
honghaiz | 9009962 | 2015-07-13 12:19:33 -0700 | [diff] [blame] | 149 | void SetIceConnectionReceivingTimeout(int timeout_ms); |
| 150 | |
deadbeef | 47ee2f3 | 2015-09-22 15:08:23 -0700 | [diff] [blame^] | 151 | // Start gathering candidates for any new transports, or transports doing an |
| 152 | // ICE restart. |
| 153 | void MaybeStartGathering(); |
Guo-wei Shieh | 8902433 | 2015-09-18 13:50:22 -0700 | [diff] [blame] | 154 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 155 | protected: |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 156 | bool PushdownTransportDescription(ContentSource source, |
| 157 | ContentAction action, |
| 158 | std::string* error_desc); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 159 | |
| 160 | // Handles messages posted to us. |
| 161 | virtual void OnMessage(rtc::Message *pmsg); |
| 162 | |
deadbeef | 47ee2f3 | 2015-09-22 15:08:23 -0700 | [diff] [blame^] | 163 | TransportController* transport_controller() { |
| 164 | return transport_controller_.get(); |
| 165 | } |
Guo-wei Shieh | 8902433 | 2015-09-18 13:50:22 -0700 | [diff] [blame] | 166 | |
deadbeef | 47ee2f3 | 2015-09-22 15:08:23 -0700 | [diff] [blame^] | 167 | protected: |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 168 | State state_; |
| 169 | Error error_; |
| 170 | std::string error_desc_; |
| 171 | |
| 172 | private: |
| 173 | // Helper methods to push local and remote transport descriptions. |
| 174 | bool PushdownLocalTransportDescription( |
| 175 | const SessionDescription* sdesc, ContentAction action, |
| 176 | std::string* error_desc); |
| 177 | bool PushdownRemoteTransportDescription( |
| 178 | const SessionDescription* sdesc, ContentAction action, |
| 179 | std::string* error_desc); |
| 180 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 181 | // Log session state. |
| 182 | void LogState(State old_state, State new_state); |
| 183 | |
| 184 | // Returns true and the TransportInfo of the given |content_name| |
| 185 | // from |description|. Returns false if it's not available. |
| 186 | static bool GetTransportDescription(const SessionDescription* description, |
| 187 | const std::string& content_name, |
| 188 | TransportDescription* info); |
| 189 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 190 | rtc::Thread* const signaling_thread_; |
| 191 | rtc::Thread* const worker_thread_; |
| 192 | PortAllocator* const port_allocator_; |
| 193 | const std::string sid_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 194 | bool initiator_; |
deadbeef | 47ee2f3 | 2015-09-22 15:08:23 -0700 | [diff] [blame^] | 195 | rtc::scoped_ptr<TransportController> transport_controller_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 196 | rtc::scoped_ptr<const SessionDescription> local_description_; |
| 197 | rtc::scoped_ptr<SessionDescription> remote_description_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 198 | }; |
| 199 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 200 | } // namespace cricket |
| 201 | |
| 202 | #endif // WEBRTC_P2P_BASE_SESSION_H_ |