You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

484 lines
16 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  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. #include <cstdio>
  28. #include <cstring>
  29. #include <time.h>
  30. #include <iomanip>
  31. #include <iostream>
  32. #include <vector>
  33. #include "talk/base/flags.h"
  34. #include "talk/base/logging.h"
  35. #ifdef OSX
  36. #include "talk/base/maccocoasocketserver.h"
  37. #endif
  38. #include "talk/base/pathutils.h"
  39. #include "talk/base/ssladapter.h"
  40. #include "talk/base/stream.h"
  41. #include "talk/base/win32socketserver.h"
  42. #include "talk/examples/call/callclient.h"
  43. #include "talk/examples/call/console.h"
  44. #include "talk/examples/call/mediaenginefactory.h"
  45. #include "talk/p2p/base/constants.h"
  46. #ifdef ANDROID
  47. #include "talk/media/other/androidmediaengine.h"
  48. #endif
  49. #include "talk/session/media/mediasessionclient.h"
  50. #include "talk/session/media/srtpfilter.h"
  51. #include "talk/xmpp/xmppauth.h"
  52. #include "talk/xmpp/xmppclientsettings.h"
  53. #include "talk/xmpp/xmpppump.h"
  54. #include "talk/xmpp/xmppsocket.h"
  55. class 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. void Input(const char * data, int len) {
  70. if (debug_input_len_ + len > debug_input_alloc_) {
  71. char * old_buf = debug_input_buf_;
  72. debug_input_alloc_ = 4096;
  73. while (debug_input_alloc_ < debug_input_len_ + len) {
  74. debug_input_alloc_ *= 2;
  75. }
  76. debug_input_buf_ = new char[debug_input_alloc_];
  77. memcpy(debug_input_buf_, old_buf, debug_input_len_);
  78. delete[] old_buf;
  79. }
  80. memcpy(debug_input_buf_ + debug_input_len_, data, len);
  81. debug_input_len_ += len;
  82. DebugPrint(debug_input_buf_, &debug_input_len_, false);
  83. }
  84. void Output(const char * data, int len) {
  85. if (debug_output_len_ + len > debug_output_alloc_) {
  86. char * old_buf = debug_output_buf_;
  87. debug_output_alloc_ = 4096;
  88. while (debug_output_alloc_ < debug_output_len_ + len) {
  89. debug_output_alloc_ *= 2;
  90. }
  91. debug_output_buf_ = new char[debug_output_alloc_];
  92. memcpy(debug_output_buf_, old_buf, debug_output_len_);
  93. delete[] old_buf;
  94. }
  95. memcpy(debug_output_buf_ + debug_output_len_, data, len);
  96. debug_output_len_ += len;
  97. DebugPrint(debug_output_buf_, &debug_output_len_, true);
  98. }
  99. static bool IsAuthTag(const char * str, size_t len) {
  100. if (str[0] == '<' && str[1] == 'a' &&
  101. str[2] == 'u' &&
  102. str[3] == 't' &&
  103. str[4] == 'h' &&
  104. str[5] <= ' ') {
  105. std::string tag(str, len);
  106. if (tag.find("mechanism") != std::string::npos)
  107. return true;
  108. }
  109. return false;
  110. }
  111. void DebugPrint(char * buf, int * plen, bool output) {
  112. int len = *plen;
  113. if (len > 0) {
  114. time_t tim = time(NULL);
  115. struct tm * now = localtime(&tim);
  116. char *time_string = asctime(now);
  117. if (time_string) {
  118. size_t time_len = strlen(time_string);
  119. if (time_len > 0) {
  120. time_string[time_len-1] = 0; // trim off terminating \n
  121. }
  122. }
  123. LOG(INFO) << (output ? "SEND >>>>>>>>>>>>>>>>" : "RECV <<<<<<<<<<<<<<<<")
  124. << " : " << time_string;
  125. bool indent;
  126. int start = 0, nest = 3;
  127. for (int i = 0; i < len; i += 1) {
  128. if (buf[i] == '>') {
  129. if ((i > 0) && (buf[i-1] == '/')) {
  130. indent = false;
  131. } else if ((start + 1 < len) && (buf[start + 1] == '/')) {
  132. indent = false;
  133. nest -= 2;
  134. } else {
  135. indent = true;
  136. }
  137. // Output a tag
  138. LOG(INFO) << std::setw(nest) << " "
  139. << std::string(buf + start, i + 1 - start);
  140. if (indent)
  141. nest += 2;
  142. // Note if it's a PLAIN auth tag
  143. if (IsAuthTag(buf + start, i + 1 - start)) {
  144. censor_password_ = true;
  145. }
  146. // incr
  147. start = i + 1;
  148. }
  149. if (buf[i] == '<' && start < i) {
  150. if (censor_password_) {
  151. LOG(INFO) << std::setw(nest) << " " << "## TEXT REMOVED ##";
  152. censor_password_ = false;
  153. } else {
  154. LOG(INFO) << std::setw(nest) << " "
  155. << std::string(buf + start, i - start);
  156. }
  157. start = i;
  158. }
  159. }
  160. len = len - start;
  161. memcpy(buf, buf + start, len);
  162. *plen = len;
  163. }
  164. }
  165. };
  166. static DebugLog debug_log_;
  167. static const int DEFAULT_PORT = 5222;
  168. #ifdef ANDROID
  169. static std::vector<cricket::AudioCodec> codecs;
  170. static const cricket::AudioCodec ISAC(103, "ISAC", 40000, 16000, 1, 0);
  171. cricket::MediaEngine *AndroidMediaEngineFactory() {
  172. cricket::FakeMediaEngine *engine = new cricket::FakeMediaEngine();
  173. codecs.push_back(ISAC);
  174. engine->SetAudioCodecs(codecs);
  175. return engine;
  176. }
  177. #endif
  178. // TODO: Move this into Console.
  179. void Print(const char* chars) {
  180. printf("%s", chars);
  181. fflush(stdout);
  182. }
  183. bool GetSecurePolicy(const std::string& in, cricket::SecurePolicy* out) {
  184. if (in == "disable") {
  185. *out = cricket::SEC_DISABLED;
  186. } else if (in == "enable") {
  187. *out = cricket::SEC_ENABLED;
  188. } else if (in == "require") {
  189. *out = cricket::SEC_REQUIRED;
  190. } else {
  191. return false;
  192. }
  193. return true;
  194. }
  195. int main(int argc, char **argv) {
  196. // This app has three threads. The main thread will run the XMPP client,
  197. // which will print to the screen in its own thread. A second thread
  198. // will get input from the console, parse it, and pass the appropriate
  199. // message back to the XMPP client's thread. A third thread is used
  200. // by MediaSessionClient as its worker thread.
  201. // define options
  202. DEFINE_string(s, "talk.google.com", "The connection server to use.");
  203. DEFINE_string(tls, "require",
  204. "Select connection encryption: disable, enable, require.");
  205. DEFINE_bool(allowplain, false, "Allow plain authentication.");
  206. DEFINE_bool(testserver, false, "Use test server.");
  207. DEFINE_string(oauth, "", "OAuth2 access token.");
  208. DEFINE_bool(a, false, "Turn on auto accept for incoming calls.");
  209. DEFINE_string(signaling, "hybrid",
  210. "Initial signaling protocol to use: jingle, gingle, or hybrid.");
  211. DEFINE_string(transport, "hybrid",
  212. "Initial transport protocol to use: ice, gice, or hybrid.");
  213. DEFINE_string(sdes, "enable",
  214. "Select SDES media encryption: disable, enable, require.");
  215. DEFINE_string(dtls, "disable",
  216. "Select DTLS transport encryption: disable, enable, require.");
  217. DEFINE_int(portallocator, 0, "Filter out unwanted connection types.");
  218. DEFINE_string(pmuc, "groupchat.google.com", "The persistant muc domain.");
  219. DEFINE_string(capsnode, "http://code.google.com/p/libjingle/call",
  220. "Caps node: A URI identifying the app.");
  221. DEFINE_string(capsver, "0.6",
  222. "Caps ver: A string identifying the version of the app.");
  223. DEFINE_string(voiceinput, NULL, "RTP dump file for voice input.");
  224. DEFINE_string(voiceoutput, NULL, "RTP dump file for voice output.");
  225. DEFINE_string(videoinput, NULL, "RTP dump file for video input.");
  226. DEFINE_string(videooutput, NULL, "RTP dump file for video output.");
  227. DEFINE_bool(render, true, "Renders the video.");
  228. DEFINE_bool(datachannel, false, "Enable an RTP data channel.");
  229. DEFINE_bool(d, false, "Turn on debugging.");
  230. DEFINE_string(log, "", "Turn on debugging to a file.");
  231. DEFINE_bool(debugsrtp, false, "Enable debugging for srtp.");
  232. DEFINE_bool(help, false, "Prints this message");
  233. DEFINE_bool(multisession, false,
  234. "Enable support for multiple sessions in calls.");
  235. // parse options
  236. FlagList::SetFlagsFromCommandLine(&argc, argv, true);
  237. if (FLAG_help) {
  238. FlagList::Print(NULL, false);
  239. return 0;
  240. }
  241. bool auto_accept = FLAG_a;
  242. bool debug = FLAG_d;
  243. std::string log = FLAG_log;
  244. std::string signaling = FLAG_signaling;
  245. std::string transport = FLAG_transport;
  246. bool test_server = FLAG_testserver;
  247. bool allow_plain = FLAG_allowplain;
  248. std::string tls = FLAG_tls;
  249. std::string oauth_token = FLAG_oauth;
  250. int32 portallocator_flags = FLAG_portallocator;
  251. std::string pmuc_domain = FLAG_pmuc;
  252. std::string server = FLAG_s;
  253. std::string sdes = FLAG_sdes;
  254. std::string dtls = FLAG_dtls;
  255. std::string caps_node = FLAG_capsnode;
  256. std::string caps_ver = FLAG_capsver;
  257. bool debugsrtp = FLAG_debugsrtp;
  258. bool render = FLAG_render;
  259. bool data_channel_enabled = FLAG_datachannel;
  260. bool multisession_enabled = FLAG_multisession;
  261. talk_base::SSLIdentity* ssl_identity = NULL;
  262. // Set up debugging.
  263. if (debug) {
  264. talk_base::LogMessage::LogToDebug(talk_base::LS_VERBOSE);
  265. }
  266. if (!log.empty()) {
  267. talk_base::StreamInterface* stream =
  268. talk_base::Filesystem::OpenFile(log, "a");
  269. if (stream) {
  270. talk_base::LogMessage::LogToStream(stream, talk_base::LS_VERBOSE);
  271. } else {
  272. Print(("Cannot open debug log " + log + "\n").c_str());
  273. return 1;
  274. }
  275. }
  276. if (debugsrtp) {
  277. cricket::EnableSrtpDebugging();
  278. }
  279. // Set up the crypto subsystem.
  280. talk_base::InitializeSSL();
  281. // Parse username and password, if present.
  282. buzz::Jid jid;
  283. std::string username;
  284. talk_base::InsecureCryptStringImpl pass;
  285. if (argc > 1) {
  286. username = argv[1];
  287. if (argc > 2) {
  288. pass.password() = argv[2];
  289. }
  290. }
  291. if (username.empty()) {
  292. Print("JID: ");
  293. std::cin >> username;
  294. }
  295. if (username.find('@') == std::string::npos) {
  296. username.append("@localhost");
  297. }
  298. jid = buzz::Jid(username);
  299. if (!jid.IsValid() || jid.node() == "") {
  300. Print("Invalid JID. JIDs should be in the form user@domain\n");
  301. return 1;
  302. }
  303. if (pass.password().empty() && !test_server && oauth_token.empty()) {
  304. Console::SetEcho(false);
  305. Print("Password: ");
  306. std::cin >> pass.password();
  307. Console::SetEcho(true);
  308. Print("\n");
  309. }
  310. // Decide on the connection settings.
  311. buzz::XmppClientSettings xcs;
  312. xcs.set_user(jid.node());
  313. xcs.set_resource("call");
  314. xcs.set_host(jid.domain());
  315. xcs.set_allow_plain(allow_plain);
  316. if (tls == "disable") {
  317. xcs.set_use_tls(buzz::TLS_DISABLED);
  318. } else if (tls == "enable") {
  319. xcs.set_use_tls(buzz::TLS_ENABLED);
  320. } else if (tls == "require") {
  321. xcs.set_use_tls(buzz::TLS_REQUIRED);
  322. } else {
  323. Print("Invalid TLS option, must be enable, disable, or require.\n");
  324. return 1;
  325. }
  326. if (test_server) {
  327. pass.password() = jid.node();
  328. xcs.set_allow_plain(true);
  329. xcs.set_use_tls(buzz::TLS_DISABLED);
  330. xcs.set_test_server_domain("google.com");
  331. }
  332. xcs.set_pass(talk_base::CryptString(pass));
  333. if (!oauth_token.empty()) {
  334. xcs.set_auth_token(buzz::AUTH_MECHANISM_OAUTH2, oauth_token);
  335. }
  336. std::string host;
  337. int port;
  338. int colon = server.find(':');
  339. if (colon == -1) {
  340. host = server;
  341. port = DEFAULT_PORT;
  342. } else {
  343. host = server.substr(0, colon);
  344. port = atoi(server.substr(colon + 1).c_str());
  345. }
  346. xcs.set_server(talk_base::SocketAddress(host, port));
  347. // Decide on the signaling and crypto settings.
  348. cricket::SignalingProtocol signaling_protocol = cricket::PROTOCOL_HYBRID;
  349. if (signaling == "jingle") {
  350. signaling_protocol = cricket::PROTOCOL_JINGLE;
  351. } else if (signaling == "gingle") {
  352. signaling_protocol = cricket::PROTOCOL_GINGLE;
  353. } else if (signaling == "hybrid") {
  354. signaling_protocol = cricket::PROTOCOL_HYBRID;
  355. } else {
  356. Print("Invalid signaling protocol. Must be jingle, gingle, or hybrid.\n");
  357. return 1;
  358. }
  359. cricket::TransportProtocol transport_protocol = cricket::ICEPROTO_HYBRID;
  360. if (transport == "ice") {
  361. transport_protocol = cricket::ICEPROTO_RFC5245;
  362. } else if (transport == "gice") {
  363. transport_protocol = cricket::ICEPROTO_GOOGLE;
  364. } else if (transport == "hybrid") {
  365. transport_protocol = cricket::ICEPROTO_HYBRID;
  366. } else {
  367. Print("Invalid transport protocol. Must be ice, gice, or hybrid.\n");
  368. return 1;
  369. }
  370. cricket::SecurePolicy sdes_policy, dtls_policy;
  371. if (!GetSecurePolicy(sdes, &sdes_policy)) {
  372. Print("Invalid SDES policy. Must be enable, disable, or require.\n");
  373. return 1;
  374. }
  375. if (!GetSecurePolicy(dtls, &dtls_policy)) {
  376. Print("Invalid DTLS policy. Must be enable, disable, or require.\n");
  377. return 1;
  378. }
  379. if (dtls_policy != cricket::SEC_DISABLED) {
  380. ssl_identity = talk_base::SSLIdentity::Generate(jid.Str());
  381. if (!ssl_identity) {
  382. Print("Failed to generate identity for DTLS.\n");
  383. return 1;
  384. }
  385. }
  386. #ifdef ANDROID
  387. InitAndroidMediaEngineFactory(AndroidMediaEngineFactory);
  388. #endif
  389. #if WIN32
  390. // Need to pump messages on our main thread on Windows.
  391. talk_base::Win32Thread w32_thread;
  392. talk_base::ThreadManager::Instance()->SetCurrentThread(&w32_thread);
  393. #endif
  394. talk_base::Thread* main_thread = talk_base::Thread::Current();
  395. #ifdef OSX
  396. talk_base::MacCocoaSocketServer ss;
  397. talk_base::SocketServerScope ss_scope(&ss);
  398. #endif
  399. buzz::XmppPump pump;
  400. CallClient *client = new CallClient(pump.client(), caps_node, caps_ver);
  401. if (FLAG_voiceinput || FLAG_voiceoutput ||
  402. FLAG_videoinput || FLAG_videooutput) {
  403. // If any dump file is specified, we use a FileMediaEngine.
  404. cricket::MediaEngineInterface* engine =
  405. MediaEngineFactory::CreateFileMediaEngine(
  406. FLAG_voiceinput, FLAG_voiceoutput,
  407. FLAG_videoinput, FLAG_videooutput);
  408. client->SetMediaEngine(engine);
  409. }
  410. Console *console = new Console(main_thread, client);
  411. client->SetConsole(console);
  412. client->SetAutoAccept(auto_accept);
  413. client->SetPmucDomain(pmuc_domain);
  414. client->SetPortAllocatorFlags(portallocator_flags);
  415. client->SetAllowLocalIps(true);
  416. client->SetSignalingProtocol(signaling_protocol);
  417. client->SetTransportProtocol(transport_protocol);
  418. client->SetSecurePolicy(sdes_policy, dtls_policy);
  419. client->SetSslIdentity(ssl_identity);
  420. client->SetRender(render);
  421. client->SetDataChannelEnabled(data_channel_enabled);
  422. client->SetMultiSessionEnabled(multisession_enabled);
  423. console->Start();
  424. if (debug) {
  425. pump.client()->SignalLogInput.connect(&debug_log_, &DebugLog::Input);
  426. pump.client()->SignalLogOutput.connect(&debug_log_, &DebugLog::Output);
  427. }
  428. Print(("Logging in to " + server + " as " + jid.Str() + "\n").c_str());
  429. pump.DoLogin(xcs, new buzz::XmppSocket(buzz::TLS_REQUIRED), new XmppAuth());
  430. main_thread->Run();
  431. pump.DoDisconnect();
  432. console->Stop();
  433. delete console;
  434. delete client;
  435. return 0;
  436. }