1
/* $Id: pjsua2_demo.cpp 4790 2014-03-11 07:03:22Z riza $ */
3
* Copyright (C) 2008-2013 Teluu Inc. (http://www.teluu.com)
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22
#include <pj/file_access.h>
24
#define THIS_FILE "pjsua2_demo.cpp"
30
class MyCall : public Call
36
MyCall(Account &acc, int call_id = PJSUA_INVALID_ID)
39
myAcc = (MyAccount *)&acc;
42
virtual void onCallState(OnCallStateParam &prm);
45
class MyAccount : public Account
48
std::vector<Call *> calls;
56
std::cout << "*** Account is being deleted: No of calls="
57
<< calls.size() << std::endl;
60
void removeCall(Call *call)
62
for (std::vector<Call *>::iterator it = calls.begin();
63
it != calls.end(); ++it)
72
virtual void onRegState(OnRegStateParam &prm)
74
AccountInfo ai = getInfo();
75
std::cout << (ai.regIsActive? "*** Register: code=" : "*** Unregister: code=")
76
<< prm.code << std::endl;
79
virtual void onIncomingCall(OnIncomingCallParam &iprm)
81
Call *call = new MyCall(*this, iprm.callId);
82
CallInfo ci = call->getInfo();
85
std::cout << "*** Incoming Call: " << ci.remoteUri << " ["
86
<< ci.stateText << "]" << std::endl;
88
calls.push_back(call);
89
prm.statusCode = (pjsip_status_code)200;
94
void MyCall::onCallState(OnCallStateParam &prm)
98
CallInfo ci = getInfo();
99
std::cout << "*** Call: " << ci.remoteUri << " [" << ci.stateText
102
if (ci.state == PJSIP_INV_STATE_DISCONNECTED) {
103
myAcc->removeCall(this);
104
/* Delete the call */
109
static void mainProg1(Endpoint &ep) throw(Error)
113
ep_cfg.logConfig.level = 4;
114
ep.libInit( ep_cfg );
117
TransportConfig tcfg;
119
ep.transportCreate(PJSIP_TRANSPORT_UDP, tcfg);
123
std::cout << "*** PJSUA2 STARTED ***" << std::endl;
126
AccountConfig acc_cfg;
127
acc_cfg.idUri = "sip:test1@pjsip.org";
128
acc_cfg.regConfig.registrarUri = "sip:pjsip.org";
129
acc_cfg.sipConfig.authCreds.push_back( AuthCredInfo("digest", "*",
130
"test1", 0, "test1") );
131
std::auto_ptr<MyAccount> acc(new MyAccount);
132
acc->create(acc_cfg);
134
pj_thread_sleep(2000);
136
// Make outgoing call
137
Call *call = new MyCall(*acc);
138
acc->calls.push_back(call);
139
CallOpParam prm(true);
140
prm.opt.audioCount = 1;
141
prm.opt.videoCount = 0;
142
call->makeCall("sip:test1@pjsip.org", prm);
145
pj_thread_sleep(8000);
147
pj_thread_sleep(4000);
150
std::cout << "*** PJSUA2 SHUTTING DOWN ***" << std::endl;
153
static void mainProg2() throw(Error)
160
epCfg.uaConfig.maxCalls = 61;
161
epCfg.uaConfig.userAgent = "Just JSON Test";
162
epCfg.uaConfig.stunServer.push_back("stun1.pjsip.org");
163
epCfg.uaConfig.stunServer.push_back("stun2.pjsip.org");
164
epCfg.logConfig.filename = "THE.LOG";
166
jDoc.writeObject(epCfg);
167
json_str = jDoc.saveString();
168
std::cout << json_str << std::endl << std::endl;
176
rDoc.loadString(json_str);
177
rDoc.readObject(epCfg);
181
wDoc.writeObject(epCfg);
182
json_str = wDoc.saveString();
183
std::cout << json_str << std::endl << std::endl;
185
wDoc.saveFile("jsontest.js");
192
rDoc.loadFile("jsontest.js");
193
rDoc.readObject(epCfg);
194
pj_file_delete("jsontest.js");
199
static void mainProg3(Endpoint &ep) throw(Error)
201
const char *paths[] = { "../../../../tests/pjsua/wavs/input.16.wav",
202
"../../tests/pjsua/wavs/input.16.wav",
205
const char *filename = NULL;
209
ep.libInit( ep_cfg );
211
for (i=0; i<PJ_ARRAY_SIZE(paths); ++i) {
212
if (pj_file_exists(paths[i])) {
219
PJSUA2_RAISE_ERROR3(PJ_ENOTFOUND, "mainProg3()",
220
"Could not locate input.16.wav");
225
std::cout << "*** PJSUA2 STARTED ***" << std::endl;
227
// Create player and recorder
229
AudioMediaPlayer amp;
230
amp.createPlayer(filename);
232
AudioMediaRecorder amr;
233
amr.createRecorder("recorder_test_output.wav");
235
amp.startTransmit(ep.audDevManager().getPlaybackDevMedia());
236
amp.startTransmit(amr);
238
pj_thread_sleep(5000);
243
static void mainProg() throw(Error)
249
AccountConfig accCfg;
251
accCfg.idUri = "\"Just Test\" <sip:test@pjsip.org>";
252
accCfg.regConfig.registrarUri = "sip:pjsip.org";
254
h.hName = "X-Header";
255
h.hValue = "User header";
256
accCfg.regConfig.headers.push_back(h);
258
accCfg.sipConfig.proxies.push_back("<sip:sip.pjsip.org;transport=tcp>");
259
accCfg.sipConfig.proxies.push_back("<sip:sip.pjsip.org;transport=tls>");
261
accCfg.mediaConfig.transportConfig.tlsConfig.ciphers.push_back(1);
262
accCfg.mediaConfig.transportConfig.tlsConfig.ciphers.push_back(2);
263
accCfg.mediaConfig.transportConfig.tlsConfig.ciphers.push_back(3);
266
aci.scheme = "digest";
267
aci.username = "test";
270
accCfg.sipConfig.authCreds.push_back(aci);
272
jdoc.writeObject(accCfg);
273
json_str = jdoc.saveString();
274
std::cout << "Original:" << std::endl;
275
std::cout << json_str << std::endl << std::endl;
281
rdoc.loadString(json_str);
282
AccountConfig accCfg;
283
rdoc.readObject(accCfg);
286
wdoc.writeObject(accCfg);
287
json_str = wdoc.saveString();
289
std::cout << "Parsed:" << std::endl;
290
std::cout << json_str << std::endl << std::endl;
304
} catch (Error & err) {
305
std::cout << "Exception: " << err.info() << std::endl;
311
} catch(Error &err) {
312
std::cout << "Exception: " << err.info() << std::endl;
316
if (ret == PJ_SUCCESS) {
317
std::cout << "Success" << std::endl;
319
std::cout << "Error Found" << std::endl;