~ubuntu-branches/ubuntu/wily/sflphone/wily

« back to all changes in this revision

Viewing changes to daemon/libs/pjproject-2.2.1/pjsip-apps/src/samples/pjsua2_demo.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2015-01-07 14:51:16 UTC
  • mfrom: (4.3.5 sid)
  • Revision ID: package-import@ubuntu.com-20150107145116-yxnafinf4lrdvrmx
Tags: 1.4.1-0.1ubuntu1
* Merge with Debian, remaining changes:
 - Drop soprano, nepomuk build-dep
* Drop ubuntu patches, now upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Id: pjsua2_demo.cpp 4790 2014-03-11 07:03:22Z riza $ */
 
2
/*
 
3
 * Copyright (C) 2008-2013 Teluu Inc. (http://www.teluu.com)
 
4
 *
 
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.
 
9
 *
 
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.
 
14
 *
 
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
 
18
 */
 
19
#include <pjsua2.hpp>
 
20
#include <iostream>
 
21
#include <memory>
 
22
#include <pj/file_access.h>
 
23
 
 
24
#define THIS_FILE       "pjsua2_demo.cpp"
 
25
 
 
26
using namespace pj;
 
27
 
 
28
class MyAccount;
 
29
 
 
30
class MyCall : public Call
 
31
{
 
32
private:
 
33
    MyAccount *myAcc;
 
34
 
 
35
public:
 
36
    MyCall(Account &acc, int call_id = PJSUA_INVALID_ID)
 
37
    : Call(acc, call_id)
 
38
    {
 
39
        myAcc = (MyAccount *)&acc;
 
40
    }
 
41
    
 
42
    virtual void onCallState(OnCallStateParam &prm);
 
43
};
 
44
 
 
45
class MyAccount : public Account
 
46
{
 
47
public:
 
48
    std::vector<Call *> calls;
 
49
    
 
50
public:
 
51
    MyAccount()
 
52
    {}
 
53
 
 
54
    ~MyAccount()
 
55
    {
 
56
        std::cout << "*** Account is being deleted: No of calls="
 
57
                  << calls.size() << std::endl;
 
58
    }
 
59
    
 
60
    void removeCall(Call *call)
 
61
    {
 
62
        for (std::vector<Call *>::iterator it = calls.begin();
 
63
             it != calls.end(); ++it)
 
64
        {
 
65
            if (*it == call) {
 
66
                calls.erase(it);
 
67
                break;
 
68
            }
 
69
        }
 
70
    }
 
71
 
 
72
    virtual void onRegState(OnRegStateParam &prm)
 
73
    {
 
74
        AccountInfo ai = getInfo();
 
75
        std::cout << (ai.regIsActive? "*** Register: code=" : "*** Unregister: code=")
 
76
                  << prm.code << std::endl;
 
77
    }
 
78
    
 
79
    virtual void onIncomingCall(OnIncomingCallParam &iprm)
 
80
    {
 
81
        Call *call = new MyCall(*this, iprm.callId);
 
82
        CallInfo ci = call->getInfo();
 
83
        CallOpParam prm;
 
84
        
 
85
        std::cout << "*** Incoming Call: " <<  ci.remoteUri << " ["
 
86
                  << ci.stateText << "]" << std::endl;
 
87
        
 
88
        calls.push_back(call);
 
89
        prm.statusCode = (pjsip_status_code)200;
 
90
        call->answer(prm);
 
91
    }
 
92
};
 
93
 
 
94
void MyCall::onCallState(OnCallStateParam &prm)
 
95
{
 
96
    PJ_UNUSED_ARG(prm);
 
97
 
 
98
    CallInfo ci = getInfo();
 
99
    std::cout << "*** Call: " <<  ci.remoteUri << " [" << ci.stateText
 
100
              << "]" << std::endl;
 
101
    
 
102
    if (ci.state == PJSIP_INV_STATE_DISCONNECTED) {
 
103
        myAcc->removeCall(this);
 
104
        /* Delete the call */
 
105
        delete this;
 
106
    }
 
107
}
 
108
 
 
109
static void mainProg1(Endpoint &ep) throw(Error)
 
110
{
 
111
    // Init library
 
112
    EpConfig ep_cfg;
 
113
    ep_cfg.logConfig.level = 4;
 
114
    ep.libInit( ep_cfg );
 
115
 
 
116
    // Transport
 
117
    TransportConfig tcfg;
 
118
    tcfg.port = 5060;
 
119
    ep.transportCreate(PJSIP_TRANSPORT_UDP, tcfg);
 
120
 
 
121
    // Start library
 
122
    ep.libStart();
 
123
    std::cout << "*** PJSUA2 STARTED ***" << std::endl;
 
124
 
 
125
    // Add account
 
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);
 
133
    
 
134
    pj_thread_sleep(2000);
 
135
    
 
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);
 
143
    
 
144
    // Hangup all calls
 
145
    pj_thread_sleep(8000);
 
146
    ep.hangupAllCalls();
 
147
    pj_thread_sleep(4000);
 
148
    
 
149
    // Destroy library
 
150
    std::cout << "*** PJSUA2 SHUTTING DOWN ***" << std::endl;
 
151
}
 
152
 
 
153
static void mainProg2() throw(Error)
 
154
{
 
155
    string json_str;
 
156
    {
 
157
        EpConfig epCfg;
 
158
        JsonDocument jDoc;
 
159
 
 
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";
 
165
 
 
166
        jDoc.writeObject(epCfg);
 
167
        json_str = jDoc.saveString();
 
168
        std::cout << json_str << std::endl << std::endl;
 
169
    }
 
170
 
 
171
    {
 
172
        EpConfig epCfg;
 
173
        JsonDocument rDoc;
 
174
        string output;
 
175
 
 
176
        rDoc.loadString(json_str);
 
177
        rDoc.readObject(epCfg);
 
178
 
 
179
        JsonDocument wDoc;
 
180
 
 
181
        wDoc.writeObject(epCfg);
 
182
        json_str = wDoc.saveString();
 
183
        std::cout << json_str << std::endl << std::endl;
 
184
 
 
185
        wDoc.saveFile("jsontest.js");
 
186
    }
 
187
 
 
188
    {
 
189
        EpConfig epCfg;
 
190
        JsonDocument rDoc;
 
191
 
 
192
        rDoc.loadFile("jsontest.js");
 
193
        rDoc.readObject(epCfg);
 
194
        pj_file_delete("jsontest.js");
 
195
    }
 
196
}
 
197
 
 
198
 
 
199
static void mainProg3(Endpoint &ep) throw(Error)
 
200
{
 
201
    const char *paths[] = { "../../../../tests/pjsua/wavs/input.16.wav",
 
202
                            "../../tests/pjsua/wavs/input.16.wav",
 
203
                            "input.16.wav"};
 
204
    unsigned i;
 
205
    const char *filename = NULL;
 
206
 
 
207
    // Init library
 
208
    EpConfig ep_cfg;
 
209
    ep.libInit( ep_cfg );
 
210
 
 
211
    for (i=0; i<PJ_ARRAY_SIZE(paths); ++i) {
 
212
       if (pj_file_exists(paths[i])) {
 
213
          filename = paths[i];
 
214
          break;
 
215
       }
 
216
    }
 
217
 
 
218
    if (!filename) {
 
219
        PJSUA2_RAISE_ERROR3(PJ_ENOTFOUND, "mainProg3()",
 
220
                           "Could not locate input.16.wav");
 
221
    }
 
222
 
 
223
    // Start library
 
224
    ep.libStart();
 
225
    std::cout << "*** PJSUA2 STARTED ***" << std::endl;
 
226
 
 
227
    // Create player and recorder
 
228
    {
 
229
        AudioMediaPlayer amp;
 
230
        amp.createPlayer(filename);
 
231
 
 
232
        AudioMediaRecorder amr;
 
233
        amr.createRecorder("recorder_test_output.wav");
 
234
 
 
235
        amp.startTransmit(ep.audDevManager().getPlaybackDevMedia());
 
236
        amp.startTransmit(amr);
 
237
 
 
238
        pj_thread_sleep(5000);
 
239
    }
 
240
}
 
241
 
 
242
 
 
243
static void mainProg() throw(Error)
 
244
{
 
245
    string json_str;
 
246
 
 
247
    {
 
248
        JsonDocument jdoc;
 
249
        AccountConfig accCfg;
 
250
 
 
251
        accCfg.idUri = "\"Just Test\" <sip:test@pjsip.org>";
 
252
        accCfg.regConfig.registrarUri = "sip:pjsip.org";
 
253
        SipHeader h;
 
254
        h.hName = "X-Header";
 
255
        h.hValue = "User header";
 
256
        accCfg.regConfig.headers.push_back(h);
 
257
 
 
258
        accCfg.sipConfig.proxies.push_back("<sip:sip.pjsip.org;transport=tcp>");
 
259
        accCfg.sipConfig.proxies.push_back("<sip:sip.pjsip.org;transport=tls>");
 
260
 
 
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);
 
264
 
 
265
        AuthCredInfo aci;
 
266
        aci.scheme = "digest";
 
267
        aci.username = "test";
 
268
        aci.data = "passwd";
 
269
        aci.realm = "*";
 
270
        accCfg.sipConfig.authCreds.push_back(aci);
 
271
 
 
272
        jdoc.writeObject(accCfg);
 
273
        json_str = jdoc.saveString();
 
274
        std::cout << "Original:" << std::endl;
 
275
        std::cout << json_str << std::endl << std::endl;
 
276
    }
 
277
 
 
278
    {
 
279
        JsonDocument rdoc;
 
280
 
 
281
        rdoc.loadString(json_str);
 
282
        AccountConfig accCfg;
 
283
        rdoc.readObject(accCfg);
 
284
 
 
285
        JsonDocument wdoc;
 
286
        wdoc.writeObject(accCfg);
 
287
        json_str = wdoc.saveString();
 
288
 
 
289
        std::cout << "Parsed:" << std::endl;
 
290
        std::cout << json_str << std::endl << std::endl;
 
291
    }
 
292
}
 
293
 
 
294
int main()
 
295
{
 
296
    int ret = 0;
 
297
    Endpoint ep;
 
298
 
 
299
    try {
 
300
        ep.libCreate();
 
301
 
 
302
        mainProg3(ep);
 
303
        ret = PJ_SUCCESS;
 
304
    } catch (Error & err) {
 
305
        std::cout << "Exception: " << err.info() << std::endl;
 
306
        ret = 1;
 
307
    }
 
308
 
 
309
    try {
 
310
        ep.libDestroy();
 
311
    } catch(Error &err) {
 
312
        std::cout << "Exception: " << err.info() << std::endl;
 
313
        ret = 1;
 
314
    }
 
315
 
 
316
    if (ret == PJ_SUCCESS) {
 
317
        std::cout << "Success" << std::endl;
 
318
    } else {
 
319
        std::cout << "Error Found" << std::endl;
 
320
    }
 
321
 
 
322
    return ret;
 
323
}
 
324
 
 
325