10e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org/*
20e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * libjingle
30e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * Copyright 2004--2005, Google Inc.
40e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *
50e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * Redistribution and use in source and binary forms, with or without
60e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * modification, are permitted provided that the following conditions are met:
70e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *
80e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *  1. Redistributions of source code must retain the above copyright notice,
90e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *     this list of conditions and the following disclaimer.
100e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *  2. Redistributions in binary form must reproduce the above copyright notice,
110e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *     this list of conditions and the following disclaimer in the documentation
120e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *     and/or other materials provided with the distribution.
130e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *  3. The name of the author may not be used to endorse or promote products
140e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *     derived from this software without specific prior written permission.
150e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *
160e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
170e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
180e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
190e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
200e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
210e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
220e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
230e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
240e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
250e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
260e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org */
270e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
280e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#ifndef TALK_P2P_BASE_SESSION_H_
290e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#define TALK_P2P_BASE_SESSION_H_
300e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
310e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include <list>
320e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include <map>
330e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include <string>
340e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include <vector>
350e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
360e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include "talk/p2p/base/parsing.h"
370e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include "talk/p2p/base/port.h"
380e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include "talk/p2p/base/sessionclient.h"
390e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include "talk/p2p/base/sessionmanager.h"
400e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include "talk/p2p/base/sessionmessages.h"
410e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include "talk/p2p/base/transport.h"
4213b2d035e2e7f2f18e3a4d3377bc1a09f43a4ff9buildbot@webrtc.org#include "webrtc/libjingle/xmllite/xmlelement.h"
430e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include "talk/xmpp/constants.h"
44cf81adffe15fa8ea0f333432e41f6d504148f18abuildbot@webrtc.org#include "webrtc/base/refcount.h"
45cf81adffe15fa8ea0f333432e41f6d504148f18abuildbot@webrtc.org#include "webrtc/base/scoped_ptr.h"
46cf81adffe15fa8ea0f333432e41f6d504148f18abuildbot@webrtc.org#include "webrtc/base/scoped_ref_ptr.h"
47cf81adffe15fa8ea0f333432e41f6d504148f18abuildbot@webrtc.org#include "webrtc/base/socketaddress.h"
480e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
490e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgnamespace cricket {
500e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
510e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgclass BaseSession;
520e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgclass P2PTransportChannel;
530e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgclass Transport;
540e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgclass TransportChannel;
550e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgclass TransportChannelProxy;
560e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgclass TransportChannelImpl;
570e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
582a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.orgtypedef rtc::RefCountedObject<rtc::scoped_ptr<Transport> >
590e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgTransportWrapper;
600e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
610e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// Used for errors that will send back a specific error message to the
620e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// remote peer.  We add "type" to the errors because it's needed for
630e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// SignalErrorMessage.
640e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgstruct MessageError : ParseError {
650e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  buzz::QName type;
660e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
670e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // if unset, assume type is a parse error
680e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  MessageError() : ParseError(), type(buzz::QN_STANZA_BAD_REQUEST) {}
690e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
700e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  void SetType(const buzz::QName type) {
710e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    this->type = type;
720e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
730e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org};
740e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
750e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// Used for errors that may be returned by public session methods that
760e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// can fail.
770e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// TODO: Use this error in Session::Initiate and
780e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// Session::Accept.
790e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgstruct SessionError : WriteError {
800e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org};
810e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
820e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// Bundles a Transport and ChannelMap together. ChannelMap is used to
830e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// create transport channels before receiving or sending a session
840e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// initiate, and for speculatively connecting channels.  Previously, a
850e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// session had one ChannelMap and transport.  Now, with multiple
860e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// transports per session, we need multiple ChannelMaps as well.
870e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
880e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgtypedef std::map<int, TransportChannelProxy*> ChannelMap;
890e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
900e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgclass TransportProxy : public sigslot::has_slots<>,
910e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                       public CandidateTranslator {
920e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org public:
930e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  TransportProxy(
942a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org      rtc::Thread* worker_thread,
950e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      const std::string& sid,
960e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      const std::string& content_name,
970e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      TransportWrapper* transport)
985528070a0c76057a000b877fc56ca4180ad2087bmallinath@webrtc.org      : worker_thread_(worker_thread),
995528070a0c76057a000b877fc56ca4180ad2087bmallinath@webrtc.org        sid_(sid),
1000e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org        content_name_(content_name),
1010e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org        transport_(transport),
1020e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org        connecting_(false),
1030e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org        negotiated_(false),
1040e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org        sent_candidates_(false),
105f72f5a64fe04356b56ddfb84c6ed488d61ced7c4buildbot@webrtc.org        candidates_allocated_(false),
106f72f5a64fe04356b56ddfb84c6ed488d61ced7c4buildbot@webrtc.org        local_description_set_(false),
107f72f5a64fe04356b56ddfb84c6ed488d61ced7c4buildbot@webrtc.org        remote_description_set_(false) {
1080e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    transport_->get()->SignalCandidatesReady.connect(
1090e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org        this, &TransportProxy::OnTransportCandidatesReady);
1100e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
1110e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  ~TransportProxy();
1120e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
11332a0ac7e336c6132d7a48aa08fe2c912c0eb749etommi@webrtc.org  const std::string& content_name() const { return content_name_; }
1140e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // TODO(juberti): It's not good form to expose the object you're wrapping,
1150e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // since callers can mutate it. Can we make this return a const Transport*?
1160e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  Transport* impl() const { return transport_->get(); }
1170e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
11832a0ac7e336c6132d7a48aa08fe2c912c0eb749etommi@webrtc.org  const std::string& type() const;
1190e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bool negotiated() const { return negotiated_; }
1200e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  const Candidates& sent_candidates() const { return sent_candidates_; }
1210e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  const Candidates& unsent_candidates() const { return unsent_candidates_; }
1220e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bool candidates_allocated() const { return candidates_allocated_; }
1230e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  void set_candidates_allocated(bool allocated) {
1240e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    candidates_allocated_ = allocated;
1250e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
1260e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1270e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  TransportChannel* GetChannel(int component);
1280e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  TransportChannel* CreateChannel(const std::string& channel_name,
1290e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                  int component);
1300e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bool HasChannel(int component);
1310e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  void DestroyChannel(int component);
1320e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1330e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  void AddSentCandidates(const Candidates& candidates);
1340e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  void AddUnsentCandidates(const Candidates& candidates);
1350e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  void ClearSentCandidates() { sent_candidates_.clear(); }
1360e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  void ClearUnsentCandidates() { unsent_candidates_.clear(); }
1370e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1380e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Start the connection process for any channels, creating impls if needed.
1390e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  void ConnectChannels();
1400e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Hook up impls to the proxy channels. Doesn't change connect state.
1410e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  void CompleteNegotiation();
1420e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1430e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Mux this proxy onto the specified proxy's transport.
1440e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bool SetupMux(TransportProxy* proxy);
1450e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1460e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Simple functions that thunk down to the same functions on Transport.
147e560834da4ee5a5f38a96a8cb9290c5ce1096989mallinath@webrtc.org  void SetIceRole(IceRole role);
1482a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  void SetIdentity(rtc::SSLIdentity* identity);
1490e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bool SetLocalTransportDescription(const TransportDescription& description,
150f32dd31e14521d7f845e7776af6d44d411573370sergeyu@chromium.org                                    ContentAction action,
151f32dd31e14521d7f845e7776af6d44d411573370sergeyu@chromium.org                                    std::string* error_desc);
1520e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bool SetRemoteTransportDescription(const TransportDescription& description,
153f32dd31e14521d7f845e7776af6d44d411573370sergeyu@chromium.org                                     ContentAction action,
154f32dd31e14521d7f845e7776af6d44d411573370sergeyu@chromium.org                                     std::string* error_desc);
1550e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  void OnSignalingReady();
1560e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bool OnRemoteCandidates(const Candidates& candidates, std::string* error);
1570e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1580e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // CandidateTranslator methods.
1590e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual bool GetChannelNameFromComponent(
1600e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      int component, std::string* channel_name) const;
1610e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual bool GetComponentFromChannelName(
1620e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      const std::string& channel_name, int* component) const;
1630e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1640e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Called when a transport signals that it has new candidates.
1650e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  void OnTransportCandidatesReady(cricket::Transport* transport,
1660e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                  const Candidates& candidates) {
1670e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    SignalCandidatesReady(this, candidates);
1680e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
1690e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
170f72f5a64fe04356b56ddfb84c6ed488d61ced7c4buildbot@webrtc.org  bool local_description_set() const {
171f72f5a64fe04356b56ddfb84c6ed488d61ced7c4buildbot@webrtc.org    return local_description_set_;
172f72f5a64fe04356b56ddfb84c6ed488d61ced7c4buildbot@webrtc.org  }
173f72f5a64fe04356b56ddfb84c6ed488d61ced7c4buildbot@webrtc.org  bool remote_description_set() const {
174f72f5a64fe04356b56ddfb84c6ed488d61ced7c4buildbot@webrtc.org    return remote_description_set_;
175f72f5a64fe04356b56ddfb84c6ed488d61ced7c4buildbot@webrtc.org  }
176f72f5a64fe04356b56ddfb84c6ed488d61ced7c4buildbot@webrtc.org
1770e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Handles sending of ready candidates and receiving of remote candidates.
1780e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  sigslot::signal2<TransportProxy*,
1790e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                         const std::vector<Candidate>&> SignalCandidatesReady;
1800e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1810e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org private:
1820e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  TransportChannelProxy* GetChannelProxy(int component) const;
1830e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  TransportChannelProxy* GetChannelProxyByName(const std::string& name) const;
1845528070a0c76057a000b877fc56ca4180ad2087bmallinath@webrtc.org
1850e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  TransportChannelImpl* GetOrCreateChannelProxyImpl(int component);
1865528070a0c76057a000b877fc56ca4180ad2087bmallinath@webrtc.org  TransportChannelImpl* GetOrCreateChannelProxyImpl_w(int component);
1870e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1885528070a0c76057a000b877fc56ca4180ad2087bmallinath@webrtc.org  // Manipulators of transportchannelimpl in channel proxy.
1895528070a0c76057a000b877fc56ca4180ad2087bmallinath@webrtc.org  void SetupChannelProxy(int component,
1905528070a0c76057a000b877fc56ca4180ad2087bmallinath@webrtc.org                           TransportChannelProxy* proxy);
1915528070a0c76057a000b877fc56ca4180ad2087bmallinath@webrtc.org  void SetupChannelProxy_w(int component,
1925528070a0c76057a000b877fc56ca4180ad2087bmallinath@webrtc.org                             TransportChannelProxy* proxy);
1935528070a0c76057a000b877fc56ca4180ad2087bmallinath@webrtc.org  void ReplaceChannelProxyImpl(TransportChannelProxy* proxy,
1945528070a0c76057a000b877fc56ca4180ad2087bmallinath@webrtc.org                               TransportChannelImpl* impl);
1955528070a0c76057a000b877fc56ca4180ad2087bmallinath@webrtc.org  void ReplaceChannelProxyImpl_w(TransportChannelProxy* proxy,
1965528070a0c76057a000b877fc56ca4180ad2087bmallinath@webrtc.org                                 TransportChannelImpl* impl);
1975528070a0c76057a000b877fc56ca4180ad2087bmallinath@webrtc.org
1982a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  rtc::Thread* const worker_thread_;
19932a0ac7e336c6132d7a48aa08fe2c912c0eb749etommi@webrtc.org  const std::string sid_;
20032a0ac7e336c6132d7a48aa08fe2c912c0eb749etommi@webrtc.org  const std::string content_name_;
2012a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  rtc::scoped_refptr<TransportWrapper> transport_;
2020e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bool connecting_;
2030e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bool negotiated_;
2040e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  ChannelMap channels_;
2050e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  Candidates sent_candidates_;
2060e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  Candidates unsent_candidates_;
2070e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bool candidates_allocated_;
208f72f5a64fe04356b56ddfb84c6ed488d61ced7c4buildbot@webrtc.org  bool local_description_set_;
209f72f5a64fe04356b56ddfb84c6ed488d61ced7c4buildbot@webrtc.org  bool remote_description_set_;
2100e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org};
2110e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2120e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgtypedef std::map<std::string, TransportProxy*> TransportMap;
2130e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2140e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// Statistics for all the transports of this session.
2150e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgtypedef std::map<std::string, TransportStats> TransportStatsMap;
2160e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgtypedef std::map<std::string, std::string> ProxyTransportMap;
2170e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2180e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgstruct SessionStats {
2190e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  ProxyTransportMap proxy_to_transport;
2200e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  TransportStatsMap transport_stats;
2210e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org};
2220e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2230e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// A BaseSession manages general session state. This includes negotiation
2240e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// of both the application-level and network-level protocols:  the former
2250e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// defines what will be sent and the latter defines how it will be sent.  Each
2260e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// network-level protocol is represented by a Transport object.  Each Transport
2270e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// participates in the network-level negotiation.  The individual streams of
2280e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// packets are represented by TransportChannels.  The application-level protocol
2290e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// is represented by SessionDecription objects.
2300e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgclass BaseSession : public sigslot::has_slots<>,
2312a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org                    public rtc::MessageHandler {
2320e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org public:
2330e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  enum {
2340e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    MSG_TIMEOUT = 0,
2350e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    MSG_ERROR,
2360e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    MSG_STATE,
2370e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  };
2380e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2390e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  enum State {
2400e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    STATE_INIT = 0,
2410e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    STATE_SENTINITIATE,       // sent initiate, waiting for Accept or Reject
2420e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    STATE_RECEIVEDINITIATE,   // received an initiate. Call Accept or Reject
2430e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    STATE_SENTPRACCEPT,       // sent provisional Accept
2440e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    STATE_SENTACCEPT,         // sent accept. begin connecting transport
2450e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    STATE_RECEIVEDPRACCEPT,   // received provisional Accept, waiting for Accept
2460e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    STATE_RECEIVEDACCEPT,     // received accept. begin connecting transport
2470e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    STATE_SENTMODIFY,         // sent modify, waiting for Accept or Reject
2480e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    STATE_RECEIVEDMODIFY,     // received modify, call Accept or Reject
2490e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    STATE_SENTREJECT,         // sent reject after receiving initiate
2500e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    STATE_RECEIVEDREJECT,     // received reject after sending initiate
2510e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    STATE_SENTREDIRECT,       // sent direct after receiving initiate
2520e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    STATE_SENTTERMINATE,      // sent terminate (any time / either side)
2530e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    STATE_RECEIVEDTERMINATE,  // received terminate (any time / either side)
2540e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    STATE_INPROGRESS,         // session accepted and in progress
2550e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    STATE_DEINIT,             // session is being destroyed
2560e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  };
2570e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2580e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  enum Error {
2590e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    ERROR_NONE = 0,       // no error
2600e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    ERROR_TIME = 1,       // no response to signaling
2610e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    ERROR_RESPONSE = 2,   // error during signaling
2620e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    ERROR_NETWORK = 3,    // network error, could not allocate network resources
2630e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    ERROR_CONTENT = 4,    // channel errors in SetLocalContent/SetRemoteContent
2640e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    ERROR_TRANSPORT = 5,  // transport error of some kind
2650e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  };
2660e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2670e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Convert State to a readable string.
2680e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  static std::string StateToString(State state);
2690e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2702a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  BaseSession(rtc::Thread* signaling_thread,
2712a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org              rtc::Thread* worker_thread,
2720e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org              PortAllocator* port_allocator,
2730e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org              const std::string& sid,
2740e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org              const std::string& content_type,
2750e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org              bool initiator);
2760e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual ~BaseSession();
2770e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
27832a0ac7e336c6132d7a48aa08fe2c912c0eb749etommi@webrtc.org  // These are const to allow them to be called from const methods.
2792a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  rtc::Thread* signaling_thread() const { return signaling_thread_; }
2802a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  rtc::Thread* worker_thread() const { return worker_thread_; }
28132a0ac7e336c6132d7a48aa08fe2c912c0eb749etommi@webrtc.org  PortAllocator* port_allocator() const { return port_allocator_; }
2820e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2830e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // The ID of this session.
2840e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  const std::string& id() const { return sid_; }
2850e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2860e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // TODO(juberti): This data is largely redundant, as it can now be obtained
2870e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // from local/remote_description(). Remove these functions and members.
2880e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Returns the XML namespace identifying the type of this session.
2890e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  const std::string& content_type() const { return content_type_; }
2900e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Returns the XML namespace identifying the transport used for this session.
2910e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  const std::string& transport_type() const { return transport_type_; }
2920e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2930e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Indicates whether we initiated this session.
2940e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bool initiator() const { return initiator_; }
2950e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2960e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Returns the application-level description given by our client.
2970e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // If we are the recipient, this will be NULL until we send an accept.
29832a0ac7e336c6132d7a48aa08fe2c912c0eb749etommi@webrtc.org  const SessionDescription* local_description() const;
29932a0ac7e336c6132d7a48aa08fe2c912c0eb749etommi@webrtc.org
3000e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Returns the application-level description given by the other client.
3010e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // If we are the initiator, this will be NULL until we receive an accept.
30232a0ac7e336c6132d7a48aa08fe2c912c0eb749etommi@webrtc.org  const SessionDescription* remote_description() const;
30332a0ac7e336c6132d7a48aa08fe2c912c0eb749etommi@webrtc.org
30432a0ac7e336c6132d7a48aa08fe2c912c0eb749etommi@webrtc.org  SessionDescription* remote_description();
3050e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
3060e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Takes ownership of SessionDescription*
30732a0ac7e336c6132d7a48aa08fe2c912c0eb749etommi@webrtc.org  void set_local_description(const SessionDescription* sdesc);
3080e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
3090e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Takes ownership of SessionDescription*
31032a0ac7e336c6132d7a48aa08fe2c912c0eb749etommi@webrtc.org  void set_remote_description(SessionDescription* sdesc);
3110e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
31232a0ac7e336c6132d7a48aa08fe2c912c0eb749etommi@webrtc.org  const SessionDescription* initiator_description() const;
3130e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
3140e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Returns the current state of the session.  See the enum above for details.
3150e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Each time the state changes, we will fire this signal.
3160e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  State state() const { return state_; }
3170e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  sigslot::signal2<BaseSession* , State> SignalState;
3180e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
3190e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Returns the last error in the session.  See the enum above for details.
3200e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Each time the an error occurs, we will fire this signal.
3210e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  Error error() const { return error_; }
322f32dd31e14521d7f845e7776af6d44d411573370sergeyu@chromium.org  const std::string& error_desc() const { return error_desc_; }
3230e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  sigslot::signal2<BaseSession* , Error> SignalError;
3240e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
3250e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Updates the state, signaling if necessary.
3260e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual void SetState(State state);
3270e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
3280e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Updates the error state, signaling if necessary.
329f32dd31e14521d7f845e7776af6d44d411573370sergeyu@chromium.org  // TODO(ronghuawu): remove the SetError method that doesn't take |error_desc|.
330f32dd31e14521d7f845e7776af6d44d411573370sergeyu@chromium.org  virtual void SetError(Error error, const std::string& error_desc);
3310e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
3320e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Fired when the remote description is updated, with the updated
3330e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // contents.
3340e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  sigslot::signal2<BaseSession* , const ContentInfos&>
3350e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      SignalRemoteDescriptionUpdate;
3360e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
3370e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Fired when SetState is called (regardless if there's a state change), which
3380e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // indicates the session description might have be updated.
3390e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  sigslot::signal2<BaseSession*, ContentAction> SignalNewLocalDescription;
3400e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
3410e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Fired when SetState is called (regardless if there's a state change), which
3420e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // indicates the session description might have be updated.
3430e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  sigslot::signal2<BaseSession*, ContentAction> SignalNewRemoteDescription;
3440e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
3450e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Returns the transport that has been negotiated or NULL if
3460e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // negotiation is still in progress.
34762fe97f10a7a3200c9724851f6a18537ed270cddwu@webrtc.org  virtual Transport* GetTransport(const std::string& content_name);
3480e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
3490e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Creates a new channel with the given names.  This method may be called
3500e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // immediately after creating the session.  However, the actual
3510e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // implementation may not be fixed until transport negotiation completes.
3520e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // This will usually be called from the worker thread, but that
3530e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // shouldn't be an issue since the main thread will be blocked in
3540e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Send when doing so.
3550e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual TransportChannel* CreateChannel(const std::string& content_name,
3560e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                          const std::string& channel_name,
3570e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                          int component);
3580e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
3590e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Returns the channel with the given names.
3600e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual TransportChannel* GetChannel(const std::string& content_name,
3610e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                       int component);
3620e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
3630e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Destroys the channel with the given names.
3640e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // This will usually be called from the worker thread, but that
3650e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // shouldn't be an issue since the main thread will be blocked in
3660e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Send when doing so.
3670e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual void DestroyChannel(const std::string& content_name,
3680e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                              int component);
3690e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
3700e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Returns stats for all channels of all transports.
3710e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // This avoids exposing the internal structures used to track them.
3720e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual bool GetStats(SessionStats* stats);
3730e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
3742a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  rtc::SSLIdentity* identity() { return identity_; }
3755aed3bb9fb287faecd773b88bb68732c31579590wu@webrtc.org
3760e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org protected:
3775aed3bb9fb287faecd773b88bb68732c31579590wu@webrtc.org  // Specifies the identity to use in this session.
3782a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  bool SetIdentity(rtc::SSLIdentity* identity);
3795aed3bb9fb287faecd773b88bb68732c31579590wu@webrtc.org
3800e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bool PushdownTransportDescription(ContentSource source,
381f32dd31e14521d7f845e7776af6d44d411573370sergeyu@chromium.org                                    ContentAction action,
382f32dd31e14521d7f845e7776af6d44d411573370sergeyu@chromium.org                                    std::string* error_desc);
3830e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  void set_initiator(bool initiator) { initiator_ = initiator; }
3840e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
3850e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  const TransportMap& transport_proxies() const { return transports_; }
3860e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Get a TransportProxy by content_name or transport. NULL if not found.
3870e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  TransportProxy* GetTransportProxy(const std::string& content_name);
3880e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  TransportProxy* GetTransportProxy(const Transport* transport);
3890e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  TransportProxy* GetFirstTransportProxy();
3900e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  void DestroyTransportProxy(const std::string& content_name);
3910e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // TransportProxy is owned by session.  Return proxy just for convenience.
3920e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  TransportProxy* GetOrCreateTransportProxy(const std::string& content_name);
3930e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Creates the actual transport object. Overridable for testing.
3940e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual Transport* CreateTransport(const std::string& content_name);
3950e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
3960e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  void OnSignalingReady();
3970e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  void SpeculativelyConnectAllTransportChannels();
3980e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Helper method to provide remote candidates to the transport.
3990e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bool OnRemoteCandidates(const std::string& content_name,
4000e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                          const Candidates& candidates,
4010e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                          std::string* error);
4020e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
4030e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // This method will mux transport channels by content_name.
4040e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // First content is used for muxing.
4050e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bool MaybeEnableMuxingSupport();
4060e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
4070e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Called when a transport requests signaling.
4080e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual void OnTransportRequestSignaling(Transport* transport) {
4090e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
4100e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
4110e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Called when the first channel of a transport begins connecting.  We use
4120e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // this to start a timer, to make sure that the connection completes in a
4130e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // reasonable amount of time.
4140e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual void OnTransportConnecting(Transport* transport) {
4150e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
4160e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
4170e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Called when a transport changes its writable state.  We track this to make
4180e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // sure that the transport becomes writable within a reasonable amount of
4190e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // time.  If this does not occur, we signal an error.
4200e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual void OnTransportWritable(Transport* transport) {
4210e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
4220e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual void OnTransportReadable(Transport* transport) {
423f5e5b3a9ce372d0e3cc594bf0036dda64a57d81dmallinath@webrtc.org  }
424f5e5b3a9ce372d0e3cc594bf0036dda64a57d81dmallinath@webrtc.org
425f5e5b3a9ce372d0e3cc594bf0036dda64a57d81dmallinath@webrtc.org  // Called when a transport has found its steady-state connections.
426f5e5b3a9ce372d0e3cc594bf0036dda64a57d81dmallinath@webrtc.org  virtual void OnTransportCompleted(Transport* transport) {
427f5e5b3a9ce372d0e3cc594bf0036dda64a57d81dmallinath@webrtc.org  }
428f5e5b3a9ce372d0e3cc594bf0036dda64a57d81dmallinath@webrtc.org
429f5e5b3a9ce372d0e3cc594bf0036dda64a57d81dmallinath@webrtc.org  // Called when a transport has failed permanently.
430f5e5b3a9ce372d0e3cc594bf0036dda64a57d81dmallinath@webrtc.org  virtual void OnTransportFailed(Transport* transport) {
4310e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
4320e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
4330e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Called when a transport signals that it has new candidates.
4340e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual void OnTransportProxyCandidatesReady(TransportProxy* proxy,
4350e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                               const Candidates& candidates) {
4360e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
4370e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
4380e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Called when a transport signals that it found an error in an incoming
4390e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // message.
4400e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual void OnTransportSendError(Transport* transport,
4410e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                    const buzz::XmlElement* stanza,
4420e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                    const buzz::QName& name,
4430e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                    const std::string& type,
4440e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                    const std::string& text,
4450e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                    const buzz::XmlElement* extra_info) {
4460e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
4470e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
4480e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual void OnTransportRouteChange(
4490e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      Transport* transport,
4500e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      int component,
4510e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      const cricket::Candidate& remote_candidate) {
4520e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
4530e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
4540e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual void OnTransportCandidatesAllocationDone(Transport* transport);
4550e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
4560e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Called when all transport channels allocated required candidates.
4570e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // This method should be used as an indication of candidates gathering process
4580e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // is completed and application can now send local candidates list to remote.
4590e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual void OnCandidatesAllocationDone() {
4600e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
4610e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
4620e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Handles the ice role change callback from Transport. This must be
4630e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // propagated to all the transports.
4640e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual void OnRoleConflict();
4650e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
4660e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Handles messages posted to us.
4672a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  virtual void OnMessage(rtc::Message *pmsg);
4680e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
4690e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org protected:
4700e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  State state_;
4710e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  Error error_;
472f32dd31e14521d7f845e7776af6d44d411573370sergeyu@chromium.org  std::string error_desc_;
4730e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
4740e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org private:
4750e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Helper methods to push local and remote transport descriptions.
4760e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bool PushdownLocalTransportDescription(
477f32dd31e14521d7f845e7776af6d44d411573370sergeyu@chromium.org      const SessionDescription* sdesc, ContentAction action,
478f32dd31e14521d7f845e7776af6d44d411573370sergeyu@chromium.org      std::string* error_desc);
4790e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bool PushdownRemoteTransportDescription(
480f32dd31e14521d7f845e7776af6d44d411573370sergeyu@chromium.org      const SessionDescription* sdesc, ContentAction action,
481f32dd31e14521d7f845e7776af6d44d411573370sergeyu@chromium.org      std::string* error_desc);
4820e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
4830e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bool IsCandidateAllocationDone() const;
4840e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  void MaybeCandidateAllocationDone();
4850e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
4860e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // This method will delete the Transport and TransportChannelImpls and
4870e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // replace those with the selected Transport objects. Selection is done
4880e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // based on the content_name and in this case first MediaContent information
4890e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // is used for mux.
4900e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bool SetSelectedProxy(const std::string& content_name,
4910e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                        const ContentGroup* muxed_group);
4920e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Log session state.
4930e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  void LogState(State old_state, State new_state);
4940e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
4950e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Returns true and the TransportInfo of the given |content_name|
4960e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // from |description|. Returns false if it's not available.
49732a0ac7e336c6132d7a48aa08fe2c912c0eb749etommi@webrtc.org  static bool GetTransportDescription(const SessionDescription* description,
49832a0ac7e336c6132d7a48aa08fe2c912c0eb749etommi@webrtc.org                                      const std::string& content_name,
49932a0ac7e336c6132d7a48aa08fe2c912c0eb749etommi@webrtc.org                                      TransportDescription* info);
5000e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
5010e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Fires the new description signal according to the current state.
5020e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  void SignalNewDescription();
5030e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
5040e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Gets the ContentAction and ContentSource according to the session state.
5050e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bool GetContentAction(ContentAction* action, ContentSource* source);
5060e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
5072a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  rtc::Thread* const signaling_thread_;
5082a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  rtc::Thread* const worker_thread_;
50932a0ac7e336c6132d7a48aa08fe2c912c0eb749etommi@webrtc.org  PortAllocator* const port_allocator_;
51032a0ac7e336c6132d7a48aa08fe2c912c0eb749etommi@webrtc.org  const std::string sid_;
51132a0ac7e336c6132d7a48aa08fe2c912c0eb749etommi@webrtc.org  const std::string content_type_;
51232a0ac7e336c6132d7a48aa08fe2c912c0eb749etommi@webrtc.org  const std::string transport_type_;
5130e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bool initiator_;
5142a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  rtc::SSLIdentity* identity_;
5152a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  rtc::scoped_ptr<const SessionDescription> local_description_;
5162a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  rtc::scoped_ptr<SessionDescription> remote_description_;
5170e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  uint64 ice_tiebreaker_;
5180e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // This flag will be set to true after the first role switch. This flag
5190e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // will enable us to stop any role switch during the call.
5200e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bool role_switch_;
5210e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  TransportMap transports_;
5220e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org};
5230e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
5240e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// A specific Session created by the SessionManager, using XMPP for protocol.
5250e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgclass Session : public BaseSession {
5260e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org public:
5270e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Returns the manager that created and owns this session.
5280e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  SessionManager* session_manager() const { return session_manager_; }
5290e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
5300e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Returns the client that is handling the application data of this session.
5310e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  SessionClient* client() const { return client_; }
5320e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
5330e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    // Returns the JID of this client.
5340e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  const std::string& local_name() const { return local_name_; }
5350e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
5360e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Returns the JID of the other peer in this session.
5370e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  const std::string& remote_name() const { return remote_name_; }
5380e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
5390e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Set the JID of the other peer in this session.
5400e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Typically the remote_name_ is set when the session is initiated.
5410e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // However, sometimes (e.g when a proxy is used) the peer name is
5420e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // known after the BaseSession has been initiated and it must be updated
5430e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // explicitly.
5440e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  void set_remote_name(const std::string& name) { remote_name_ = name; }
5450e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
5460e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Set the JID of the initiator of this session. Allows for the overriding
5470e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // of the initiator to be a third-party, eg. the MUC JID when creating p2p
5480e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // sessions.
5490e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  void set_initiator_name(const std::string& name) { initiator_name_ = name; }
5500e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
5510e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Indicates the JID of the entity who initiated this session.
5520e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // In special cases, may be different than both local_name and remote_name.
5530e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  const std::string& initiator_name() const { return initiator_name_; }
5540e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
5550e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  SignalingProtocol current_protocol() const { return current_protocol_; }
5560e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
5570e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  void set_current_protocol(SignalingProtocol protocol) {
5580e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    current_protocol_ = protocol;
5590e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
5600e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
5610e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Updates the error state, signaling if necessary.
562f32dd31e14521d7f845e7776af6d44d411573370sergeyu@chromium.org  virtual void SetError(Error error, const std::string& error_desc);
5630e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
5640e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // When the session needs to send signaling messages, it beings by requesting
5650e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // signaling.  The client should handle this by calling OnSignalingReady once
5660e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // it is ready to send the messages.
5670e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // (These are called only by SessionManager.)
5680e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  sigslot::signal1<Session*> SignalRequestSignaling;
5690e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  void OnSignalingReady() { BaseSession::OnSignalingReady(); }
5700e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
5710e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Takes ownership of session description.
5720e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // TODO: Add an error argument to pass back to the caller.
5730e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bool Initiate(const std::string& to,
5740e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                const SessionDescription* sdesc);
5750e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
5760e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // When we receive an initiate, we create a session in the
5770e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // RECEIVEDINITIATE state and respond by accepting or rejecting.
5780e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Takes ownership of session description.
5790e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // TODO: Add an error argument to pass back to the caller.
5800e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bool Accept(const SessionDescription* sdesc);
5810e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bool Reject(const std::string& reason);
5820e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bool Terminate() {
5830e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    return TerminateWithReason(STR_TERMINATE_SUCCESS);
5840e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
5850e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bool TerminateWithReason(const std::string& reason);
5860e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Fired whenever we receive a terminate message along with a reason
5870e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  sigslot::signal2<Session*, const std::string&> SignalReceivedTerminateReason;
5880e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
5890e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // The two clients in the session may also send one another
5900e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // arbitrary XML messages, which are called "info" messages. Sending
5910e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // takes ownership of the given elements.  The signal does not; the
5920e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // parent element will be deleted after the signal.
5933db045861afcaf20aed671c0de15a6ff4ca32d08wu@webrtc.org  bool SendInfoMessage(const XmlElements& elems,
5943db045861afcaf20aed671c0de15a6ff4ca32d08wu@webrtc.org                       const std::string& remote_name);
5950e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bool SendDescriptionInfoMessage(const ContentInfos& contents);
5960e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  sigslot::signal2<Session*, const buzz::XmlElement*> SignalInfoMessage;
5970e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
5980e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org private:
5990e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Creates or destroys a session.  (These are called only SessionManager.)
6000e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  Session(SessionManager *session_manager,
6010e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org          const std::string& local_name, const std::string& initiator_name,
6020e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org          const std::string& sid, const std::string& content_type,
6030e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org          SessionClient* client);
6040e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  ~Session();
6050e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // For each transport info, create a transport proxy.  Can fail for
6060e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // incompatible transport types.
6070e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bool CreateTransportProxies(const TransportInfos& tinfos,
6080e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                              SessionError* error);
6090e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bool OnRemoteCandidates(const TransportInfos& tinfos,
6100e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                          ParseError* error);
6110e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Returns a TransportInfo without candidates for each content name.
6120e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Uses the transport_type_ of the session.
6130e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  TransportInfos GetEmptyTransportInfos(const ContentInfos& contents) const;
6140e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
6150e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    // Maps passed to serialization functions.
6160e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  TransportParserMap GetTransportParsers();
6170e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  ContentParserMap GetContentParsers();
6180e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  CandidateTranslatorMap GetCandidateTranslators();
6190e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
6200e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual void OnTransportRequestSignaling(Transport* transport);
6210e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual void OnTransportConnecting(Transport* transport);
6220e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual void OnTransportWritable(Transport* transport);
6230e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual void OnTransportProxyCandidatesReady(TransportProxy* proxy,
6240e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                               const Candidates& candidates);
6250e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual void OnTransportSendError(Transport* transport,
6260e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                    const buzz::XmlElement* stanza,
6270e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                    const buzz::QName& name,
6280e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                    const std::string& type,
6290e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                    const std::string& text,
6300e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                    const buzz::XmlElement* extra_info);
6312a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  virtual void OnMessage(rtc::Message *pmsg);
6320e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
6330e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Send various kinds of session messages.
6340e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bool SendInitiateMessage(const SessionDescription* sdesc,
6350e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                           SessionError* error);
6360e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bool SendAcceptMessage(const SessionDescription* sdesc, SessionError* error);
6370e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bool SendRejectMessage(const std::string& reason, SessionError* error);
6380e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bool SendTerminateMessage(const std::string& reason, SessionError* error);
6390e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bool SendTransportInfoMessage(const TransportInfo& tinfo,
6400e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                SessionError* error);
6410e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bool SendTransportInfoMessage(const TransportProxy* transproxy,
6420e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                const Candidates& candidates,
6430e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                SessionError* error);
6440e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
6450e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bool ResendAllTransportInfoMessages(SessionError* error);
6460e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bool SendAllUnsentTransportInfoMessages(SessionError* error);
6470e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
6483db045861afcaf20aed671c0de15a6ff4ca32d08wu@webrtc.org  // All versions of SendMessage send a message of the given type to
6490e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // the other client.  Can pass either a set of elements or an
6500e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // "action", which must have a WriteSessionAction method to go along
6510e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // with it.  Sending with an action supports sending a "hybrid"
6520e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // message.  Sending with elements must be sent as Jingle or Gingle.
6530e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
6540e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // When passing elems, must be either Jingle or Gingle protocol.
6550e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Takes ownership of action_elems.
6560e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bool SendMessage(ActionType type, const XmlElements& action_elems,
6570e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                   SessionError* error);
6583db045861afcaf20aed671c0de15a6ff4ca32d08wu@webrtc.org  // Sends a messge, but overrides the remote name.
6593db045861afcaf20aed671c0de15a6ff4ca32d08wu@webrtc.org  bool SendMessage(ActionType type, const XmlElements& action_elems,
6603db045861afcaf20aed671c0de15a6ff4ca32d08wu@webrtc.org                   const std::string& remote_name,
6613db045861afcaf20aed671c0de15a6ff4ca32d08wu@webrtc.org                   SessionError* error);
6620e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // When passing an action, may be Hybrid protocol.
6630e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  template <typename Action>
6640e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bool SendMessage(ActionType type, const Action& action,
6650e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                   SessionError* error);
6660e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
6670e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Helper methods to write the session message stanza.
6680e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  template <typename Action>
6690e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bool WriteActionMessage(ActionType type, const Action& action,
6700e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                          buzz::XmlElement* stanza, WriteError* error);
6710e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  template <typename Action>
6720e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bool WriteActionMessage(SignalingProtocol protocol,
6730e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                          ActionType type, const Action& action,
6740e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                          buzz::XmlElement* stanza, WriteError* error);
6750e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
6760e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Sending messages in hybrid form requires being able to write them
6770e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // on a per-protocol basis with a common method signature, which all
6780e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // of these have.
6790e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bool WriteSessionAction(SignalingProtocol protocol,
6800e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                          const SessionInitiate& init,
6810e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                          XmlElements* elems, WriteError* error);
6820e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bool WriteSessionAction(SignalingProtocol protocol,
6830e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                          const TransportInfo& tinfo,
6840e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                          XmlElements* elems, WriteError* error);
6850e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bool WriteSessionAction(SignalingProtocol protocol,
6860e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                          const SessionTerminate& term,
6870e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                          XmlElements* elems, WriteError* error);
6880e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
6890e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Sends a message back to the other client indicating that we have received
6900e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // and accepted their message.
6910e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  void SendAcknowledgementMessage(const buzz::XmlElement* stanza);
6920e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
6930e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Once signaling is ready, the session will use this signal to request the
6940e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // sending of each message.  When messages are received by the other client,
6950e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // they should be handed to OnIncomingMessage.
6960e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // (These are called only by SessionManager.)
6970e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  sigslot::signal2<Session* , const buzz::XmlElement*> SignalOutgoingMessage;
6980e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  void OnIncomingMessage(const SessionMessage& msg);
6990e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
7000e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  void OnIncomingResponse(const buzz::XmlElement* orig_stanza,
7010e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                          const buzz::XmlElement* response_stanza,
7020e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                          const SessionMessage& msg);
7030e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  void OnInitiateAcked();
7040e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  void OnFailedSend(const buzz::XmlElement* orig_stanza,
7050e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                    const buzz::XmlElement* error_stanza);
7060e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
7070e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Invoked when an error is found in an incoming message.  This is translated
7080e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // into the appropriate XMPP response by SessionManager.
7090e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  sigslot::signal6<BaseSession*,
7100e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                   const buzz::XmlElement*,
7110e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                   const buzz::QName&,
7120e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                   const std::string&,
7130e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                   const std::string&,
7140e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                   const buzz::XmlElement*> SignalErrorMessage;
7150e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
7160e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Handlers for the various types of messages.  These functions may take
7170e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // pointers to the whole stanza or to just the session element.
7180e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bool OnInitiateMessage(const SessionMessage& msg, MessageError* error);
7190e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bool OnAcceptMessage(const SessionMessage& msg, MessageError* error);
7200e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bool OnRejectMessage(const SessionMessage& msg, MessageError* error);
7210e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bool OnInfoMessage(const SessionMessage& msg);
7220e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bool OnTerminateMessage(const SessionMessage& msg, MessageError* error);
7230e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bool OnTransportInfoMessage(const SessionMessage& msg, MessageError* error);
7240e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bool OnTransportAcceptMessage(const SessionMessage& msg, MessageError* error);
7250e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bool OnDescriptionInfoMessage(const SessionMessage& msg, MessageError* error);
7260e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bool OnRedirectError(const SessionRedirect& redirect, SessionError* error);
7270e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
7280e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Verifies that we are in the appropriate state to receive this message.
7290e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bool CheckState(State state, MessageError* error);
7300e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
7310e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  SessionManager* session_manager_;
7320e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bool initiate_acked_;
7330e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  std::string local_name_;
7340e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  std::string initiator_name_;
7350e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  std::string remote_name_;
7360e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  SessionClient* client_;
7370e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  TransportParser* transport_parser_;
7380e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Keeps track of what protocol we are speaking.
7390e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  SignalingProtocol current_protocol_;
7400e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
7410e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  friend class SessionManager;  // For access to constructor, destructor,
7420e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                // and signaling related methods.
7430e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org};
7440e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
7450e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}  // namespace cricket
7460e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
7470e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#endif  // TALK_P2P_BASE_SESSION_H_
748