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/examples/call/callclient.h"
32 #include "talk/examples/call/console.h"
33 #include "talk/examples/call/friendinvitesendtask.h"
34 #include "talk/examples/call/muc.h"
35 #include "talk/examples/call/mucinviterecvtask.h"
36 #include "talk/examples/call/mucinvitesendtask.h"
37 #include "talk/examples/call/presencepushtask.h"
38 #include "talk/media/base/mediacommon.h"
39 #include "talk/media/base/mediaengine.h"
40 #include "talk/media/base/rtpdataengine.h"
41 #include "talk/media/base/screencastid.h"
42 #include "webrtc/base/helpers.h"
43 #include "webrtc/base/logging.h"
44 #include "webrtc/base/network.h"
45 #include "webrtc/base/socketaddress.h"
46 #include "webrtc/base/stringencode.h"
47 #include "webrtc/base/stringutils.h"
48 #include "webrtc/base/thread.h"
49 #include "webrtc/base/windowpickerfactory.h"
51 #include "talk/media/sctp/sctpdataengine.h"
53 #include "talk/media/base/videorenderer.h"
54 #include "talk/media/devices/devicemanager.h"
55 #include "talk/media/devices/videorendererfactory.h"
56 #include "talk/p2p/base/sessionmanager.h"
57 #include "talk/p2p/client/basicportallocator.h"
58 #include "talk/p2p/client/sessionmanagertask.h"
59 #include "talk/session/media/mediamessages.h"
60 #include "talk/session/media/mediasessionclient.h"
61 #include "talk/xmpp/constants.h"
62 #include "talk/xmpp/hangoutpubsubclient.h"
63 #include "talk/xmpp/mucroomconfigtask.h"
64 #include "talk/xmpp/mucroomlookuptask.h"
65 #include "talk/xmpp/pingtask.h"
66 #include "talk/xmpp/presenceouttask.h"
70 // Must be period >= timeout.
71 const uint32 kPingPeriodMillis = 10000;
72 const uint32 kPingTimeoutMillis = 10000;
74 const char* DescribeStatus(buzz::PresenceStatus::Show show,
75 const std::string& desc) {
77 case buzz::PresenceStatus::SHOW_XA: return desc.c_str();
78 case buzz::PresenceStatus::SHOW_ONLINE: return "online";
79 case buzz::PresenceStatus::SHOW_AWAY: return "away";
80 case buzz::PresenceStatus::SHOW_DND: return "do not disturb";
81 case buzz::PresenceStatus::SHOW_CHAT: return "ready to chat";
82 default: return "offline";
86 std::string GetWord(const std::vector<std::string>& words,
87 size_t index, const std::string& def) {
88 if (words.size() > index) {
95 int GetInt(const std::vector<std::string>& words, size_t index, int def) {
97 if (words.size() > index && rtc::FromString(words[index], &val)) {
106 const char* CALL_COMMANDS =
107 "Available commands:\n"
109 " hangup Ends the call.\n"
110 " hold Puts the current call on hold\n"
111 " calls Lists the current calls and their sessions\n"
112 " switch [call_id] Switch to the specified call\n"
113 " addsession [jid] Add a new session to the current call.\n"
114 " rmsession [sid] Remove specified session.\n"
115 " mute Stops sending voice.\n"
116 " unmute Re-starts sending voice.\n"
117 " vmute Stops sending video.\n"
118 " vunmute Re-starts sending video.\n"
119 " dtmf Sends a DTMF tone.\n"
120 " stats Print voice stats for the current call.\n"
121 " quit Quits the application.\n"
124 // TODO: Make present and record really work.
125 const char* HANGOUT_COMMANDS =
126 "Available MUC commands:\n"
128 " present Starts presenting (just signalling; not actually presenting.)\n"
129 " unpresent Stops presenting (just signalling; not actually presenting.)\n"
130 " record Starts recording (just signalling; not actually recording.)\n"
131 " unrecord Stops recording (just signalling; not actually recording.)\n"
132 " rmute [nick] Remote mute another participant.\n"
133 " block [nick] Block another participant.\n"
134 " screencast [fps] Starts screencast. \n"
135 " unscreencast Stops screencast. \n"
136 " quit Quits the application.\n"
139 const char* RECEIVE_COMMANDS =
140 "Available commands:\n"
142 " accept [bw] Accepts the incoming call and switches to it.\n"
143 " reject Rejects the incoming call and stays with the current call.\n"
144 " quit Quits the application.\n"
147 const char* CONSOLE_COMMANDS =
148 "Available commands:\n"
150 " roster Prints the online friends from your roster.\n"
151 " friend user Request to add a user to your roster.\n"
152 " call [jid] [bw] Initiates a call to the user[/room] with the\n"
153 " given JID and with optional bandwidth.\n"
154 " vcall [jid] [bw] Initiates a video call to the user[/room] with\n"
155 " the given JID and with optional bandwidth.\n"
156 " calls Lists the current calls\n"
157 " switch [call_id] Switch to the specified call\n"
158 " join [room_jid] Joins a multi-user-chat with room JID.\n"
159 " ljoin [room_name] Joins a MUC by looking up JID from room name.\n"
160 " invite user [room] Invites a friend to a multi-user-chat.\n"
161 " leave [room] Leaves a multi-user-chat.\n"
162 " nick [nick] Sets the nick.\n"
163 " priority [int] Sets the priority.\n"
164 " getdevs Prints the available media devices.\n"
165 " quit Quits the application.\n"
168 void CallClient::ParseLine(const std::string& line) {
169 std::vector<std::string> words;
172 for (int index = 0; index <= static_cast<int>(line.size()); ++index) {
174 if (!isspace(line[index])) {
181 if (isspace(line[index])) {
182 std::string word(line, start, index - start);
183 words.push_back(word);
191 const std::string& command = GetWord(words, 0, "");
192 if (command == "quit") {
194 } else if (call_ && incoming_call_) {
195 if (command == "accept") {
196 cricket::CallOptions options;
197 options.video_bandwidth = GetInt(words, 1, cricket::kAutoBandwidth);
198 options.has_video = true;
199 options.data_channel_type = data_channel_type_;
201 } else if (command == "reject") {
204 console_->PrintLine(RECEIVE_COMMANDS);
207 if (command == "hangup") {
209 } else if (command == "hold") {
210 media_client_->SetFocus(NULL);
212 } else if (command == "addsession") {
213 std::string to = GetWord(words, 1, "");
214 cricket::CallOptions options;
215 options.has_video = call_->has_video();
216 options.video_bandwidth = cricket::kAutoBandwidth;
217 options.data_channel_type = data_channel_type_;
218 options.AddStream(cricket::MEDIA_TYPE_VIDEO, "", "");
219 if (!InitiateAdditionalSession(to, options)) {
220 console_->PrintLine("Failed to initiate additional session.");
222 } else if (command == "rmsession") {
223 std::string id = GetWord(words, 1, "");
224 TerminateAndRemoveSession(call_, id);
225 } else if (command == "calls") {
227 } else if ((words.size() == 2) && (command == "switch")) {
228 SwitchToCall(GetInt(words, 1, -1));
229 } else if (command == "mute") {
232 hangout_pubsub_client_->PublishAudioMuteState(true);
234 } else if (command == "unmute") {
237 hangout_pubsub_client_->PublishAudioMuteState(false);
239 } else if (command == "vmute") {
240 call_->MuteVideo(true);
242 hangout_pubsub_client_->PublishVideoMuteState(true);
244 } else if (command == "vunmute") {
245 call_->MuteVideo(false);
247 hangout_pubsub_client_->PublishVideoMuteState(false);
249 } else if (command == "screencast") {
250 if (screencast_ssrc_ != 0) {
251 console_->PrintLine("Can't screencast twice. Unscreencast first.");
253 std::string streamid = "screencast";
254 screencast_ssrc_ = rtc::CreateRandomId();
255 int fps = GetInt(words, 1, 5); // Default to 5 fps.
257 cricket::ScreencastId screencastid;
258 cricket::Session* session = GetFirstSession();
259 if (session && SelectFirstDesktopScreencastId(&screencastid)) {
260 call_->StartScreencast(
261 session, streamid, screencast_ssrc_, screencastid, fps);
264 } else if (command == "unscreencast") {
265 // TODO: Use a random ssrc
266 std::string streamid = "screencast";
268 cricket::Session* session = GetFirstSession();
270 call_->StopScreencast(session, streamid, screencast_ssrc_);
271 screencast_ssrc_ = 0;
273 } else if (command == "present") {
275 hangout_pubsub_client_->PublishPresenterState(true);
277 } else if (command == "unpresent") {
279 hangout_pubsub_client_->PublishPresenterState(false);
281 } else if (command == "record") {
283 hangout_pubsub_client_->PublishRecordingState(true);
285 } else if (command == "unrecord") {
287 hangout_pubsub_client_->PublishRecordingState(false);
289 } else if ((command == "rmute") && (words.size() == 2)) {
291 const std::string& nick = words[1];
292 hangout_pubsub_client_->RemoteMute(nick);
294 } else if ((command == "block") && (words.size() == 2)) {
296 const std::string& nick = words[1];
297 hangout_pubsub_client_->BlockMedia(nick);
299 } else if (command == "senddata") {
300 // "" is the default streamid.
301 SendData("", words[1]);
302 } else if ((command == "dtmf") && (words.size() == 2)) {
303 int ev = std::string("0123456789*#").find(words[1][0]);
304 call_->PressDTMF(ev);
305 } else if (command == "stats") {
308 console_->PrintLine(CALL_COMMANDS);
310 console_->PrintLine(HANGOUT_COMMANDS);
314 if (command == "roster") {
316 } else if (command == "send") {
317 buzz::Jid jid(words[1]);
319 last_sent_to_ = words[1];
320 SendChat(words[1], words[2]);
321 } else if (!last_sent_to_.empty()) {
322 SendChat(last_sent_to_, words[1]);
325 "Invalid JID. JIDs should be in the form user@domain");
327 } else if ((words.size() == 2) && (command == "friend")) {
328 InviteFriend(words[1]);
329 } else if (command == "call") {
330 std::string to = GetWord(words, 1, "");
331 cricket::CallOptions options;
332 options.data_channel_type = data_channel_type_;
333 if (!PlaceCall(to, options)) {
334 console_->PrintLine("Failed to initiate call.");
336 } else if (command == "vcall") {
337 std::string to = GetWord(words, 1, "");
338 int bandwidth = GetInt(words, 2, cricket::kAutoBandwidth);
339 cricket::CallOptions options;
340 options.has_video = true;
341 options.video_bandwidth = bandwidth;
342 options.data_channel_type = data_channel_type_;
343 if (!PlaceCall(to, options)) {
344 console_->PrintLine("Failed to initiate call.");
346 } else if (command == "calls") {
348 } else if ((words.size() == 2) && (command == "switch")) {
349 SwitchToCall(GetInt(words, 1, -1));
350 } else if (command == "join") {
351 JoinMuc(GetWord(words, 1, ""));
352 } else if (command == "ljoin") {
353 LookupAndJoinMuc(GetWord(words, 1, ""));
354 } else if ((words.size() >= 2) && (command == "invite")) {
355 InviteToMuc(words[1], GetWord(words, 2, ""));
356 } else if (command == "leave") {
357 LeaveMuc(GetWord(words, 1, ""));
358 } else if (command == "nick") {
359 SetNick(GetWord(words, 1, ""));
360 } else if (command == "priority") {
361 int priority = GetInt(words, 1, 0);
362 SetPriority(priority);
364 } else if (command == "getdevs") {
366 } else if ((words.size() == 2) && (command == "setvol")) {
369 console_->PrintLine(CONSOLE_COMMANDS);
374 CallClient::CallClient(buzz::XmppClient* xmpp_client,
375 const std::string& caps_node, const std::string& version)
376 : xmpp_client_(xmpp_client),
377 worker_thread_(NULL),
382 hangout_pubsub_client_(NULL),
383 incoming_call_(false),
385 pmuc_domain_("groupchat.google.com"),
387 data_channel_type_(cricket::DCT_NONE),
388 multisession_enabled_(false),
389 local_renderer_(NULL),
390 static_views_accumulated_count_(0),
392 roster_(new RosterMap),
393 portallocator_flags_(0),
394 allow_local_ips_(false),
395 signaling_protocol_(cricket::PROTOCOL_HYBRID),
396 transport_protocol_(cricket::ICEPROTO_HYBRID),
397 sdes_policy_(cricket::SEC_DISABLED),
398 dtls_policy_(cricket::SEC_DISABLED),
400 show_roster_messages_(false) {
401 xmpp_client_->SignalStateChange.connect(this, &CallClient::OnStateChange);
402 my_status_.set_caps_node(caps_node);
403 my_status_.set_version(version);
406 CallClient::~CallClient() {
407 delete media_client_;
409 delete worker_thread_;
412 const std::string CallClient::strerror(buzz::XmppEngine::Error err) {
414 case buzz::XmppEngine::ERROR_NONE:
416 case buzz::XmppEngine::ERROR_XML:
417 return "Malformed XML or encoding error";
418 case buzz::XmppEngine::ERROR_STREAM:
419 return "XMPP stream error";
420 case buzz::XmppEngine::ERROR_VERSION:
421 return "XMPP version error";
422 case buzz::XmppEngine::ERROR_UNAUTHORIZED:
423 return "User is not authorized (Check your username and password)";
424 case buzz::XmppEngine::ERROR_TLS:
425 return "TLS could not be negotiated";
426 case buzz::XmppEngine::ERROR_AUTH:
427 return "Authentication could not be negotiated";
428 case buzz::XmppEngine::ERROR_BIND:
429 return "Resource or session binding could not be negotiated";
430 case buzz::XmppEngine::ERROR_CONNECTION_CLOSED:
431 return "Connection closed by output handler.";
432 case buzz::XmppEngine::ERROR_DOCUMENT_CLOSED:
433 return "Closed by </stream:stream>";
434 case buzz::XmppEngine::ERROR_SOCKET:
435 return "Socket error";
437 return "Unknown error";
441 void CallClient::OnCallDestroy(cricket::Call* call) {
442 RemoveCallsStaticRenderedViews(call);
444 if (local_renderer_) {
445 delete local_renderer_;
446 local_renderer_ = NULL;
448 console_->PrintLine("call destroyed");
450 delete hangout_pubsub_client_;
451 hangout_pubsub_client_ = NULL;
455 void CallClient::OnStateChange(buzz::XmppEngine::State state) {
457 case buzz::XmppEngine::STATE_START:
458 console_->PrintLine("connecting...");
460 case buzz::XmppEngine::STATE_OPENING:
461 console_->PrintLine("logging in...");
463 case buzz::XmppEngine::STATE_OPEN:
464 console_->PrintLine("logged in...");
468 case buzz::XmppEngine::STATE_CLOSED:
470 buzz::XmppEngine::Error error = xmpp_client_->GetError(NULL);
471 console_->PrintLine("logged out... %s", strerror(error).c_str());
480 void CallClient::InitMedia() {
481 worker_thread_ = new rtc::Thread();
482 // The worker thread must be started here since initialization of
483 // the ChannelManager will generate messages that need to be
485 worker_thread_->Start();
487 // TODO: It looks like we are leaking many objects. E.g.
488 // |network_manager_| is never deleted.
489 network_manager_ = new rtc::BasicNetworkManager();
491 // TODO: Decide if the relay address should be specified here.
492 rtc::SocketAddress stun_addr("stun.l.google.com", 19302);
493 cricket::ServerAddresses stun_servers;
494 stun_servers.insert(stun_addr);
495 port_allocator_ = new cricket::BasicPortAllocator(
496 network_manager_, stun_servers, rtc::SocketAddress(),
497 rtc::SocketAddress(), rtc::SocketAddress());
499 if (portallocator_flags_ != 0) {
500 port_allocator_->set_flags(portallocator_flags_);
502 session_manager_ = new cricket::SessionManager(
503 port_allocator_, worker_thread_);
504 session_manager_->set_secure(dtls_policy_);
505 session_manager_->set_identity(ssl_identity_.get());
506 session_manager_->set_transport_protocol(transport_protocol_);
507 session_manager_->SignalRequestSignaling.connect(
508 this, &CallClient::OnRequestSignaling);
509 session_manager_->SignalSessionCreate.connect(
510 this, &CallClient::OnSessionCreate);
511 session_manager_->OnSignalingReady();
513 session_manager_task_ =
514 new cricket::SessionManagerTask(xmpp_client_, session_manager_);
515 session_manager_task_->EnableOutgoingMessages();
516 session_manager_task_->Start();
518 if (!media_engine_) {
519 media_engine_ = cricket::MediaEngineFactory::Create();
523 if (data_channel_type_ == cricket::DCT_SCTP) {
525 data_engine_ = new cricket::SctpDataEngine();
527 LOG(LS_WARNING) << "SCTP Data Engine not supported.";
528 data_channel_type_ = cricket::DCT_NONE;
529 data_engine_ = new cricket::RtpDataEngine();
532 // Even if we have DCT_NONE, we still have a data engine, just
533 // to make sure it isn't NULL.
534 data_engine_ = new cricket::RtpDataEngine();
538 media_client_ = new cricket::MediaSessionClient(
543 cricket::DeviceManagerFactory::Create());
544 media_client_->SignalCallCreate.connect(this, &CallClient::OnCallCreate);
545 media_client_->SignalCallDestroy.connect(this, &CallClient::OnCallDestroy);
546 media_client_->SignalDevicesChange.connect(this,
547 &CallClient::OnDevicesChange);
548 media_client_->set_secure(sdes_policy_);
549 media_client_->set_multisession_enabled(multisession_enabled_);
552 void CallClient::OnRequestSignaling() {
553 session_manager_->OnSignalingReady();
556 void CallClient::OnSessionCreate(cricket::Session* session, bool initiate) {
557 session->set_current_protocol(signaling_protocol_);
560 void CallClient::OnCallCreate(cricket::Call* call) {
561 call->SignalSessionState.connect(this, &CallClient::OnSessionState);
562 call->SignalMediaStreamsUpdate.connect(
563 this, &CallClient::OnMediaStreamsUpdate);
566 void CallClient::OnSessionState(cricket::Call* call,
567 cricket::Session* session,
568 cricket::Session::State state) {
569 if (state == cricket::Session::STATE_RECEIVEDINITIATE) {
570 buzz::Jid jid(session->remote_name());
571 if (call_ == call && multisession_enabled_) {
572 // We've received an initiate for an existing call. This is actually a
573 // new session for that call.
574 console_->PrintLine("Incoming session from '%s'", jid.Str().c_str());
577 cricket::CallOptions options;
578 options.has_video = call_->has_video();
579 options.data_channel_type = data_channel_type_;
580 call_->AcceptSession(session, options);
582 if (call_->has_video() && render_) {
583 RenderAllStreams(call, session, true);
586 console_->PrintLine("Incoming call from '%s'", jid.Str().c_str());
589 incoming_call_ = true;
590 if (call->has_video() && render_) {
592 cricket::VideoRendererFactory::CreateGuiVideoRenderer(160, 100);
595 cricket::CallOptions options;
596 options.has_video = true;
597 options.data_channel_type = data_channel_type_;
601 } else if (state == cricket::Session::STATE_SENTINITIATE) {
602 if (call->has_video() && render_) {
604 cricket::VideoRendererFactory::CreateGuiVideoRenderer(160, 100);
606 console_->PrintLine("calling...");
607 } else if (state == cricket::Session::STATE_RECEIVEDACCEPT) {
608 console_->PrintLine("call answered");
610 } else if (state == cricket::Session::STATE_RECEIVEDREJECT) {
611 console_->PrintLine("call not answered");
612 } else if (state == cricket::Session::STATE_INPROGRESS) {
613 console_->PrintLine("call in progress");
614 call->SignalSpeakerMonitor.connect(this, &CallClient::OnSpeakerChanged);
615 call->StartSpeakerMonitor(session);
616 } else if (state == cricket::Session::STATE_RECEIVEDTERMINATE) {
617 console_->PrintLine("other side terminated");
618 TerminateAndRemoveSession(call, session->id());
622 void CallClient::OnSpeakerChanged(cricket::Call* call,
623 cricket::Session* session,
624 const cricket::StreamParams& speaker) {
625 if (!speaker.has_ssrcs()) {
626 console_->PrintLine("Session %s has no current speaker.",
627 session->id().c_str());
628 } else if (speaker.id.empty()) {
629 console_->PrintLine("Session %s speaker change to unknown (%u).",
630 session->id().c_str(), speaker.first_ssrc());
632 console_->PrintLine("Session %s speaker changed to %s (%u).",
633 session->id().c_str(), speaker.id.c_str(),
634 speaker.first_ssrc());
638 void SetMediaCaps(int media_caps, buzz::PresenceStatus* status) {
639 status->set_voice_capability((media_caps & cricket::AUDIO_RECV) != 0);
640 status->set_video_capability((media_caps & cricket::VIDEO_RECV) != 0);
641 status->set_camera_capability((media_caps & cricket::VIDEO_SEND) != 0);
644 void SetCaps(int media_caps, buzz::PresenceStatus* status) {
645 status->set_know_capabilities(true);
646 status->set_pmuc_capability(true);
647 SetMediaCaps(media_caps, status);
650 void SetAvailable(const buzz::Jid& jid, buzz::PresenceStatus* status) {
651 status->set_jid(jid);
652 status->set_available(true);
653 status->set_show(buzz::PresenceStatus::SHOW_ONLINE);
656 void CallClient::InitPresence() {
657 presence_push_ = new buzz::PresencePushTask(xmpp_client_, this);
658 presence_push_->SignalStatusUpdate.connect(
659 this, &CallClient::OnStatusUpdate);
660 presence_push_->SignalMucJoined.connect(this, &CallClient::OnMucJoined);
661 presence_push_->SignalMucLeft.connect(this, &CallClient::OnMucLeft);
662 presence_push_->SignalMucStatusUpdate.connect(
663 this, &CallClient::OnMucStatusUpdate);
664 presence_push_->Start();
666 presence_out_ = new buzz::PresenceOutTask(xmpp_client_);
667 SetAvailable(xmpp_client_->jid(), &my_status_);
668 SetCaps(media_client_->GetCapabilities(), &my_status_);
669 SendStatus(my_status_);
670 presence_out_->Start();
672 muc_invite_recv_ = new buzz::MucInviteRecvTask(xmpp_client_);
673 muc_invite_recv_->SignalInviteReceived.connect(this,
674 &CallClient::OnMucInviteReceived);
675 muc_invite_recv_->Start();
677 muc_invite_send_ = new buzz::MucInviteSendTask(xmpp_client_);
678 muc_invite_send_->Start();
680 friend_invite_send_ = new buzz::FriendInviteSendTask(xmpp_client_);
681 friend_invite_send_->Start();
686 void CallClient::StartXmppPing() {
687 buzz::PingTask* ping = new buzz::PingTask(
688 xmpp_client_, rtc::Thread::Current(),
689 kPingPeriodMillis, kPingTimeoutMillis);
690 ping->SignalTimeout.connect(this, &CallClient::OnPingTimeout);
694 void CallClient::OnPingTimeout() {
695 LOG(LS_WARNING) << "XMPP Ping timeout. Will keep trying...";
698 // Or should we do this instead?
702 void CallClient::SendStatus(const buzz::PresenceStatus& status) {
703 presence_out_->Send(status);
706 void CallClient::OnStatusUpdate(const buzz::PresenceStatus& status) {
708 item.jid = status.jid();
709 item.show = status.show();
710 item.status = status.status();
712 std::string key = item.jid.Str();
714 if (status.available() && status.voice_capability()) {
715 if (show_roster_messages_) {
716 console_->PrintLine("Adding to roster: %s", key.c_str());
718 (*roster_)[key] = item;
719 // TODO: Make some of these constants.
721 if (show_roster_messages_) {
722 console_->PrintLine("Removing from roster: %s", key.c_str());
724 RosterMap::iterator iter = roster_->find(key);
725 if (iter != roster_->end())
726 roster_->erase(iter);
730 void CallClient::PrintRoster() {
731 console_->PrintLine("Roster contains %d callable", roster_->size());
732 RosterMap::iterator iter = roster_->begin();
733 while (iter != roster_->end()) {
734 console_->PrintLine("%s - %s",
735 iter->second.jid.BareJid().Str().c_str(),
736 DescribeStatus(iter->second.show, iter->second.status));
741 void CallClient::SendChat(const std::string& to, const std::string msg) {
742 buzz::XmlElement* stanza = new buzz::XmlElement(buzz::QN_MESSAGE);
743 stanza->AddAttr(buzz::QN_TO, to);
744 stanza->AddAttr(buzz::QN_ID, rtc::CreateRandomString(16));
745 stanza->AddAttr(buzz::QN_TYPE, "chat");
746 buzz::XmlElement* body = new buzz::XmlElement(buzz::QN_BODY);
747 body->SetBodyText(msg);
748 stanza->AddElement(body);
750 xmpp_client_->SendStanza(stanza);
754 void CallClient::SendData(const std::string& streamid,
755 const std::string& text) {
756 // TODO(mylesj): Support sending data over sessions other than the first.
757 cricket::Session* session = GetFirstSession();
758 if (!call_ || !session) {
759 console_->PrintLine("Must be in a call to send data.");
762 if (!call_->has_data()) {
763 console_->PrintLine("This call doesn't have a data channel.");
767 const cricket::DataContentDescription* data =
768 cricket::GetFirstDataContentDescription(session->local_description());
770 console_->PrintLine("This call doesn't have a data content.");
774 cricket::StreamParams stream;
775 if (!cricket::GetStreamByIds(
776 data->streams(), "", streamid, &stream)) {
777 LOG(LS_WARNING) << "Could not send data: no such stream: "
782 cricket::SendDataParams params;
783 params.ssrc = stream.first_ssrc();
784 rtc::Buffer payload(text.data(), text.length());
785 cricket::SendDataResult result;
786 bool sent = call_->SendData(session, params, payload, &result);
788 if (result == cricket::SDR_BLOCK) {
789 LOG(LS_WARNING) << "Could not send data because it would block.";
791 LOG(LS_WARNING) << "Could not send data for unknown reason.";
796 void CallClient::InviteFriend(const std::string& name) {
798 if (!jid.IsValid() || jid.node() == "") {
799 console_->PrintLine("Invalid JID. JIDs should be in the form user@domain.");
802 // Note: for some reason the Buzz backend does not forward our presence
803 // subscription requests to the end user when that user is another call
804 // client as opposed to a Smurf user. Thus, in that scenario, you must
805 // run the friend command as the other user too to create the linkage
806 // (and you won't be notified to do so).
807 friend_invite_send_->Send(jid);
808 console_->PrintLine("Requesting to befriend %s.", name.c_str());
811 bool CallClient::FindJid(const std::string& name, buzz::Jid* found_jid,
812 cricket::CallOptions* options) {
814 options->is_muc = false;
815 buzz::Jid callto_jid(name);
816 if (name.length() == 0 && mucs_.size() > 0) {
817 // if no name, and in a MUC, establish audio with the MUC
818 *found_jid = mucs_.begin()->first;
820 options->is_muc = true;
821 } else if (name[0] == '+') {
822 // if the first character is a +, assume it's a phone number
823 *found_jid = callto_jid;
826 // otherwise, it's a friend
827 for (RosterMap::iterator iter = roster_->begin();
828 iter != roster_->end(); ++iter) {
829 if (iter->second.jid.BareEquals(callto_jid)) {
831 *found_jid = iter->second.jid;
837 if (mucs_.count(callto_jid) == 1 &&
838 mucs_[callto_jid]->state() == buzz::Muc::MUC_JOINED) {
840 *found_jid = callto_jid;
841 options->is_muc = true;
847 console_->PrintLine("Found %s '%s'",
848 options->is_muc ? "room" : "online friend",
849 found_jid->Str().c_str());
851 console_->PrintLine("Could not find online friend '%s'", name.c_str());
857 void CallClient::OnDataReceived(cricket::Call*,
858 const cricket::ReceiveDataParams& params,
859 const rtc::Buffer& payload) {
860 // TODO(mylesj): Support receiving data on sessions other than the first.
861 cricket::Session* session = GetFirstSession();
865 cricket::StreamParams stream;
866 const std::vector<cricket::StreamParams>* data_streams =
867 call_->GetDataRecvStreams(session);
868 std::string text(payload.data(), payload.length());
869 if (data_streams && GetStreamBySsrc(*data_streams, params.ssrc, &stream)) {
871 "Received data from '%s' on stream '%s' (ssrc=%u): %s",
872 stream.groupid.c_str(), stream.id.c_str(),
873 params.ssrc, text.c_str());
876 "Received data (ssrc=%u): %s",
877 params.ssrc, text.c_str());
881 bool CallClient::PlaceCall(const std::string& name,
882 cricket::CallOptions options) {
884 if (!FindJid(name, &jid, &options))
888 call_ = media_client_->CreateCall();
889 AddSession(call_->InitiateSession(jid, media_client_->jid(), options));
891 media_client_->SetFocus(call_);
892 if (call_->has_video() && render_ && !options.is_muc) {
893 // TODO(pthatcher): Hookup local_render_ to the local capturer.
895 if (options.is_muc) {
896 const std::string& nick = mucs_[jid]->local_jid().resource();
897 hangout_pubsub_client_ =
898 new buzz::HangoutPubSubClient(xmpp_client_, jid, nick);
899 hangout_pubsub_client_->SignalPresenterStateChange.connect(
900 this, &CallClient::OnPresenterStateChange);
901 hangout_pubsub_client_->SignalAudioMuteStateChange.connect(
902 this, &CallClient::OnAudioMuteStateChange);
903 hangout_pubsub_client_->SignalRecordingStateChange.connect(
904 this, &CallClient::OnRecordingStateChange);
905 hangout_pubsub_client_->SignalRemoteMute.connect(
906 this, &CallClient::OnRemoteMuted);
907 hangout_pubsub_client_->SignalMediaBlock.connect(
908 this, &CallClient::OnMediaBlocked);
909 hangout_pubsub_client_->SignalRequestError.connect(
910 this, &CallClient::OnHangoutRequestError);
911 hangout_pubsub_client_->SignalPublishAudioMuteError.connect(
912 this, &CallClient::OnHangoutPublishAudioMuteError);
913 hangout_pubsub_client_->SignalPublishPresenterError.connect(
914 this, &CallClient::OnHangoutPublishPresenterError);
915 hangout_pubsub_client_->SignalPublishRecordingError.connect(
916 this, &CallClient::OnHangoutPublishRecordingError);
917 hangout_pubsub_client_->SignalRemoteMuteError.connect(
918 this, &CallClient::OnHangoutRemoteMuteError);
919 hangout_pubsub_client_->RequestAll();
925 bool CallClient::InitiateAdditionalSession(const std::string& name,
926 cricket::CallOptions options) {
927 // Can't add a session if there is no call yet.
932 if (!FindJid(name, &jid, &options))
935 std::vector<cricket::Session*>& call_sessions = sessions_[call_->id()];
936 call_sessions.push_back(
937 call_->InitiateSession(jid,
938 buzz::Jid(call_sessions[0]->remote_name()),
944 void CallClient::TerminateAndRemoveSession(cricket::Call* call,
945 const std::string& id) {
946 std::vector<cricket::Session*>& call_sessions = sessions_[call->id()];
947 for (std::vector<cricket::Session*>::iterator iter = call_sessions.begin();
948 iter != call_sessions.end(); ++iter) {
949 if ((*iter)->id() == id) {
950 RenderAllStreams(call, *iter, false);
951 call_->TerminateSession(*iter);
952 call_sessions.erase(iter);
958 void CallClient::PrintCalls() {
959 const std::map<uint32, cricket::Call*>& calls = media_client_->calls();
960 for (std::map<uint32, cricket::Call*>::const_iterator i = calls.begin();
961 i != calls.end(); ++i) {
962 console_->PrintLine("Call (id:%d), is %s",
964 i->second == call_ ? "active" : "on hold");
965 std::vector<cricket::Session *>& sessions = sessions_[call_->id()];
966 for (std::vector<cricket::Session *>::const_iterator j = sessions.begin();
967 j != sessions.end(); ++j) {
968 console_->PrintLine("|--Session (id:%s), to %s", (*j)->id().c_str(),
969 (*j)->remote_name().c_str());
971 std::vector<cricket::StreamParams>::const_iterator k;
972 const std::vector<cricket::StreamParams>* streams =
973 i->second->GetAudioRecvStreams(*j);
975 for (k = streams->begin(); k != streams->end(); ++k) {
976 console_->PrintLine("|----Audio Stream: %s", k->ToString().c_str());
978 streams = i->second->GetVideoRecvStreams(*j);
980 for (k = streams->begin(); k != streams->end(); ++k) {
981 console_->PrintLine("|----Video Stream: %s", k->ToString().c_str());
983 streams = i->second->GetDataRecvStreams(*j);
985 for (k = streams->begin(); k != streams->end(); ++k) {
986 console_->PrintLine("|----Data Stream: %s", k->ToString().c_str());
992 void CallClient::SwitchToCall(uint32 call_id) {
993 const std::map<uint32, cricket::Call*>& calls = media_client_->calls();
994 std::map<uint32, cricket::Call*>::const_iterator call_iter =
996 if (call_iter != calls.end()) {
997 media_client_->SetFocus(call_iter->second);
998 call_ = call_iter->second;
1000 console_->PrintLine("Unable to find call: %d", call_id);
1004 void CallClient::OnPresenterStateChange(
1005 const std::string& nick, bool was_presenting, bool is_presenting) {
1006 if (!was_presenting && is_presenting) {
1007 console_->PrintLine("%s now presenting.", nick.c_str());
1008 } else if (was_presenting && !is_presenting) {
1009 console_->PrintLine("%s no longer presenting.", nick.c_str());
1010 } else if (was_presenting && is_presenting) {
1011 console_->PrintLine("%s still presenting.", nick.c_str());
1012 } else if (!was_presenting && !is_presenting) {
1013 console_->PrintLine("%s still not presenting.", nick.c_str());
1017 void CallClient::OnAudioMuteStateChange(
1018 const std::string& nick, bool was_muted, bool is_muted) {
1019 if (!was_muted && is_muted) {
1020 console_->PrintLine("%s now muted.", nick.c_str());
1021 } else if (was_muted && !is_muted) {
1022 console_->PrintLine("%s no longer muted.", nick.c_str());
1026 void CallClient::OnRecordingStateChange(
1027 const std::string& nick, bool was_recording, bool is_recording) {
1028 if (!was_recording && is_recording) {
1029 console_->PrintLine("%s now recording.", nick.c_str());
1030 } else if (was_recording && !is_recording) {
1031 console_->PrintLine("%s no longer recording.", nick.c_str());
1035 void CallClient::OnRemoteMuted(const std::string& mutee_nick,
1036 const std::string& muter_nick,
1037 bool should_mute_locally) {
1038 if (should_mute_locally) {
1040 console_->PrintLine("Remote muted by %s.", muter_nick.c_str());
1042 console_->PrintLine("%s remote muted by %s.",
1043 mutee_nick.c_str(), muter_nick.c_str());
1047 void CallClient::OnMediaBlocked(const std::string& blockee_nick,
1048 const std::string& blocker_nick) {
1049 console_->PrintLine("%s blocked by %s.",
1050 blockee_nick.c_str(), blocker_nick.c_str());
1053 void CallClient::OnHangoutRequestError(const std::string& node,
1054 const buzz::XmlElement* stanza) {
1055 console_->PrintLine("Failed request pub sub items for node %s.",
1059 void CallClient::OnHangoutPublishAudioMuteError(
1060 const std::string& task_id, const buzz::XmlElement* stanza) {
1061 console_->PrintLine("Failed to publish audio mute state.");
1064 void CallClient::OnHangoutPublishPresenterError(
1065 const std::string& task_id, const buzz::XmlElement* stanza) {
1066 console_->PrintLine("Failed to publish presenting state.");
1069 void CallClient::OnHangoutPublishRecordingError(
1070 const std::string& task_id, const buzz::XmlElement* stanza) {
1071 console_->PrintLine("Failed to publish recording state.");
1074 void CallClient::OnHangoutRemoteMuteError(const std::string& task_id,
1075 const std::string& mutee_nick,
1076 const buzz::XmlElement* stanza) {
1077 console_->PrintLine("Failed to remote mute.");
1080 void CallClient::Accept(const cricket::CallOptions& options) {
1081 ASSERT(call_ && incoming_call_);
1082 ASSERT(sessions_[call_->id()].size() == 1);
1083 cricket::Session* session = GetFirstSession();
1084 call_->AcceptSession(session, options);
1085 media_client_->SetFocus(call_);
1086 if (call_->has_video() && render_) {
1087 // TODO(pthatcher): Hookup local_render_ to the local capturer.
1088 RenderAllStreams(call_, session, true);
1090 SetupAcceptedCall();
1091 incoming_call_ = false;
1094 void CallClient::SetupAcceptedCall() {
1095 if (call_->has_data()) {
1096 call_->SignalDataReceived.connect(this, &CallClient::OnDataReceived);
1100 void CallClient::Reject() {
1101 ASSERT(call_ && incoming_call_);
1102 call_->RejectSession(call_->sessions()[0]);
1103 incoming_call_ = false;
1106 void CallClient::Quit() {
1107 rtc::Thread::Current()->Quit();
1110 void CallClient::SetNick(const std::string& muc_nick) {
1111 my_status_.set_nick(muc_nick);
1113 // TODO: We might want to re-send presence, but right
1114 // now, it appears to be ignored by the MUC.
1116 // presence_out_->Send(my_status_); for (MucMap::const_iterator itr
1117 // = mucs_.begin(); itr != mucs_.end(); ++itr) {
1118 // presence_out_->SendDirected(itr->second->local_jid(),
1121 console_->PrintLine("Nick set to '%s'.", muc_nick.c_str());
1124 void CallClient::LookupAndJoinMuc(const std::string& room_name) {
1125 // The room_name can't be empty for lookup task.
1126 if (room_name.empty()) {
1127 console_->PrintLine("Please provide a room name or room jid.");
1131 std::string room = room_name;
1132 std::string domain = xmpp_client_->jid().domain();
1133 if (room_name.find("@") != std::string::npos) {
1134 // Assume the room_name is a fully qualified room name.
1135 // We'll find the room name string and domain name string from it.
1136 room = room_name.substr(0, room_name.find("@"));
1137 domain = room_name.substr(room_name.find("@") + 1);
1140 buzz::MucRoomLookupTask* lookup_query_task =
1141 buzz::MucRoomLookupTask::CreateLookupTaskForRoomName(
1142 xmpp_client_, buzz::Jid(buzz::STR_GOOGLE_MUC_LOOKUP_JID), room,
1144 lookup_query_task->SignalResult.connect(this,
1145 &CallClient::OnRoomLookupResponse);
1146 lookup_query_task->SignalError.connect(this,
1147 &CallClient::OnRoomLookupError);
1148 lookup_query_task->Start();
1151 void CallClient::JoinMuc(const std::string& room_jid_str) {
1152 if (room_jid_str.empty()) {
1153 buzz::Jid room_jid = GenerateRandomMucJid();
1154 console_->PrintLine("Generated a random room jid: %s",
1155 room_jid.Str().c_str());
1158 JoinMuc(buzz::Jid(room_jid_str));
1162 void CallClient::JoinMuc(const buzz::Jid& room_jid) {
1163 if (!room_jid.IsValid()) {
1164 console_->PrintLine("Unable to make valid muc endpoint for %s",
1165 room_jid.Str().c_str());
1169 std::string room_nick = room_jid.resource();
1170 if (room_nick.empty()) {
1171 room_nick = (xmpp_client_->jid().node()
1172 + "_" + xmpp_client_->jid().resource());
1175 MucMap::iterator elem = mucs_.find(room_jid);
1176 if (elem != mucs_.end()) {
1177 console_->PrintLine("This MUC already exists.");
1181 buzz::Muc* muc = new buzz::Muc(room_jid.BareJid(), room_nick);
1182 mucs_[muc->jid()] = muc;
1183 presence_out_->SendDirected(muc->local_jid(), my_status_);
1186 void CallClient::OnRoomLookupResponse(buzz::MucRoomLookupTask* task,
1187 const buzz::MucRoomInfo& room) {
1188 // The server requires the room be "configured" before being used.
1189 // We only need to configure it if we create it, but rooms are
1190 // auto-created at lookup, so there's currently no way to know if we
1191 // created it. So, we configure it every time, just in case.
1192 // Luckily, it appears to be safe to configure a room that's already
1193 // configured. Our current flow is:
1194 // 1. Lookup/auto-create
1197 // TODO: In the future, once the server supports it, we
1200 // 2. Create and Configure if necessary
1202 std::vector<std::string> room_features;
1203 room_features.push_back(buzz::STR_MUC_ROOM_FEATURE_ENTERPRISE);
1204 buzz::MucRoomConfigTask* room_config_task = new buzz::MucRoomConfigTask(
1205 xmpp_client_, room.jid, room.full_name(), room_features);
1206 room_config_task->SignalResult.connect(this,
1207 &CallClient::OnRoomConfigResult);
1208 room_config_task->SignalError.connect(this,
1209 &CallClient::OnRoomConfigError);
1210 room_config_task->Start();
1213 void CallClient::OnRoomLookupError(buzz::IqTask* task,
1214 const buzz::XmlElement* stanza) {
1215 if (stanza == NULL) {
1216 console_->PrintLine("Room lookup failed.");
1218 console_->PrintLine("Room lookup error: ", stanza->Str().c_str());
1222 void CallClient::OnRoomConfigResult(buzz::MucRoomConfigTask* task) {
1223 JoinMuc(task->room_jid());
1226 void CallClient::OnRoomConfigError(buzz::IqTask* task,
1227 const buzz::XmlElement* stanza) {
1228 console_->PrintLine("Room config failed.");
1229 // We join the muc anyway, because if the room is already
1230 // configured, the configure will fail, but we still want to join.
1231 // Idealy, we'd know why the room config failed and only do this on
1232 // "already configured" errors. But right now all we get back is
1234 buzz::MucRoomConfigTask* config_task =
1235 static_cast<buzz::MucRoomConfigTask*>(task);
1236 JoinMuc(config_task->room_jid());
1239 void CallClient::OnMucInviteReceived(const buzz::Jid& inviter,
1240 const buzz::Jid& room,
1241 const std::vector<buzz::AvailableMediaEntry>& avail) {
1243 console_->PrintLine("Invited to join %s by %s.", room.Str().c_str(),
1244 inviter.Str().c_str());
1245 console_->PrintLine("Available media:");
1246 if (avail.size() > 0) {
1247 for (std::vector<buzz::AvailableMediaEntry>::const_iterator i =
1251 console_->PrintLine(" %s, %s",
1252 buzz::AvailableMediaEntry::TypeAsString(i->type),
1253 buzz::AvailableMediaEntry::StatusAsString(i->status));
1256 console_->PrintLine(" None");
1258 // We automatically join the room.
1262 void CallClient::OnMucJoined(const buzz::Jid& endpoint) {
1263 MucMap::iterator elem = mucs_.find(endpoint);
1264 ASSERT(elem != mucs_.end() &&
1265 elem->second->state() == buzz::Muc::MUC_JOINING);
1267 buzz::Muc* muc = elem->second;
1268 muc->set_state(buzz::Muc::MUC_JOINED);
1269 console_->PrintLine("Joined \"%s\"", muc->jid().Str().c_str());
1272 void CallClient::OnMucStatusUpdate(const buzz::Jid& jid,
1273 const buzz::MucPresenceStatus& status) {
1275 // Look up this muc.
1276 MucMap::iterator elem = mucs_.find(jid);
1277 ASSERT(elem != mucs_.end());
1279 buzz::Muc* muc = elem->second;
1281 if (status.jid().IsBare() || status.jid() == muc->local_jid()) {
1282 // We are only interested in status about other users.
1286 if (status.available()) {
1287 muc->members()[status.jid().resource()] = status;
1289 muc->members().erase(status.jid().resource());
1293 bool CallClient::InMuc() {
1294 const buzz::Jid* muc_jid = FirstMucJid();
1295 if (!muc_jid) return false;
1296 return muc_jid->IsValid();
1299 const buzz::Jid* CallClient::FirstMucJid() {
1300 if (mucs_.empty()) return NULL;
1301 return &(mucs_.begin()->first);
1304 void CallClient::LeaveMuc(const std::string& room) {
1306 const buzz::Jid* muc_jid = FirstMucJid();
1307 if (room.length() > 0) {
1308 room_jid = buzz::Jid(room);
1309 } else if (mucs_.size() > 0) {
1310 // leave the first MUC if no JID specified
1312 room_jid = *(muc_jid);
1316 if (!room_jid.IsValid()) {
1317 console_->PrintLine("Invalid MUC JID.");
1321 MucMap::iterator elem = mucs_.find(room_jid);
1322 if (elem == mucs_.end()) {
1323 console_->PrintLine("No such MUC.");
1327 buzz::Muc* muc = elem->second;
1328 muc->set_state(buzz::Muc::MUC_LEAVING);
1330 buzz::PresenceStatus status;
1331 status.set_jid(my_status_.jid());
1332 status.set_available(false);
1333 status.set_priority(0);
1334 presence_out_->SendDirected(muc->local_jid(), status);
1337 void CallClient::OnMucLeft(const buzz::Jid& endpoint, int error) {
1338 // We could be kicked from a room from any state. We would hope this
1339 // happens While in the MUC_LEAVING state
1340 MucMap::iterator elem = mucs_.find(endpoint);
1341 if (elem == mucs_.end())
1344 buzz::Muc* muc = elem->second;
1345 if (muc->state() == buzz::Muc::MUC_JOINING) {
1346 console_->PrintLine("Failed to join \"%s\", code=%d",
1347 muc->jid().Str().c_str(), error);
1348 } else if (muc->state() == buzz::Muc::MUC_JOINED) {
1349 console_->PrintLine("Kicked from \"%s\"",
1350 muc->jid().Str().c_str());
1357 void CallClient::InviteToMuc(const std::string& given_user,
1358 const std::string& room) {
1359 std::string user = given_user;
1361 // First find the room.
1362 const buzz::Muc* found_muc;
1363 if (room.length() == 0) {
1364 if (mucs_.size() == 0) {
1365 console_->PrintLine("Not in a room yet; can't invite.");
1368 // Invite to the first muc
1369 found_muc = mucs_.begin()->second;
1371 MucMap::iterator elem = mucs_.find(buzz::Jid(room));
1372 if (elem == mucs_.end()) {
1373 console_->PrintLine("Not in room %s.", room.c_str());
1376 found_muc = elem->second;
1379 buzz::Jid invite_to = found_muc->jid();
1381 // Now find the user. We invite all of their resources.
1382 bool found_user = false;
1383 buzz::Jid user_jid(user);
1384 for (RosterMap::iterator iter = roster_->begin();
1385 iter != roster_->end(); ++iter) {
1386 if (iter->second.jid.BareEquals(user_jid)) {
1387 buzz::Jid invitee = iter->second.jid;
1388 muc_invite_send_->Send(invite_to, invitee);
1393 buzz::Jid invitee = user_jid;
1394 muc_invite_send_->Send(invite_to, invitee);
1398 void CallClient::GetDevices() {
1399 std::vector<std::string> names;
1400 media_client_->GetAudioInputDevices(&names);
1401 console_->PrintLine("Audio input devices:");
1402 PrintDevices(names);
1403 media_client_->GetAudioOutputDevices(&names);
1404 console_->PrintLine("Audio output devices:");
1405 PrintDevices(names);
1406 media_client_->GetVideoCaptureDevices(&names);
1407 console_->PrintLine("Video capture devices:");
1408 PrintDevices(names);
1411 void CallClient::PrintDevices(const std::vector<std::string>& names) {
1412 for (size_t i = 0; i < names.size(); ++i) {
1413 console_->PrintLine("%d: %s", static_cast<int>(i), names[i].c_str());
1417 void CallClient::OnDevicesChange() {
1418 console_->PrintLine("Devices changed.");
1419 SetMediaCaps(media_client_->GetCapabilities(), &my_status_);
1420 SendStatus(my_status_);
1423 void CallClient::SetVolume(const std::string& level) {
1424 media_client_->SetOutputVolume(strtol(level.c_str(), NULL, 10));
1427 void CallClient::OnMediaStreamsUpdate(cricket::Call* call,
1428 cricket::Session* session,
1429 const cricket::MediaStreams& added,
1430 const cricket::MediaStreams& removed) {
1431 if (call && call->has_video()) {
1432 for (std::vector<cricket::StreamParams>::const_iterator
1433 it = removed.video().begin(); it != removed.video().end(); ++it) {
1434 RemoveStaticRenderedView(it->first_ssrc());
1438 RenderStreams(call, session, added.video(), true);
1440 SendViewRequest(call, session);
1444 void CallClient::RenderAllStreams(cricket::Call* call,
1445 cricket::Session* session,
1447 const std::vector<cricket::StreamParams>* video_streams =
1448 call->GetVideoRecvStreams(session);
1449 if (video_streams) {
1450 RenderStreams(call, session, *video_streams, enable);
1454 void CallClient::RenderStreams(
1455 cricket::Call* call,
1456 cricket::Session* session,
1457 const std::vector<cricket::StreamParams>& video_streams,
1459 std::vector<cricket::StreamParams>::const_iterator stream;
1460 for (stream = video_streams.begin(); stream != video_streams.end();
1462 RenderStream(call, session, *stream, enable);
1466 void CallClient::RenderStream(cricket::Call* call,
1467 cricket::Session* session,
1468 const cricket::StreamParams& stream,
1470 if (!stream.has_ssrcs()) {
1471 // Nothing to see here; move along.
1475 uint32 ssrc = stream.first_ssrc();
1476 StaticRenderedViews::iterator iter =
1477 static_rendered_views_.find(std::make_pair(session, ssrc));
1479 if (iter == static_rendered_views_.end()) {
1480 // TODO(pthatcher): Make dimensions and positions more configurable.
1481 int offset = (50 * static_views_accumulated_count_) % 300;
1482 AddStaticRenderedView(session, ssrc, 640, 400, 30,
1484 // Should have it now.
1485 iter = static_rendered_views_.find(std::make_pair(session, ssrc));
1487 call->SetVideoRenderer(session, ssrc, iter->second.renderer);
1489 if (iter != static_rendered_views_.end()) {
1490 call->SetVideoRenderer(session, ssrc, NULL);
1491 RemoveStaticRenderedView(ssrc);
1496 // TODO: Would these methods to add and remove views make
1497 // more sense in call.cc? Would other clients use them?
1498 void CallClient::AddStaticRenderedView(
1499 cricket::Session* session,
1500 uint32 ssrc, int width, int height, int framerate,
1501 int x_offset, int y_offset) {
1502 StaticRenderedView rendered_view(
1503 cricket::StaticVideoView(
1504 cricket::StreamSelector(ssrc), width, height, framerate),
1505 cricket::VideoRendererFactory::CreateGuiVideoRenderer(
1506 x_offset, y_offset));
1507 rendered_view.renderer->SetSize(width, height, 0);
1508 static_rendered_views_.insert(std::make_pair(std::make_pair(session, ssrc),
1510 ++static_views_accumulated_count_;
1511 console_->PrintLine("Added renderer for ssrc %d", ssrc);
1514 bool CallClient::RemoveStaticRenderedView(uint32 ssrc) {
1515 for (StaticRenderedViews::iterator it = static_rendered_views_.begin();
1516 it != static_rendered_views_.end(); ++it) {
1517 if (it->second.view.selector.ssrc == ssrc) {
1518 delete it->second.renderer;
1519 static_rendered_views_.erase(it);
1520 console_->PrintLine("Removed renderer for ssrc %d", ssrc);
1527 void CallClient::RemoveCallsStaticRenderedViews(cricket::Call* call) {
1528 std::vector<cricket::Session*>& sessions = sessions_[call->id()];
1529 std::set<cricket::Session*> call_sessions(sessions.begin(), sessions.end());
1530 for (StaticRenderedViews::iterator it = static_rendered_views_.begin();
1531 it != static_rendered_views_.end(); ) {
1532 if (call_sessions.find(it->first.first) != call_sessions.end()) {
1533 delete it->second.renderer;
1534 static_rendered_views_.erase(it++);
1541 void CallClient::SendViewRequest(cricket::Call* call,
1542 cricket::Session* session) {
1543 cricket::ViewRequest request;
1544 for (StaticRenderedViews::iterator it = static_rendered_views_.begin();
1545 it != static_rendered_views_.end(); ++it) {
1546 if (it->first.first == session) {
1547 request.static_video_views.push_back(it->second.view);
1550 call->SendViewRequest(session, request);
1553 buzz::Jid CallClient::GenerateRandomMucJid() {
1554 // Generate a GUID of the form XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX,
1555 // for an eventual JID of private-chat-<GUID>@groupchat.google.com.
1556 char guid[37], guid_room[256];
1557 for (size_t i = 0; i < ARRAY_SIZE(guid) - 1;) {
1558 if (i == 8 || i == 13 || i == 18 || i == 23) {
1561 sprintf(guid + i, "%04x", rand());
1566 rtc::sprintfn(guid_room,
1567 ARRAY_SIZE(guid_room),
1568 "private-chat-%s@%s",
1570 pmuc_domain_.c_str());
1571 return buzz::Jid(guid_room);
1574 bool CallClient::SelectFirstDesktopScreencastId(
1575 cricket::ScreencastId* screencastid) {
1576 if (!rtc::WindowPickerFactory::IsSupported()) {
1577 LOG(LS_WARNING) << "Window picker not suported on this OS.";
1581 rtc::WindowPicker* picker =
1582 rtc::WindowPickerFactory::CreateWindowPicker();
1584 LOG(LS_WARNING) << "Could not create a window picker.";
1588 rtc::DesktopDescriptionList desktops;
1589 if (!picker->GetDesktopList(&desktops) || desktops.empty()) {
1590 LOG(LS_WARNING) << "Could not get a list of desktops.";
1594 *screencastid = cricket::ScreencastId(desktops[0].id());
1598 void CallClient::PrintStats() const {
1599 const cricket::VoiceMediaInfo& vmi = call_->last_voice_media_info();
1601 for (std::vector<cricket::VoiceSenderInfo>::const_iterator it =
1602 vmi.senders.begin(); it != vmi.senders.end(); ++it) {
1603 console_->PrintLine("Sender: ssrc=%u codec='%s' bytes=%d packets=%d "
1605 it->ssrc(), it->codec_name.c_str(), it->bytes_sent,
1606 it->packets_sent, it->rtt_ms, it->jitter_ms);
1609 for (std::vector<cricket::VoiceReceiverInfo>::const_iterator it =
1610 vmi.receivers.begin(); it != vmi.receivers.end(); ++it) {
1611 console_->PrintLine("Receiver: ssrc=%u bytes=%d packets=%d "
1612 "jitter=%d loss=%.2f",
1613 it->ssrc(), it->bytes_rcvd, it->packets_rcvd,
1614 it->jitter_ms, it->fraction_lost);