~ubuntu-branches/debian/experimental/kopete/experimental

« back to all changes in this revision

Viewing changes to protocols/jabber/libjingle/talk/examples/call/call_main.cc

  • Committer: Package Import Robot
  • Author(s): Maximiliano Curia
  • Date: 2015-02-24 11:32:57 UTC
  • mfrom: (1.1.41 vivid)
  • Revision ID: package-import@ubuntu.com-20150224113257-gnupg4v7lzz18ij0
Tags: 4:14.12.2-1
* New upstream release (14.12.2).
* Bump Standards-Version to 3.9.6, no changes needed.

Show diffs side-by-side

added added

removed removed

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