blob: 25c33ff0e480a327748d6a4ce9bf695842f5249a [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
3 * Copyright 2004--2005, Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include <cstdio>
29#include <cstring>
30#include <time.h>
31#include <iomanip>
32#include <iostream>
33#include <vector>
34
35#include "talk/base/flags.h"
36#include "talk/base/logging.h"
37#ifdef OSX
38#include "talk/base/maccocoasocketserver.h"
39#endif
40#include "talk/base/pathutils.h"
41#include "talk/base/ssladapter.h"
42#include "talk/base/stream.h"
43#include "talk/base/win32socketserver.h"
44#include "talk/examples/call/callclient.h"
45#include "talk/examples/call/console.h"
46#include "talk/examples/call/mediaenginefactory.h"
47#include "talk/p2p/base/constants.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000048#include "talk/session/media/mediasessionclient.h"
49#include "talk/session/media/srtpfilter.h"
50#include "talk/xmpp/xmppauth.h"
51#include "talk/xmpp/xmppclientsettings.h"
52#include "talk/xmpp/xmpppump.h"
53#include "talk/xmpp/xmppsocket.h"
54
55class DebugLog : public sigslot::has_slots<> {
56 public:
57 DebugLog() :
58 debug_input_buf_(NULL), debug_input_len_(0), debug_input_alloc_(0),
59 debug_output_buf_(NULL), debug_output_len_(0), debug_output_alloc_(0),
60 censor_password_(false)
61 {}
62 char * debug_input_buf_;
63 int debug_input_len_;
64 int debug_input_alloc_;
65 char * debug_output_buf_;
66 int debug_output_len_;
67 int debug_output_alloc_;
68 bool censor_password_;
69
70 void Input(const char * data, int len) {
71 if (debug_input_len_ + len > debug_input_alloc_) {
72 char * old_buf = debug_input_buf_;
73 debug_input_alloc_ = 4096;
74 while (debug_input_alloc_ < debug_input_len_ + len) {
75 debug_input_alloc_ *= 2;
76 }
77 debug_input_buf_ = new char[debug_input_alloc_];
78 memcpy(debug_input_buf_, old_buf, debug_input_len_);
79 delete[] old_buf;
80 }
81 memcpy(debug_input_buf_ + debug_input_len_, data, len);
82 debug_input_len_ += len;
83 DebugPrint(debug_input_buf_, &debug_input_len_, false);
84 }
85
86 void Output(const char * data, int len) {
87 if (debug_output_len_ + len > debug_output_alloc_) {
88 char * old_buf = debug_output_buf_;
89 debug_output_alloc_ = 4096;
90 while (debug_output_alloc_ < debug_output_len_ + len) {
91 debug_output_alloc_ *= 2;
92 }
93 debug_output_buf_ = new char[debug_output_alloc_];
94 memcpy(debug_output_buf_, old_buf, debug_output_len_);
95 delete[] old_buf;
96 }
97 memcpy(debug_output_buf_ + debug_output_len_, data, len);
98 debug_output_len_ += len;
99 DebugPrint(debug_output_buf_, &debug_output_len_, true);
100 }
101
102 static bool IsAuthTag(const char * str, size_t len) {
103 if (str[0] == '<' && str[1] == 'a' &&
104 str[2] == 'u' &&
105 str[3] == 't' &&
106 str[4] == 'h' &&
107 str[5] <= ' ') {
108 std::string tag(str, len);
109
110 if (tag.find("mechanism") != std::string::npos)
111 return true;
112 }
113 return false;
114 }
115
116 void DebugPrint(char * buf, int * plen, bool output) {
117 int len = *plen;
118 if (len > 0) {
119 time_t tim = time(NULL);
120 struct tm * now = localtime(&tim);
121 char *time_string = asctime(now);
122 if (time_string) {
123 size_t time_len = strlen(time_string);
124 if (time_len > 0) {
125 time_string[time_len-1] = 0; // trim off terminating \n
126 }
127 }
128 LOG(INFO) << (output ? "SEND >>>>>>>>>>>>>>>>" : "RECV <<<<<<<<<<<<<<<<")
129 << " : " << time_string;
130
131 bool indent;
132 int start = 0, nest = 3;
133 for (int i = 0; i < len; i += 1) {
134 if (buf[i] == '>') {
135 if ((i > 0) && (buf[i-1] == '/')) {
136 indent = false;
137 } else if ((start + 1 < len) && (buf[start + 1] == '/')) {
138 indent = false;
139 nest -= 2;
140 } else {
141 indent = true;
142 }
143
144 // Output a tag
145 LOG(INFO) << std::setw(nest) << " "
146 << std::string(buf + start, i + 1 - start);
147
148 if (indent)
149 nest += 2;
150
151 // Note if it's a PLAIN auth tag
152 if (IsAuthTag(buf + start, i + 1 - start)) {
153 censor_password_ = true;
154 }
155
156 // incr
157 start = i + 1;
158 }
159
160 if (buf[i] == '<' && start < i) {
161 if (censor_password_) {
162 LOG(INFO) << std::setw(nest) << " " << "## TEXT REMOVED ##";
163 censor_password_ = false;
164 } else {
165 LOG(INFO) << std::setw(nest) << " "
166 << std::string(buf + start, i - start);
167 }
168 start = i;
169 }
170 }
171 len = len - start;
172 memcpy(buf, buf + start, len);
173 *plen = len;
174 }
175 }
176};
177
178static DebugLog debug_log_;
179static const int DEFAULT_PORT = 5222;
180
181#ifdef ANDROID
182static std::vector<cricket::AudioCodec> codecs;
183static const cricket::AudioCodec ISAC(103, "ISAC", 40000, 16000, 1, 0);
184
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000185cricket::MediaEngineInterface *CreateAndroidMediaEngine() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000186 cricket::FakeMediaEngine *engine = new cricket::FakeMediaEngine();
187
188 codecs.push_back(ISAC);
189 engine->SetAudioCodecs(codecs);
190 return engine;
191}
192#endif
193
194// TODO: Move this into Console.
195void Print(const char* chars) {
196 printf("%s", chars);
197 fflush(stdout);
198}
199
200bool GetSecurePolicy(const std::string& in, cricket::SecurePolicy* out) {
201 if (in == "disable") {
202 *out = cricket::SEC_DISABLED;
203 } else if (in == "enable") {
204 *out = cricket::SEC_ENABLED;
205 } else if (in == "require") {
206 *out = cricket::SEC_REQUIRED;
207 } else {
208 return false;
209 }
210 return true;
211}
212
213int main(int argc, char **argv) {
214 // This app has three threads. The main thread will run the XMPP client,
215 // which will print to the screen in its own thread. A second thread
216 // will get input from the console, parse it, and pass the appropriate
217 // message back to the XMPP client's thread. A third thread is used
218 // by MediaSessionClient as its worker thread.
219
220 // define options
221 DEFINE_string(s, "talk.google.com", "The connection server to use.");
222 DEFINE_string(tls, "require",
223 "Select connection encryption: disable, enable, require.");
224 DEFINE_bool(allowplain, false, "Allow plain authentication.");
225 DEFINE_bool(testserver, false, "Use test server.");
226 DEFINE_string(oauth, "", "OAuth2 access token.");
227 DEFINE_bool(a, false, "Turn on auto accept for incoming calls.");
228 DEFINE_string(signaling, "hybrid",
229 "Initial signaling protocol to use: jingle, gingle, or hybrid.");
230 DEFINE_string(transport, "hybrid",
231 "Initial transport protocol to use: ice, gice, or hybrid.");
232 DEFINE_string(sdes, "enable",
233 "Select SDES media encryption: disable, enable, require.");
234 DEFINE_string(dtls, "disable",
235 "Select DTLS transport encryption: disable, enable, require.");
236 DEFINE_int(portallocator, 0, "Filter out unwanted connection types.");
237 DEFINE_string(pmuc, "groupchat.google.com", "The persistant muc domain.");
238 DEFINE_string(capsnode, "http://code.google.com/p/libjingle/call",
239 "Caps node: A URI identifying the app.");
240 DEFINE_string(capsver, "0.6",
241 "Caps ver: A string identifying the version of the app.");
242 DEFINE_string(voiceinput, NULL, "RTP dump file for voice input.");
243 DEFINE_string(voiceoutput, NULL, "RTP dump file for voice output.");
244 DEFINE_string(videoinput, NULL, "RTP dump file for video input.");
245 DEFINE_string(videooutput, NULL, "RTP dump file for video output.");
246 DEFINE_bool(render, true, "Renders the video.");
247 DEFINE_string(datachannel, "",
248 "Enable a data channel, and choose the type: rtp or sctp.");
249 DEFINE_bool(d, false, "Turn on debugging.");
250 DEFINE_string(log, "", "Turn on debugging to a file.");
251 DEFINE_bool(debugsrtp, false, "Enable debugging for srtp.");
252 DEFINE_bool(help, false, "Prints this message");
253 DEFINE_bool(multisession, false,
254 "Enable support for multiple sessions in calls.");
255 DEFINE_bool(roster, false,
256 "Enable roster messages printed in console.");
257
258 // parse options
259 FlagList::SetFlagsFromCommandLine(&argc, argv, true);
260 if (FLAG_help) {
261 FlagList::Print(NULL, false);
262 return 0;
263 }
264
265 bool auto_accept = FLAG_a;
266 bool debug = FLAG_d;
267 std::string log = FLAG_log;
268 std::string signaling = FLAG_signaling;
269 std::string transport = FLAG_transport;
270 bool test_server = FLAG_testserver;
271 bool allow_plain = FLAG_allowplain;
272 std::string tls = FLAG_tls;
273 std::string oauth_token = FLAG_oauth;
274 int32 portallocator_flags = FLAG_portallocator;
275 std::string pmuc_domain = FLAG_pmuc;
276 std::string server = FLAG_s;
277 std::string sdes = FLAG_sdes;
278 std::string dtls = FLAG_dtls;
279 std::string caps_node = FLAG_capsnode;
280 std::string caps_ver = FLAG_capsver;
281 bool debugsrtp = FLAG_debugsrtp;
282 bool render = FLAG_render;
283 std::string data_channel = FLAG_datachannel;
284 bool multisession_enabled = FLAG_multisession;
285 talk_base::SSLIdentity* ssl_identity = NULL;
286 bool show_roster_messages = FLAG_roster;
287
288 // Set up debugging.
289 if (debug) {
290 talk_base::LogMessage::LogToDebug(talk_base::LS_VERBOSE);
291 }
292
293 if (!log.empty()) {
294 talk_base::StreamInterface* stream =
295 talk_base::Filesystem::OpenFile(log, "a");
296 if (stream) {
297 talk_base::LogMessage::LogToStream(stream, talk_base::LS_VERBOSE);
298 } else {
299 Print(("Cannot open debug log " + log + "\n").c_str());
300 return 1;
301 }
302 }
303
304 if (debugsrtp) {
305 cricket::EnableSrtpDebugging();
306 }
307
308 // Set up the crypto subsystem.
309 talk_base::InitializeSSL();
310
311 // Parse username and password, if present.
312 buzz::Jid jid;
313 std::string username;
314 talk_base::InsecureCryptStringImpl pass;
315 if (argc > 1) {
316 username = argv[1];
317 if (argc > 2) {
318 pass.password() = argv[2];
319 }
320 }
321
322 if (username.empty()) {
323 Print("JID: ");
324 std::cin >> username;
325 }
326 if (username.find('@') == std::string::npos) {
327 username.append("@localhost");
328 }
329 jid = buzz::Jid(username);
330 if (!jid.IsValid() || jid.node() == "") {
331 Print("Invalid JID. JIDs should be in the form user@domain\n");
332 return 1;
333 }
334 if (pass.password().empty() && !test_server && oauth_token.empty()) {
335 Console::SetEcho(false);
336 Print("Password: ");
337 std::cin >> pass.password();
338 Console::SetEcho(true);
339 Print("\n");
340 }
341
342 // Decide on the connection settings.
343 buzz::XmppClientSettings xcs;
344 xcs.set_user(jid.node());
345 xcs.set_resource("call");
346 xcs.set_host(jid.domain());
347 xcs.set_allow_plain(allow_plain);
348
349 if (tls == "disable") {
350 xcs.set_use_tls(buzz::TLS_DISABLED);
351 } else if (tls == "enable") {
352 xcs.set_use_tls(buzz::TLS_ENABLED);
353 } else if (tls == "require") {
354 xcs.set_use_tls(buzz::TLS_REQUIRED);
355 } else {
356 Print("Invalid TLS option, must be enable, disable, or require.\n");
357 return 1;
358 }
359
360 if (test_server) {
361 pass.password() = jid.node();
362 xcs.set_allow_plain(true);
363 xcs.set_use_tls(buzz::TLS_DISABLED);
364 xcs.set_test_server_domain("google.com");
365 }
366 xcs.set_pass(talk_base::CryptString(pass));
367 if (!oauth_token.empty()) {
368 xcs.set_auth_token(buzz::AUTH_MECHANISM_OAUTH2, oauth_token);
369 }
370
371 std::string host;
372 int port;
373
374 int colon = server.find(':');
375 if (colon == -1) {
376 host = server;
377 port = DEFAULT_PORT;
378 } else {
379 host = server.substr(0, colon);
380 port = atoi(server.substr(colon + 1).c_str());
381 }
382
383 xcs.set_server(talk_base::SocketAddress(host, port));
384
385 // Decide on the signaling and crypto settings.
386 cricket::SignalingProtocol signaling_protocol = cricket::PROTOCOL_HYBRID;
387 if (signaling == "jingle") {
388 signaling_protocol = cricket::PROTOCOL_JINGLE;
389 } else if (signaling == "gingle") {
390 signaling_protocol = cricket::PROTOCOL_GINGLE;
391 } else if (signaling == "hybrid") {
392 signaling_protocol = cricket::PROTOCOL_HYBRID;
393 } else {
394 Print("Invalid signaling protocol. Must be jingle, gingle, or hybrid.\n");
395 return 1;
396 }
397
398 cricket::TransportProtocol transport_protocol = cricket::ICEPROTO_HYBRID;
399 if (transport == "ice") {
400 transport_protocol = cricket::ICEPROTO_RFC5245;
401 } else if (transport == "gice") {
402 transport_protocol = cricket::ICEPROTO_GOOGLE;
403 } else if (transport == "hybrid") {
404 transport_protocol = cricket::ICEPROTO_HYBRID;
405 } else {
406 Print("Invalid transport protocol. Must be ice, gice, or hybrid.\n");
407 return 1;
408 }
409
410 cricket::DataChannelType data_channel_type = cricket::DCT_NONE;
411 if (data_channel == "rtp") {
412 data_channel_type = cricket::DCT_RTP;
413 } else if (data_channel == "sctp") {
414 data_channel_type = cricket::DCT_SCTP;
415 } else if (!data_channel.empty()) {
416 Print("Invalid data channel type. Must be rtp or sctp.\n");
417 return 1;
418 }
419
420 cricket::SecurePolicy sdes_policy, dtls_policy;
421 if (!GetSecurePolicy(sdes, &sdes_policy)) {
422 Print("Invalid SDES policy. Must be enable, disable, or require.\n");
423 return 1;
424 }
425 if (!GetSecurePolicy(dtls, &dtls_policy)) {
426 Print("Invalid DTLS policy. Must be enable, disable, or require.\n");
427 return 1;
428 }
429 if (dtls_policy != cricket::SEC_DISABLED) {
430 ssl_identity = talk_base::SSLIdentity::Generate(jid.Str());
431 if (!ssl_identity) {
432 Print("Failed to generate identity for DTLS.\n");
433 return 1;
434 }
435 }
436
437#ifdef ANDROID
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000438 MediaEngineFactory::SetCreateFunction(&CreateAndroidMediaEngine);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000439#endif
440
441#if WIN32
442 // Need to pump messages on our main thread on Windows.
443 talk_base::Win32Thread w32_thread;
444 talk_base::ThreadManager::Instance()->SetCurrentThread(&w32_thread);
445#endif
446 talk_base::Thread* main_thread = talk_base::Thread::Current();
447#ifdef OSX
448 talk_base::MacCocoaSocketServer ss;
449 talk_base::SocketServerScope ss_scope(&ss);
450#endif
451
452 buzz::XmppPump pump;
453 CallClient *client = new CallClient(pump.client(), caps_node, caps_ver);
454
455 if (FLAG_voiceinput || FLAG_voiceoutput ||
456 FLAG_videoinput || FLAG_videooutput) {
457 // If any dump file is specified, we use a FileMediaEngine.
458 cricket::MediaEngineInterface* engine =
459 MediaEngineFactory::CreateFileMediaEngine(
460 FLAG_voiceinput, FLAG_voiceoutput,
461 FLAG_videoinput, FLAG_videooutput);
462 client->SetMediaEngine(engine);
463 }
464
465 Console *console = new Console(main_thread, client);
466 client->SetConsole(console);
467 client->SetAutoAccept(auto_accept);
468 client->SetPmucDomain(pmuc_domain);
469 client->SetPortAllocatorFlags(portallocator_flags);
470 client->SetAllowLocalIps(true);
471 client->SetSignalingProtocol(signaling_protocol);
472 client->SetTransportProtocol(transport_protocol);
473 client->SetSecurePolicy(sdes_policy, dtls_policy);
474 client->SetSslIdentity(ssl_identity);
475 client->SetRender(render);
476 client->SetDataChannelType(data_channel_type);
477 client->SetMultiSessionEnabled(multisession_enabled);
478 client->SetShowRosterMessages(show_roster_messages);
479 console->Start();
480
481 if (debug) {
482 pump.client()->SignalLogInput.connect(&debug_log_, &DebugLog::Input);
483 pump.client()->SignalLogOutput.connect(&debug_log_, &DebugLog::Output);
484 }
485
486 Print(("Logging in to " + server + " as " + jid.Str() + "\n").c_str());
487 pump.DoLogin(xcs, new buzz::XmppSocket(buzz::TLS_REQUIRED), new XmppAuth());
488 main_thread->Run();
489 pump.DoDisconnect();
490
491 console->Stop();
492 delete console;
493 delete client;
494
495 return 0;
496}