3 * Copyright 2004--2005, Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
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.
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.
28 #include "talk/p2p/base/session.h"
30 #include "talk/base/bind.h"
31 #include "talk/base/common.h"
32 #include "talk/base/logging.h"
33 #include "talk/base/helpers.h"
34 #include "talk/base/scoped_ptr.h"
35 #include "talk/base/sslstreamadapter.h"
36 #include "talk/xmpp/constants.h"
37 #include "talk/xmpp/jid.h"
38 #include "talk/p2p/base/dtlstransport.h"
39 #include "talk/p2p/base/p2ptransport.h"
40 #include "talk/p2p/base/sessionclient.h"
41 #include "talk/p2p/base/transport.h"
42 #include "talk/p2p/base/transportchannelproxy.h"
43 #include "talk/p2p/base/transportinfo.h"
45 #include "talk/p2p/base/constants.h"
49 using talk_base::Bind;
51 bool BadMessage(const buzz::QName type,
52 const std::string& text,
59 TransportProxy::~TransportProxy() {
60 for (ChannelMap::iterator iter = channels_.begin();
61 iter != channels_.end(); ++iter) {
62 iter->second->SignalDestroyed(iter->second);
67 std::string TransportProxy::type() const {
68 return transport_->get()->type();
71 TransportChannel* TransportProxy::GetChannel(int component) {
72 ASSERT(talk_base::Thread::Current() == worker_thread_);
73 return GetChannelProxy(component);
76 TransportChannel* TransportProxy::CreateChannel(
77 const std::string& name, int component) {
78 ASSERT(talk_base::Thread::Current() == worker_thread_);
79 ASSERT(GetChannel(component) == NULL);
80 ASSERT(!transport_->get()->HasChannel(component));
82 // We always create a proxy in case we need to change out the transport later.
83 TransportChannelProxy* channel =
84 new TransportChannelProxy(content_name(), name, component);
85 channels_[component] = channel;
87 // If we're already negotiated, create an impl and hook it up to the proxy
88 // channel. If we're connecting, create an impl but don't hook it up yet.
90 SetupChannelProxy_w(component, channel);
91 } else if (connecting_) {
92 GetOrCreateChannelProxyImpl_w(component);
97 bool TransportProxy::HasChannel(int component) {
98 return transport_->get()->HasChannel(component);
101 void TransportProxy::DestroyChannel(int component) {
102 ASSERT(talk_base::Thread::Current() == worker_thread_);
103 TransportChannel* channel = GetChannel(component);
105 // If the state of TransportProxy is not NEGOTIATED
106 // then TransportChannelProxy and its impl are not
107 // connected. Both must be connected before
110 SetupChannelProxy_w(component, GetChannelProxy(component));
113 channels_.erase(component);
114 channel->SignalDestroyed(channel);
119 void TransportProxy::ConnectChannels() {
122 for (ChannelMap::iterator iter = channels_.begin();
123 iter != channels_.end(); ++iter) {
124 GetOrCreateChannelProxyImpl(iter->first);
129 // TODO(juberti): Right now Transport::ConnectChannels doesn't work if we
130 // don't have any channels yet, so we need to allow this method to be called
131 // multiple times. Once we fix Transport, we can move this call inside the
132 // if (!connecting_) block.
133 transport_->get()->ConnectChannels();
136 void TransportProxy::CompleteNegotiation() {
138 for (ChannelMap::iterator iter = channels_.begin();
139 iter != channels_.end(); ++iter) {
140 SetupChannelProxy(iter->first, iter->second);
146 void TransportProxy::AddSentCandidates(const Candidates& candidates) {
147 for (Candidates::const_iterator cand = candidates.begin();
148 cand != candidates.end(); ++cand) {
149 sent_candidates_.push_back(*cand);
153 void TransportProxy::AddUnsentCandidates(const Candidates& candidates) {
154 for (Candidates::const_iterator cand = candidates.begin();
155 cand != candidates.end(); ++cand) {
156 unsent_candidates_.push_back(*cand);
160 bool TransportProxy::GetChannelNameFromComponent(
161 int component, std::string* channel_name) const {
162 const TransportChannelProxy* channel = GetChannelProxy(component);
163 if (channel == NULL) {
167 *channel_name = channel->name();
171 bool TransportProxy::GetComponentFromChannelName(
172 const std::string& channel_name, int* component) const {
173 const TransportChannelProxy* channel = GetChannelProxyByName(channel_name);
174 if (channel == NULL) {
178 *component = channel->component();
182 TransportChannelProxy* TransportProxy::GetChannelProxy(int component) const {
183 ChannelMap::const_iterator iter = channels_.find(component);
184 return (iter != channels_.end()) ? iter->second : NULL;
187 TransportChannelProxy* TransportProxy::GetChannelProxyByName(
188 const std::string& name) const {
189 for (ChannelMap::const_iterator iter = channels_.begin();
190 iter != channels_.end();
192 if (iter->second->name() == name) {
199 TransportChannelImpl* TransportProxy::GetOrCreateChannelProxyImpl(
201 return worker_thread_->Invoke<TransportChannelImpl*>(Bind(
202 &TransportProxy::GetOrCreateChannelProxyImpl_w, this, component));
205 TransportChannelImpl* TransportProxy::GetOrCreateChannelProxyImpl_w(
207 ASSERT(talk_base::Thread::Current() == worker_thread_);
208 TransportChannelImpl* impl = transport_->get()->GetChannel(component);
210 impl = transport_->get()->CreateChannel(component);
215 void TransportProxy::SetupChannelProxy(
216 int component, TransportChannelProxy* transproxy) {
217 worker_thread_->Invoke<void>(Bind(
218 &TransportProxy::SetupChannelProxy_w, this, component, transproxy));
221 void TransportProxy::SetupChannelProxy_w(
222 int component, TransportChannelProxy* transproxy) {
223 ASSERT(talk_base::Thread::Current() == worker_thread_);
224 TransportChannelImpl* impl = GetOrCreateChannelProxyImpl(component);
225 ASSERT(impl != NULL);
226 transproxy->SetImplementation(impl);
229 void TransportProxy::ReplaceChannelProxyImpl(TransportChannelProxy* proxy,
230 TransportChannelImpl* impl) {
231 worker_thread_->Invoke<void>(Bind(
232 &TransportProxy::ReplaceChannelProxyImpl_w, this, proxy, impl));
235 void TransportProxy::ReplaceChannelProxyImpl_w(TransportChannelProxy* proxy,
236 TransportChannelImpl* impl) {
237 ASSERT(talk_base::Thread::Current() == worker_thread_);
238 ASSERT(proxy != NULL);
239 proxy->SetImplementation(impl);
242 // This function muxes |this| onto |target| by repointing |this| at
243 // |target|'s transport and setting our TransportChannelProxies
244 // to point to |target|'s underlying implementations.
245 bool TransportProxy::SetupMux(TransportProxy* target) {
246 // Bail out if there's nothing to do.
247 if (transport_ == target->transport_) {
251 // Run through all channels and remove any non-rtp transport channels before
252 // setting target transport channels.
253 for (ChannelMap::const_iterator iter = channels_.begin();
254 iter != channels_.end(); ++iter) {
255 if (!target->transport_->get()->HasChannel(iter->first)) {
256 // Remove if channel doesn't exist in |transport_|.
257 ReplaceChannelProxyImpl(iter->second, NULL);
259 // Replace the impl for all the TransportProxyChannels with the channels
260 // from |target|'s transport. Fail if there's not an exact match.
261 ReplaceChannelProxyImpl(
262 iter->second, target->transport_->get()->CreateChannel(iter->first));
266 // Now replace our transport. Must happen afterwards because
267 // it deletes all impls as a side effect.
268 transport_ = target->transport_;
269 transport_->get()->SignalCandidatesReady.connect(
270 this, &TransportProxy::OnTransportCandidatesReady);
271 set_candidates_allocated(target->candidates_allocated());
275 void TransportProxy::SetIceRole(IceRole role) {
276 transport_->get()->SetIceRole(role);
279 bool TransportProxy::SetLocalTransportDescription(
280 const TransportDescription& description,
281 ContentAction action,
282 std::string* error_desc) {
283 // If this is an answer, finalize the negotiation.
284 if (action == CA_ANSWER) {
285 CompleteNegotiation();
287 return transport_->get()->SetLocalTransportDescription(description,
292 bool TransportProxy::SetRemoteTransportDescription(
293 const TransportDescription& description,
294 ContentAction action,
295 std::string* error_desc) {
296 // If this is an answer, finalize the negotiation.
297 if (action == CA_ANSWER) {
298 CompleteNegotiation();
300 return transport_->get()->SetRemoteTransportDescription(description,
305 void TransportProxy::OnSignalingReady() {
306 // If we're starting a new allocation sequence, reset our state.
307 set_candidates_allocated(false);
308 transport_->get()->OnSignalingReady();
311 bool TransportProxy::OnRemoteCandidates(const Candidates& candidates,
312 std::string* error) {
313 // Ensure the transport is negotiated before handling candidates.
314 // TODO(juberti): Remove this once everybody calls SetLocalTD.
315 CompleteNegotiation();
317 // Verify each candidate before passing down to transport layer.
318 for (Candidates::const_iterator cand = candidates.begin();
319 cand != candidates.end(); ++cand) {
320 if (!transport_->get()->VerifyCandidate(*cand, error))
322 if (!HasChannel(cand->component())) {
323 *error = "Candidate has unknown component: " + cand->ToString() +
324 " for content: " + content_name_;
328 transport_->get()->OnRemoteCandidates(candidates);
332 void TransportProxy::SetIdentity(
333 talk_base::SSLIdentity* identity) {
334 transport_->get()->SetIdentity(identity);
337 std::string BaseSession::StateToString(State state) {
339 case Session::STATE_INIT:
341 case Session::STATE_SENTINITIATE:
342 return "STATE_SENTINITIATE";
343 case Session::STATE_RECEIVEDINITIATE:
344 return "STATE_RECEIVEDINITIATE";
345 case Session::STATE_SENTPRACCEPT:
346 return "STATE_SENTPRACCEPT";
347 case Session::STATE_SENTACCEPT:
348 return "STATE_SENTACCEPT";
349 case Session::STATE_RECEIVEDPRACCEPT:
350 return "STATE_RECEIVEDPRACCEPT";
351 case Session::STATE_RECEIVEDACCEPT:
352 return "STATE_RECEIVEDACCEPT";
353 case Session::STATE_SENTMODIFY:
354 return "STATE_SENTMODIFY";
355 case Session::STATE_RECEIVEDMODIFY:
356 return "STATE_RECEIVEDMODIFY";
357 case Session::STATE_SENTREJECT:
358 return "STATE_SENTREJECT";
359 case Session::STATE_RECEIVEDREJECT:
360 return "STATE_RECEIVEDREJECT";
361 case Session::STATE_SENTREDIRECT:
362 return "STATE_SENTREDIRECT";
363 case Session::STATE_SENTTERMINATE:
364 return "STATE_SENTTERMINATE";
365 case Session::STATE_RECEIVEDTERMINATE:
366 return "STATE_RECEIVEDTERMINATE";
367 case Session::STATE_INPROGRESS:
368 return "STATE_INPROGRESS";
369 case Session::STATE_DEINIT:
370 return "STATE_DEINIT";
374 return "STATE_" + talk_base::ToString(state);
377 BaseSession::BaseSession(talk_base::Thread* signaling_thread,
378 talk_base::Thread* worker_thread,
379 PortAllocator* port_allocator,
380 const std::string& sid,
381 const std::string& content_type,
383 : state_(STATE_INIT),
385 signaling_thread_(signaling_thread),
386 worker_thread_(worker_thread),
387 port_allocator_(port_allocator),
389 content_type_(content_type),
390 transport_type_(NS_GINGLE_P2P),
391 initiator_(initiator),
393 local_description_(NULL),
394 remote_description_(NULL),
395 ice_tiebreaker_(talk_base::CreateRandomId64()),
396 role_switch_(false) {
397 ASSERT(signaling_thread->IsCurrent());
400 BaseSession::~BaseSession() {
401 ASSERT(signaling_thread()->IsCurrent());
403 ASSERT(state_ != STATE_DEINIT);
404 LogState(state_, STATE_DEINIT);
405 state_ = STATE_DEINIT;
406 SignalState(this, state_);
408 for (TransportMap::iterator iter = transports_.begin();
409 iter != transports_.end(); ++iter) {
413 delete remote_description_;
414 delete local_description_;
417 bool BaseSession::SetIdentity(talk_base::SSLIdentity* identity) {
420 identity_ = identity;
421 for (TransportMap::iterator iter = transports_.begin();
422 iter != transports_.end(); ++iter) {
423 iter->second->SetIdentity(identity_);
428 bool BaseSession::PushdownTransportDescription(ContentSource source,
429 ContentAction action,
430 std::string* error_desc) {
431 if (source == CS_LOCAL) {
432 return PushdownLocalTransportDescription(local_description_,
436 return PushdownRemoteTransportDescription(remote_description_,
441 bool BaseSession::PushdownLocalTransportDescription(
442 const SessionDescription* sdesc,
443 ContentAction action,
444 std::string* error_desc) {
445 // Update the Transports with the right information, and trigger them to
447 for (TransportMap::iterator iter = transports_.begin();
448 iter != transports_.end(); ++iter) {
449 // If no transport info was in this session description, ret == false
450 // and we just skip this one.
451 TransportDescription tdesc;
452 bool ret = GetTransportDescription(
453 sdesc, iter->second->content_name(), &tdesc);
455 if (!iter->second->SetLocalTransportDescription(tdesc, action,
460 iter->second->ConnectChannels();
467 bool BaseSession::PushdownRemoteTransportDescription(
468 const SessionDescription* sdesc,
469 ContentAction action,
470 std::string* error_desc) {
471 // Update the Transports with the right information.
472 for (TransportMap::iterator iter = transports_.begin();
473 iter != transports_.end(); ++iter) {
474 TransportDescription tdesc;
476 // If no transport info was in this session description, ret == false
477 // and we just skip this one.
478 bool ret = GetTransportDescription(
479 sdesc, iter->second->content_name(), &tdesc);
481 if (!iter->second->SetRemoteTransportDescription(tdesc, action,
491 TransportChannel* BaseSession::CreateChannel(const std::string& content_name,
492 const std::string& channel_name,
494 // We create the proxy "on demand" here because we need to support
495 // creating channels at any time, even before we send or receive
496 // initiate messages, which is before we create the transports.
497 TransportProxy* transproxy = GetOrCreateTransportProxy(content_name);
498 return transproxy->CreateChannel(channel_name, component);
501 TransportChannel* BaseSession::GetChannel(const std::string& content_name,
503 TransportProxy* transproxy = GetTransportProxy(content_name);
504 if (transproxy == NULL)
507 return transproxy->GetChannel(component);
510 void BaseSession::DestroyChannel(const std::string& content_name,
512 TransportProxy* transproxy = GetTransportProxy(content_name);
513 ASSERT(transproxy != NULL);
514 transproxy->DestroyChannel(component);
517 TransportProxy* BaseSession::GetOrCreateTransportProxy(
518 const std::string& content_name) {
519 TransportProxy* transproxy = GetTransportProxy(content_name);
523 Transport* transport = CreateTransport(content_name);
524 transport->SetIceRole(initiator_ ? ICEROLE_CONTROLLING : ICEROLE_CONTROLLED);
525 transport->SetIceTiebreaker(ice_tiebreaker_);
526 // TODO: Connect all the Transport signals to TransportProxy
527 // then to the BaseSession.
528 transport->SignalConnecting.connect(
529 this, &BaseSession::OnTransportConnecting);
530 transport->SignalWritableState.connect(
531 this, &BaseSession::OnTransportWritable);
532 transport->SignalRequestSignaling.connect(
533 this, &BaseSession::OnTransportRequestSignaling);
534 transport->SignalTransportError.connect(
535 this, &BaseSession::OnTransportSendError);
536 transport->SignalRouteChange.connect(
537 this, &BaseSession::OnTransportRouteChange);
538 transport->SignalCandidatesAllocationDone.connect(
539 this, &BaseSession::OnTransportCandidatesAllocationDone);
540 transport->SignalRoleConflict.connect(
541 this, &BaseSession::OnRoleConflict);
542 transport->SignalCompleted.connect(
543 this, &BaseSession::OnTransportCompleted);
544 transport->SignalFailed.connect(
545 this, &BaseSession::OnTransportFailed);
547 transproxy = new TransportProxy(worker_thread_, sid_, content_name,
548 new TransportWrapper(transport));
549 transproxy->SignalCandidatesReady.connect(
550 this, &BaseSession::OnTransportProxyCandidatesReady);
551 transports_[content_name] = transproxy;
556 Transport* BaseSession::GetTransport(const std::string& content_name) {
557 TransportProxy* transproxy = GetTransportProxy(content_name);
558 if (transproxy == NULL)
560 return transproxy->impl();
563 TransportProxy* BaseSession::GetTransportProxy(
564 const std::string& content_name) {
565 TransportMap::iterator iter = transports_.find(content_name);
566 return (iter != transports_.end()) ? iter->second : NULL;
569 TransportProxy* BaseSession::GetTransportProxy(const Transport* transport) {
570 for (TransportMap::iterator iter = transports_.begin();
571 iter != transports_.end(); ++iter) {
572 TransportProxy* transproxy = iter->second;
573 if (transproxy->impl() == transport) {
580 TransportProxy* BaseSession::GetFirstTransportProxy() {
581 if (transports_.empty())
583 return transports_.begin()->second;
586 void BaseSession::DestroyTransportProxy(
587 const std::string& content_name) {
588 TransportMap::iterator iter = transports_.find(content_name);
589 if (iter != transports_.end()) {
591 transports_.erase(content_name);
595 cricket::Transport* BaseSession::CreateTransport(
596 const std::string& content_name) {
597 ASSERT(transport_type_ == NS_GINGLE_P2P);
598 return new cricket::DtlsTransport<P2PTransport>(
599 signaling_thread(), worker_thread(), content_name,
600 port_allocator(), identity_);
603 bool BaseSession::GetStats(SessionStats* stats) {
604 for (TransportMap::iterator iter = transports_.begin();
605 iter != transports_.end(); ++iter) {
606 std::string proxy_id = iter->second->content_name();
607 // We are ignoring not-yet-instantiated transports.
608 if (iter->second->impl()) {
609 std::string transport_id = iter->second->impl()->content_name();
610 stats->proxy_to_transport[proxy_id] = transport_id;
611 if (stats->transport_stats.find(transport_id)
612 == stats->transport_stats.end()) {
613 TransportStats subinfos;
614 if (!iter->second->impl()->GetStats(&subinfos)) {
617 stats->transport_stats[transport_id] = subinfos;
624 void BaseSession::SetState(State state) {
625 ASSERT(signaling_thread_->IsCurrent());
626 if (state != state_) {
627 LogState(state_, state);
629 SignalState(this, state_);
630 signaling_thread_->Post(this, MSG_STATE);
632 SignalNewDescription();
635 void BaseSession::SetError(Error error, const std::string& error_desc) {
636 ASSERT(signaling_thread_->IsCurrent());
637 if (error != error_) {
639 error_desc_ = error_desc;
640 SignalError(this, error);
644 void BaseSession::OnSignalingReady() {
645 ASSERT(signaling_thread()->IsCurrent());
646 for (TransportMap::iterator iter = transports_.begin();
647 iter != transports_.end(); ++iter) {
648 iter->second->OnSignalingReady();
652 // TODO(juberti): Since PushdownLocalTD now triggers the connection process to
653 // start, remove this method once everyone calls PushdownLocalTD.
654 void BaseSession::SpeculativelyConnectAllTransportChannels() {
655 // Put all transports into the connecting state.
656 for (TransportMap::iterator iter = transports_.begin();
657 iter != transports_.end(); ++iter) {
658 iter->second->ConnectChannels();
662 bool BaseSession::OnRemoteCandidates(const std::string& content_name,
663 const Candidates& candidates,
664 std::string* error) {
665 // Give candidates to the appropriate transport, and tell that transport
666 // to start connecting, if it's not already doing so.
667 TransportProxy* transproxy = GetTransportProxy(content_name);
669 *error = "Unknown content name " + content_name;
672 if (!transproxy->OnRemoteCandidates(candidates, error)) {
675 // TODO(juberti): Remove this call once we can be sure that we always have
676 // a local transport description (which will trigger the connection).
677 transproxy->ConnectChannels();
681 bool BaseSession::MaybeEnableMuxingSupport() {
682 // We need both a local and remote description to decide if we should mux.
683 if ((state_ == STATE_SENTINITIATE ||
684 state_ == STATE_RECEIVEDINITIATE) &&
685 ((local_description_ == NULL) ||
686 (remote_description_ == NULL))) {
690 // In order to perform the multiplexing, we need all proxies to be in the
691 // negotiated state, i.e. to have implementations underneath.
692 // Ensure that this is the case, regardless of whether we are going to mux.
693 for (TransportMap::iterator iter = transports_.begin();
694 iter != transports_.end(); ++iter) {
695 ASSERT(iter->second->negotiated());
696 if (!iter->second->negotiated())
700 // If both sides agree to BUNDLE, mux all the specified contents onto the
701 // transport belonging to the first content name in the BUNDLE group.
702 // If the contents are already muxed, this will be a no-op.
703 // TODO(juberti): Should this check that local and remote have configured
704 // BUNDLE the same way?
705 bool candidates_allocated = IsCandidateAllocationDone();
706 const ContentGroup* local_bundle_group =
707 local_description()->GetGroupByName(GROUP_TYPE_BUNDLE);
708 const ContentGroup* remote_bundle_group =
709 remote_description()->GetGroupByName(GROUP_TYPE_BUNDLE);
710 if (local_bundle_group && remote_bundle_group &&
711 local_bundle_group->FirstContentName()) {
712 const std::string* content_name = local_bundle_group->FirstContentName();
713 const ContentInfo* content =
714 local_description_->GetContentByName(*content_name);
715 ASSERT(content != NULL);
716 if (!SetSelectedProxy(content->name, local_bundle_group)) {
717 LOG(LS_WARNING) << "Failed to set up BUNDLE";
721 // If we weren't done gathering before, we might be done now, as a result
723 LOG(LS_INFO) << "Enabling BUNDLE, bundling onto transport: "
725 if (!candidates_allocated) {
726 MaybeCandidateAllocationDone();
729 LOG(LS_INFO) << "No BUNDLE information, not bundling.";
734 bool BaseSession::SetSelectedProxy(const std::string& content_name,
735 const ContentGroup* muxed_group) {
736 TransportProxy* selected_proxy = GetTransportProxy(content_name);
737 if (!selected_proxy) {
741 ASSERT(selected_proxy->negotiated());
742 for (TransportMap::iterator iter = transports_.begin();
743 iter != transports_.end(); ++iter) {
744 // If content is part of the mux group, then repoint its proxy at the
745 // transport object that we have chosen to mux onto. If the proxy
746 // is already pointing at the right object, it will be a no-op.
747 if (muxed_group->HasContentName(iter->first) &&
748 !iter->second->SetupMux(selected_proxy)) {
755 void BaseSession::OnTransportCandidatesAllocationDone(Transport* transport) {
756 // TODO(juberti): This is a clunky way of processing the done signal. Instead,
757 // TransportProxy should receive the done signal directly, set its allocated
758 // flag internally, and then reissue the done signal to Session.
759 // Overall we should make TransportProxy receive *all* the signals from
760 // Transport, since this removes the need to manually iterate over all
761 // the transports, as is needed to make sure signals are handled properly
764 ASSERT(!IsCandidateAllocationDone());
766 for (TransportMap::iterator iter = transports_.begin();
767 iter != transports_.end(); ++iter) {
768 if (iter->second->impl() == transport) {
769 iter->second->set_candidates_allocated(true);
772 MaybeCandidateAllocationDone();
775 bool BaseSession::IsCandidateAllocationDone() const {
776 for (TransportMap::const_iterator iter = transports_.begin();
777 iter != transports_.end(); ++iter) {
778 if (!iter->second->candidates_allocated())
784 void BaseSession::MaybeCandidateAllocationDone() {
785 if (IsCandidateAllocationDone()) {
786 LOG(LS_INFO) << "Candidate gathering is complete.";
787 OnCandidatesAllocationDone();
791 void BaseSession::OnRoleConflict() {
793 LOG(LS_WARNING) << "Repeat of role conflict signal from Transport.";
798 for (TransportMap::iterator iter = transports_.begin();
799 iter != transports_.end(); ++iter) {
800 // Role will be reverse of initial role setting.
801 IceRole role = initiator_ ? ICEROLE_CONTROLLED : ICEROLE_CONTROLLING;
802 iter->second->SetIceRole(role);
806 void BaseSession::LogState(State old_state, State new_state) {
807 LOG(LS_INFO) << "Session:" << id()
808 << " Old state:" << StateToString(old_state)
809 << " New state:" << StateToString(new_state)
810 << " Type:" << content_type()
811 << " Transport:" << transport_type();
814 bool BaseSession::GetTransportDescription(const SessionDescription* description,
815 const std::string& content_name,
816 TransportDescription* tdesc) {
817 if (!description || !tdesc) {
820 const TransportInfo* transport_info =
821 description->GetTransportInfoByName(content_name);
822 if (!transport_info) {
825 *tdesc = transport_info->description;
829 void BaseSession::SignalNewDescription() {
830 ContentAction action;
831 ContentSource source;
832 if (!GetContentAction(&action, &source)) {
835 if (source == CS_LOCAL) {
836 SignalNewLocalDescription(this, action);
838 SignalNewRemoteDescription(this, action);
842 bool BaseSession::GetContentAction(ContentAction* action,
843 ContentSource* source) {
845 // new local description
846 case STATE_SENTINITIATE:
850 case STATE_SENTPRACCEPT:
851 *action = CA_PRANSWER;
854 case STATE_SENTACCEPT:
858 // new remote description
859 case STATE_RECEIVEDINITIATE:
863 case STATE_RECEIVEDPRACCEPT:
864 *action = CA_PRANSWER;
867 case STATE_RECEIVEDACCEPT:
877 void BaseSession::OnMessage(talk_base::Message *pmsg) {
878 switch (pmsg->message_id) {
880 // Session timeout has occured.
881 SetError(ERROR_TIME, "Session timeout has occured.");
886 case STATE_SENTACCEPT:
887 case STATE_RECEIVEDACCEPT:
888 SetState(STATE_INPROGRESS);
892 // Explicitly ignoring some states here.
899 Session::Session(SessionManager* session_manager,
900 const std::string& local_name,
901 const std::string& initiator_name,
902 const std::string& sid,
903 const std::string& content_type,
904 SessionClient* client)
905 : BaseSession(session_manager->signaling_thread(),
906 session_manager->worker_thread(),
907 session_manager->port_allocator(),
908 sid, content_type, initiator_name == local_name) {
909 ASSERT(client != NULL);
910 session_manager_ = session_manager;
911 local_name_ = local_name;
912 initiator_name_ = initiator_name;
913 transport_parser_ = new P2PTransportParser();
915 initiate_acked_ = false;
916 current_protocol_ = PROTOCOL_HYBRID;
919 Session::~Session() {
920 delete transport_parser_;
923 bool Session::Initiate(const std::string &to,
924 const SessionDescription* sdesc) {
925 ASSERT(signaling_thread()->IsCurrent());
928 // Only from STATE_INIT
929 if (state() != STATE_INIT)
932 // Setup for signaling.
934 set_local_description(sdesc);
935 if (!CreateTransportProxies(GetEmptyTransportInfos(sdesc->contents()),
937 LOG(LS_ERROR) << "Could not create transports: " << error.text;
941 if (!SendInitiateMessage(sdesc, &error)) {
942 LOG(LS_ERROR) << "Could not send initiate message: " << error.text;
946 // We need to connect transport proxy and impl here so that we can process
947 // the TransportDescriptions.
948 SpeculativelyConnectAllTransportChannels();
950 PushdownTransportDescription(CS_LOCAL, CA_OFFER, NULL);
951 SetState(Session::STATE_SENTINITIATE);
955 bool Session::Accept(const SessionDescription* sdesc) {
956 ASSERT(signaling_thread()->IsCurrent());
958 // Only if just received initiate
959 if (state() != STATE_RECEIVEDINITIATE)
962 // Setup for signaling.
963 set_local_description(sdesc);
966 if (!SendAcceptMessage(sdesc, &error)) {
967 LOG(LS_ERROR) << "Could not send accept message: " << error.text;
970 // TODO(juberti): Add BUNDLE support to transport-info messages.
971 PushdownTransportDescription(CS_LOCAL, CA_ANSWER, NULL);
972 MaybeEnableMuxingSupport(); // Enable transport channel mux if supported.
973 SetState(Session::STATE_SENTACCEPT);
977 bool Session::Reject(const std::string& reason) {
978 ASSERT(signaling_thread()->IsCurrent());
980 // Reject is sent in response to an initiate or modify, to reject the
982 if (state() != STATE_RECEIVEDINITIATE && state() != STATE_RECEIVEDMODIFY)
986 if (!SendRejectMessage(reason, &error)) {
987 LOG(LS_ERROR) << "Could not send reject message: " << error.text;
991 SetState(STATE_SENTREJECT);
995 bool Session::TerminateWithReason(const std::string& reason) {
996 ASSERT(signaling_thread()->IsCurrent());
998 // Either side can terminate, at any time.
1000 case STATE_SENTTERMINATE:
1001 case STATE_RECEIVEDTERMINATE:
1004 case STATE_SENTREJECT:
1005 case STATE_RECEIVEDREJECT:
1006 // We don't need to send terminate if we sent or received a reject...
1012 if (!SendTerminateMessage(reason, &error)) {
1013 LOG(LS_ERROR) << "Could not send terminate message: " << error.text;
1019 SetState(STATE_SENTTERMINATE);
1023 bool Session::SendInfoMessage(const XmlElements& elems,
1024 const std::string& remote_name) {
1025 ASSERT(signaling_thread()->IsCurrent());
1027 if (!SendMessage(ACTION_SESSION_INFO, elems, remote_name, &error)) {
1028 LOG(LS_ERROR) << "Could not send info message " << error.text;
1034 bool Session::SendDescriptionInfoMessage(const ContentInfos& contents) {
1036 WriteError write_error;
1037 if (!WriteDescriptionInfo(current_protocol_,
1039 GetContentParsers(),
1040 &elems, &write_error)) {
1041 LOG(LS_ERROR) << "Could not write description info message: "
1042 << write_error.text;
1046 if (!SendMessage(ACTION_DESCRIPTION_INFO, elems, &error)) {
1047 LOG(LS_ERROR) << "Could not send description info message: "
1054 TransportInfos Session::GetEmptyTransportInfos(
1055 const ContentInfos& contents) const {
1056 TransportInfos tinfos;
1057 for (ContentInfos::const_iterator content = contents.begin();
1058 content != contents.end(); ++content) {
1059 tinfos.push_back(TransportInfo(content->name,
1060 TransportDescription(transport_type(),
1067 bool Session::OnRemoteCandidates(
1068 const TransportInfos& tinfos, ParseError* error) {
1069 for (TransportInfos::const_iterator tinfo = tinfos.begin();
1070 tinfo != tinfos.end(); ++tinfo) {
1071 std::string str_error;
1072 if (!BaseSession::OnRemoteCandidates(
1073 tinfo->content_name, tinfo->description.candidates, &str_error)) {
1074 return BadParse(str_error, error);
1080 bool Session::CreateTransportProxies(const TransportInfos& tinfos,
1081 SessionError* error) {
1082 for (TransportInfos::const_iterator tinfo = tinfos.begin();
1083 tinfo != tinfos.end(); ++tinfo) {
1084 if (tinfo->description.transport_type != transport_type()) {
1085 error->SetText("No supported transport in offer.");
1089 GetOrCreateTransportProxy(tinfo->content_name);
1094 TransportParserMap Session::GetTransportParsers() {
1095 TransportParserMap parsers;
1096 parsers[transport_type()] = transport_parser_;
1100 CandidateTranslatorMap Session::GetCandidateTranslators() {
1101 CandidateTranslatorMap translators;
1102 // NOTE: This technique makes it impossible to parse G-ICE
1103 // candidates in session-initiate messages because the channels
1104 // aren't yet created at that point. Since we don't use candidates
1105 // in session-initiate messages, we should be OK. Once we switch to
1106 // ICE, this translation shouldn't be necessary.
1107 for (TransportMap::const_iterator iter = transport_proxies().begin();
1108 iter != transport_proxies().end(); ++iter) {
1109 translators[iter->first] = iter->second;
1114 ContentParserMap Session::GetContentParsers() {
1115 ContentParserMap parsers;
1116 parsers[content_type()] = client_;
1117 // We need to be able parse both RTP-based and SCTP-based Jingle
1118 // with the same client.
1119 if (content_type() == NS_JINGLE_RTP) {
1120 parsers[NS_JINGLE_DRAFT_SCTP] = client_;
1125 void Session::OnTransportRequestSignaling(Transport* transport) {
1126 ASSERT(signaling_thread()->IsCurrent());
1127 TransportProxy* transproxy = GetTransportProxy(transport);
1128 ASSERT(transproxy != NULL);
1130 // Reset candidate allocation status for the transport proxy.
1131 transproxy->set_candidates_allocated(false);
1133 SignalRequestSignaling(this);
1136 void Session::OnTransportConnecting(Transport* transport) {
1137 // This is an indication that we should begin watching the writability
1138 // state of the transport.
1139 OnTransportWritable(transport);
1142 void Session::OnTransportWritable(Transport* transport) {
1143 ASSERT(signaling_thread()->IsCurrent());
1145 // If the transport is not writable, start a timer to make sure that it
1146 // becomes writable within a reasonable amount of time. If it does not, we
1147 // terminate since we can't actually send data. If the transport is writable,
1148 // cancel the timer. Note that writability transitions may occur repeatedly
1149 // during the lifetime of the session.
1150 signaling_thread()->Clear(this, MSG_TIMEOUT);
1151 if (transport->HasChannels() && !transport->writable()) {
1152 signaling_thread()->PostDelayed(
1153 session_manager_->session_timeout() * 1000, this, MSG_TIMEOUT);
1157 void Session::OnTransportProxyCandidatesReady(TransportProxy* transproxy,
1158 const Candidates& candidates) {
1159 ASSERT(signaling_thread()->IsCurrent());
1160 if (transproxy != NULL) {
1161 if (initiator() && !initiate_acked_) {
1162 // TODO: This is to work around server re-ordering
1163 // messages. We send the candidates once the session-initiate
1164 // is acked. Once we have fixed the server to guarantee message
1165 // order, we can remove this case.
1166 transproxy->AddUnsentCandidates(candidates);
1168 if (!transproxy->negotiated()) {
1169 transproxy->AddSentCandidates(candidates);
1172 if (!SendTransportInfoMessage(transproxy, candidates, &error)) {
1173 LOG(LS_ERROR) << "Could not send transport info message: "
1181 void Session::OnTransportSendError(Transport* transport,
1182 const buzz::XmlElement* stanza,
1183 const buzz::QName& name,
1184 const std::string& type,
1185 const std::string& text,
1186 const buzz::XmlElement* extra_info) {
1187 ASSERT(signaling_thread()->IsCurrent());
1188 SignalErrorMessage(this, stanza, name, type, text, extra_info);
1191 void Session::OnIncomingMessage(const SessionMessage& msg) {
1192 ASSERT(signaling_thread()->IsCurrent());
1193 ASSERT(state() == STATE_INIT || msg.from == remote_name());
1195 if (current_protocol_== PROTOCOL_HYBRID) {
1196 if (msg.protocol == PROTOCOL_GINGLE) {
1197 current_protocol_ = PROTOCOL_GINGLE;
1199 current_protocol_ = PROTOCOL_JINGLE;
1206 case ACTION_SESSION_INITIATE:
1207 valid = OnInitiateMessage(msg, &error);
1209 case ACTION_SESSION_INFO:
1210 valid = OnInfoMessage(msg);
1212 case ACTION_SESSION_ACCEPT:
1213 valid = OnAcceptMessage(msg, &error);
1215 case ACTION_SESSION_REJECT:
1216 valid = OnRejectMessage(msg, &error);
1218 case ACTION_SESSION_TERMINATE:
1219 valid = OnTerminateMessage(msg, &error);
1221 case ACTION_TRANSPORT_INFO:
1222 valid = OnTransportInfoMessage(msg, &error);
1224 case ACTION_TRANSPORT_ACCEPT:
1225 valid = OnTransportAcceptMessage(msg, &error);
1227 case ACTION_DESCRIPTION_INFO:
1228 valid = OnDescriptionInfoMessage(msg, &error);
1231 valid = BadMessage(buzz::QN_STANZA_BAD_REQUEST,
1232 "unknown session message type",
1237 SendAcknowledgementMessage(msg.stanza);
1239 SignalErrorMessage(this, msg.stanza, error.type,
1240 "modify", error.text, NULL);
1244 void Session::OnIncomingResponse(const buzz::XmlElement* orig_stanza,
1245 const buzz::XmlElement* response_stanza,
1246 const SessionMessage& msg) {
1247 ASSERT(signaling_thread()->IsCurrent());
1249 if (msg.type == ACTION_SESSION_INITIATE) {
1254 void Session::OnInitiateAcked() {
1255 // TODO: This is to work around server re-ordering
1256 // messages. We send the candidates once the session-initiate
1257 // is acked. Once we have fixed the server to guarantee message
1258 // order, we can remove this case.
1259 if (!initiate_acked_) {
1260 initiate_acked_ = true;
1262 SendAllUnsentTransportInfoMessages(&error);
1266 void Session::OnFailedSend(const buzz::XmlElement* orig_stanza,
1267 const buzz::XmlElement* error_stanza) {
1268 ASSERT(signaling_thread()->IsCurrent());
1271 ParseError parse_error;
1272 if (!ParseSessionMessage(orig_stanza, &msg, &parse_error)) {
1273 LOG(LS_ERROR) << "Error parsing failed send: " << parse_error.text
1274 << ":" << orig_stanza;
1278 // If the error is a session redirect, call OnRedirectError, which will
1279 // continue the session with a new remote JID.
1280 SessionRedirect redirect;
1281 if (FindSessionRedirect(error_stanza, &redirect)) {
1283 if (!OnRedirectError(redirect, &error)) {
1284 // TODO: Should we send a message back? The standard
1285 // says nothing about it.
1286 std::ostringstream desc;
1287 desc << "Failed to redirect: " << error.text;
1288 LOG(LS_ERROR) << desc.str();
1289 SetError(ERROR_RESPONSE, desc.str());
1294 std::string error_type = "cancel";
1296 const buzz::XmlElement* error = error_stanza->FirstNamed(buzz::QN_ERROR);
1298 error_type = error->Attr(buzz::QN_TYPE);
1300 LOG(LS_ERROR) << "Session error:\n" << error->Str() << "\n"
1301 << "in response to:\n" << orig_stanza->Str();
1303 // don't crash if <error> is missing
1304 LOG(LS_ERROR) << "Session error without <error/> element, ignoring";
1308 if (msg.type == ACTION_TRANSPORT_INFO) {
1309 // Transport messages frequently generate errors because they are sent right
1310 // when we detect a network failure. For that reason, we ignore such
1311 // errors, because if we do not establish writability again, we will
1312 // terminate anyway. The exceptions are transport-specific error tags,
1313 // which we pass on to the respective transport.
1314 } else if ((error_type != "continue") && (error_type != "wait")) {
1315 // We do not set an error if the other side said it is okay to continue
1316 // (possibly after waiting). These errors can be ignored.
1317 SetError(ERROR_RESPONSE, "");
1321 bool Session::OnInitiateMessage(const SessionMessage& msg,
1322 MessageError* error) {
1323 if (!CheckState(STATE_INIT, error))
1326 SessionInitiate init;
1327 if (!ParseSessionInitiate(msg.protocol, msg.action_elem,
1328 GetContentParsers(), GetTransportParsers(),
1329 GetCandidateTranslators(),
1333 SessionError session_error;
1334 if (!CreateTransportProxies(init.transports, &session_error)) {
1335 return BadMessage(buzz::QN_STANZA_NOT_ACCEPTABLE,
1336 session_error.text, error);
1339 set_remote_name(msg.from);
1340 set_initiator_name(msg.initiator);
1341 set_remote_description(new SessionDescription(init.ClearContents(),
1344 // Updating transport with TransportDescription.
1345 PushdownTransportDescription(CS_REMOTE, CA_OFFER, NULL);
1346 SetState(STATE_RECEIVEDINITIATE);
1348 // Users of Session may listen to state change and call Reject().
1349 if (state() != STATE_SENTREJECT) {
1350 if (!OnRemoteCandidates(init.transports, error))
1353 // TODO(juberti): Auto-generate and push down the local transport answer.
1354 // This is necessary for trickling to work with RFC 5245 ICE.
1359 bool Session::OnAcceptMessage(const SessionMessage& msg, MessageError* error) {
1360 if (!CheckState(STATE_SENTINITIATE, error))
1363 SessionAccept accept;
1364 if (!ParseSessionAccept(msg.protocol, msg.action_elem,
1365 GetContentParsers(), GetTransportParsers(),
1366 GetCandidateTranslators(),
1371 // If we get an accept, we can assume the initiate has been
1372 // received, even if we haven't gotten an IQ response.
1375 set_remote_description(new SessionDescription(accept.ClearContents(),
1378 // Updating transport with TransportDescription.
1379 PushdownTransportDescription(CS_REMOTE, CA_ANSWER, NULL);
1380 MaybeEnableMuxingSupport(); // Enable transport channel mux if supported.
1381 SetState(STATE_RECEIVEDACCEPT);
1383 if (!OnRemoteCandidates(accept.transports, error))
1389 bool Session::OnRejectMessage(const SessionMessage& msg, MessageError* error) {
1390 if (!CheckState(STATE_SENTINITIATE, error))
1393 SetState(STATE_RECEIVEDREJECT);
1397 bool Session::OnInfoMessage(const SessionMessage& msg) {
1398 SignalInfoMessage(this, msg.action_elem);
1402 bool Session::OnTerminateMessage(const SessionMessage& msg,
1403 MessageError* error) {
1404 SessionTerminate term;
1405 if (!ParseSessionTerminate(msg.protocol, msg.action_elem, &term, error))
1408 SignalReceivedTerminateReason(this, term.reason);
1409 if (term.debug_reason != buzz::STR_EMPTY) {
1410 LOG(LS_VERBOSE) << "Received error on call: " << term.debug_reason;
1413 SetState(STATE_RECEIVEDTERMINATE);
1417 bool Session::OnTransportInfoMessage(const SessionMessage& msg,
1418 MessageError* error) {
1419 TransportInfos tinfos;
1420 if (!ParseTransportInfos(msg.protocol, msg.action_elem,
1421 initiator_description()->contents(),
1422 GetTransportParsers(), GetCandidateTranslators(),
1426 if (!OnRemoteCandidates(tinfos, error))
1432 bool Session::OnTransportAcceptMessage(const SessionMessage& msg,
1433 MessageError* error) {
1434 // TODO: Currently here only for compatibility with
1435 // Gingle 1.1 clients (notably, Google Voice).
1439 bool Session::OnDescriptionInfoMessage(const SessionMessage& msg,
1440 MessageError* error) {
1441 if (!CheckState(STATE_INPROGRESS, error))
1444 DescriptionInfo description_info;
1445 if (!ParseDescriptionInfo(msg.protocol, msg.action_elem,
1446 GetContentParsers(), GetTransportParsers(),
1447 GetCandidateTranslators(),
1448 &description_info, error)) {
1452 ContentInfos& updated_contents = description_info.contents;
1454 // TODO: Currently, reflector sends back
1455 // video stream updates even for an audio-only call, which causes
1456 // this to fail. Put this back once reflector is fixed.
1458 // ContentInfos::iterator it;
1459 // First, ensure all updates are valid before modifying remote_description_.
1460 // for (it = updated_contents.begin(); it != updated_contents.end(); ++it) {
1461 // if (remote_description()->GetContentByName(it->name) == NULL) {
1466 // TODO: We used to replace contents from an update, but
1467 // that no longer works with partial updates. We need to figure out
1468 // a way to merge patial updates into contents. For now, users of
1469 // Session should listen to SignalRemoteDescriptionUpdate and handle
1470 // updates. They should not expect remote_description to be the
1473 // for (it = updated_contents.begin(); it != updated_contents.end(); ++it) {
1474 // remote_description()->RemoveContentByName(it->name);
1475 // remote_description()->AddContent(it->name, it->type, it->description);
1479 SignalRemoteDescriptionUpdate(this, updated_contents);
1483 bool BareJidsEqual(const std::string& name1,
1484 const std::string& name2) {
1485 buzz::Jid jid1(name1);
1486 buzz::Jid jid2(name2);
1488 return jid1.IsValid() && jid2.IsValid() && jid1.BareEquals(jid2);
1491 bool Session::OnRedirectError(const SessionRedirect& redirect,
1492 SessionError* error) {
1493 MessageError message_error;
1494 if (!CheckState(STATE_SENTINITIATE, &message_error)) {
1495 return BadWrite(message_error.text, error);
1498 if (!BareJidsEqual(remote_name(), redirect.target))
1499 return BadWrite("Redirection not allowed: must be the same bare jid.",
1502 // When we receive a redirect, we point the session at the new JID
1503 // and resend the candidates.
1504 set_remote_name(redirect.target);
1505 return (SendInitiateMessage(local_description(), error) &&
1506 ResendAllTransportInfoMessages(error));
1509 bool Session::CheckState(State expected, MessageError* error) {
1510 if (state() != expected) {
1511 // The server can deliver messages out of order/repeated for various
1512 // reasons. For example, if the server does not recive our iq response,
1513 // it could assume that the iq it sent was lost, and will then send
1514 // it again. Ideally, we should implement reliable messaging with
1515 // duplicate elimination.
1516 return BadMessage(buzz::QN_STANZA_NOT_ALLOWED,
1517 "message not allowed in current state",
1523 void Session::SetError(Error error, const std::string& error_desc) {
1524 BaseSession::SetError(error, error_desc);
1525 if (error != ERROR_NONE)
1526 signaling_thread()->Post(this, MSG_ERROR);
1529 void Session::OnMessage(talk_base::Message* pmsg) {
1530 // preserve this because BaseSession::OnMessage may modify it
1531 State orig_state = state();
1533 BaseSession::OnMessage(pmsg);
1535 switch (pmsg->message_id) {
1537 TerminateWithReason(STR_TERMINATE_ERROR);
1541 switch (orig_state) {
1542 case STATE_SENTREJECT:
1543 case STATE_RECEIVEDREJECT:
1544 // Assume clean termination.
1548 case STATE_SENTTERMINATE:
1549 case STATE_RECEIVEDTERMINATE:
1550 session_manager_->DestroySession(this);
1554 // Explicitly ignoring some states here.
1561 bool Session::SendInitiateMessage(const SessionDescription* sdesc,
1562 SessionError* error) {
1563 SessionInitiate init;
1564 init.contents = sdesc->contents();
1565 init.transports = GetEmptyTransportInfos(init.contents);
1566 init.groups = sdesc->groups();
1567 return SendMessage(ACTION_SESSION_INITIATE, init, error);
1570 bool Session::WriteSessionAction(
1571 SignalingProtocol protocol, const SessionInitiate& init,
1572 XmlElements* elems, WriteError* error) {
1573 return WriteSessionInitiate(protocol, init.contents, init.transports,
1574 GetContentParsers(), GetTransportParsers(),
1575 GetCandidateTranslators(), init.groups,
1579 bool Session::SendAcceptMessage(const SessionDescription* sdesc,
1580 SessionError* error) {
1582 if (!WriteSessionAccept(current_protocol_,
1584 GetEmptyTransportInfos(sdesc->contents()),
1585 GetContentParsers(), GetTransportParsers(),
1586 GetCandidateTranslators(), sdesc->groups(),
1590 return SendMessage(ACTION_SESSION_ACCEPT, elems, error);
1593 bool Session::SendRejectMessage(const std::string& reason,
1594 SessionError* error) {
1595 SessionTerminate term(reason);
1596 return SendMessage(ACTION_SESSION_REJECT, term, error);
1599 bool Session::SendTerminateMessage(const std::string& reason,
1600 SessionError* error) {
1601 SessionTerminate term(reason);
1602 return SendMessage(ACTION_SESSION_TERMINATE, term, error);
1605 bool Session::WriteSessionAction(SignalingProtocol protocol,
1606 const SessionTerminate& term,
1607 XmlElements* elems, WriteError* error) {
1608 WriteSessionTerminate(protocol, term, elems);
1612 bool Session::SendTransportInfoMessage(const TransportInfo& tinfo,
1613 SessionError* error) {
1614 return SendMessage(ACTION_TRANSPORT_INFO, tinfo, error);
1617 bool Session::SendTransportInfoMessage(const TransportProxy* transproxy,
1618 const Candidates& candidates,
1619 SessionError* error) {
1620 return SendTransportInfoMessage(TransportInfo(transproxy->content_name(),
1621 TransportDescription(transproxy->type(), std::vector<std::string>(),
1622 std::string(), std::string(), ICEMODE_FULL,
1623 CONNECTIONROLE_NONE, NULL, candidates)), error);
1626 bool Session::WriteSessionAction(SignalingProtocol protocol,
1627 const TransportInfo& tinfo,
1628 XmlElements* elems, WriteError* error) {
1629 TransportInfos tinfos;
1630 tinfos.push_back(tinfo);
1631 return WriteTransportInfos(protocol, tinfos,
1632 GetTransportParsers(), GetCandidateTranslators(),
1636 bool Session::ResendAllTransportInfoMessages(SessionError* error) {
1637 for (TransportMap::const_iterator iter = transport_proxies().begin();
1638 iter != transport_proxies().end(); ++iter) {
1639 TransportProxy* transproxy = iter->second;
1640 if (transproxy->sent_candidates().size() > 0) {
1641 if (!SendTransportInfoMessage(
1642 transproxy, transproxy->sent_candidates(), error)) {
1643 LOG(LS_ERROR) << "Could not resend transport info messages: "
1647 transproxy->ClearSentCandidates();
1653 bool Session::SendAllUnsentTransportInfoMessages(SessionError* error) {
1654 for (TransportMap::const_iterator iter = transport_proxies().begin();
1655 iter != transport_proxies().end(); ++iter) {
1656 TransportProxy* transproxy = iter->second;
1657 if (transproxy->unsent_candidates().size() > 0) {
1658 if (!SendTransportInfoMessage(
1659 transproxy, transproxy->unsent_candidates(), error)) {
1660 LOG(LS_ERROR) << "Could not send unsent transport info messages: "
1664 transproxy->ClearUnsentCandidates();
1670 bool Session::SendMessage(ActionType type, const XmlElements& action_elems,
1671 SessionError* error) {
1672 return SendMessage(type, action_elems, remote_name(), error);
1675 bool Session::SendMessage(ActionType type, const XmlElements& action_elems,
1676 const std::string& remote_name, SessionError* error) {
1677 talk_base::scoped_ptr<buzz::XmlElement> stanza(
1678 new buzz::XmlElement(buzz::QN_IQ));
1680 SessionMessage msg(current_protocol_, type, id(), initiator_name());
1681 msg.to = remote_name;
1682 WriteSessionMessage(msg, action_elems, stanza.get());
1684 SignalOutgoingMessage(this, stanza.get());
1688 template <typename Action>
1689 bool Session::SendMessage(ActionType type, const Action& action,
1690 SessionError* error) {
1691 talk_base::scoped_ptr<buzz::XmlElement> stanza(
1692 new buzz::XmlElement(buzz::QN_IQ));
1693 if (!WriteActionMessage(type, action, stanza.get(), error))
1696 SignalOutgoingMessage(this, stanza.get());
1700 template <typename Action>
1701 bool Session::WriteActionMessage(ActionType type, const Action& action,
1702 buzz::XmlElement* stanza,
1703 WriteError* error) {
1704 if (current_protocol_ == PROTOCOL_HYBRID) {
1705 if (!WriteActionMessage(PROTOCOL_JINGLE, type, action, stanza, error))
1707 if (!WriteActionMessage(PROTOCOL_GINGLE, type, action, stanza, error))
1710 if (!WriteActionMessage(current_protocol_, type, action, stanza, error))
1716 template <typename Action>
1717 bool Session::WriteActionMessage(SignalingProtocol protocol,
1718 ActionType type, const Action& action,
1719 buzz::XmlElement* stanza, WriteError* error) {
1720 XmlElements action_elems;
1721 if (!WriteSessionAction(protocol, action, &action_elems, error))
1724 SessionMessage msg(protocol, type, id(), initiator_name());
1725 msg.to = remote_name();
1727 WriteSessionMessage(msg, action_elems, stanza);
1731 void Session::SendAcknowledgementMessage(const buzz::XmlElement* stanza) {
1732 talk_base::scoped_ptr<buzz::XmlElement> ack(
1733 new buzz::XmlElement(buzz::QN_IQ));
1734 ack->SetAttr(buzz::QN_TO, remote_name());
1735 ack->SetAttr(buzz::QN_ID, stanza->Attr(buzz::QN_ID));
1736 ack->SetAttr(buzz::QN_TYPE, "result");
1738 SignalOutgoingMessage(this, ack.get());
1741 } // namespace cricket