sipvoiplink.h 11.1 KB
Newer Older
jpbl's avatar
jpbl committed
1
/*
2
 *  Copyright (C) 2004, 2005, 2006, 2008, 2009, 2010, 2011 Savoir-Faire Linux Inc.
3
 *
4
 *  Author: Emmanuel Milou <emmanuel.milou@savoirfairelinux.com>
Emmanuel Milou's avatar
Emmanuel Milou committed
5
 *  Author: Yun Liu <yun.liu@savoirfairelinux.com>
6
 *  Author: Pierre-Luc Bacon <pierre-luc.bacon@savoirfairelinux.com>
7
 *  Author: Alexandre Savard <alexandre.savard@savoirfairelinux.com>
jpbl's avatar
jpbl committed
8
9
10
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
11
 *  the Free Software Foundation; either version 3 of the License, or
jpbl's avatar
jpbl committed
12
13
14
15
16
17
18
19
20
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
21
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22
23
24
25
26
27
28
29
30
31
32
 *
 *  Additional permission under GNU GPL version 3 section 7:
 *
 *  If you modify this program, or any covered work, by linking or
 *  combining it with the OpenSSL project's OpenSSL library (or a
 *  modified version of that library), containing parts covered by the
 *  terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
 *  grants you additional permission to convey the resulting work.
 *  Corresponding Source for a non-source form of such a combination
 *  shall include the source code for the parts of OpenSSL used as well
 *  as that of the covered work.
jpbl's avatar
jpbl committed
33
 */
34

yanmorin's avatar
   
yanmorin committed
35
36
#ifndef SIPVOIPLINK_H
#define SIPVOIPLINK_H
jpbl's avatar
jpbl committed
37

38
#include <map>
39

Emmanuel Milou's avatar
Emmanuel Milou committed
40
41
//////////////////////////////
/* PJSIP imports */
42
43
44
#include <pjsip.h>
#include <pjlib.h>
#include <pjsip_ua.h>
Emmanuel Milou's avatar
Emmanuel Milou committed
45
46
47
48
#include <pjlib-util.h>
#include <pjnath/stun_config.h>
///////////////////////////////

Rafaël Carré's avatar
Rafaël Carré committed
49
#include "sipaccount.h"
50
#include "voiplink.h"
51
52
53
54

namespace sfl {
    class InstantMessaging;
}
55

yanmorin's avatar
   
yanmorin committed
56
57
class EventThread;
class SIPCall;
Rafaël Carré's avatar
Rafaël Carré committed
58
class SIPAccount;
59

yanmorin's avatar
   
yanmorin committed
60
/**
61
 * @file sipvoiplink.h
Emmanuel Milou's avatar
Emmanuel Milou committed
62
63
64
 * @brief Specific VoIPLink for SIP (SIP core for incoming and outgoing events).
 *          This class is based on the singleton design pattern.
 *          One SIPVoIPLink can handle multiple SIP accounts, but all the SIP accounts have all the same SIPVoIPLink
yanmorin's avatar
   
yanmorin committed
65
 */
Alexandre Bourget's avatar
Alexandre Bourget committed
66

yanmorin's avatar
   
yanmorin committed
67
68
class SIPVoIPLink : public VoIPLink
{
69
70
	public:

71
72
73
74
		/**
		 * Destructor
		 */
		~SIPVoIPLink();
75

76
        /**
Emmanuel Milou's avatar
Emmanuel Milou committed
77
78
         * Singleton method. Enable to retrieve the unique static instance
         * @return SIPVoIPLink* A pointer on the object
79
         */
80
        static SIPVoIPLink* instance ();
81

Julien Bonjean's avatar
Julien Bonjean committed
82
83
        /**
         * Try to initiate the pjsip engine/thread and set config
84
         */
85
        virtual void init (void);
86

Emmanuel Milou's avatar
Emmanuel Milou committed
87
88
89
        /**
         * Shut the library and clean up
         */
90
        virtual void terminate (void);
91
92
93
94

        /**
         * Event listener. Each event send by the call manager is received and handled from here
         */
95
        virtual void getEvent (void);
96
97
98
99

        /**
         * Build and send SIP registration request
         */
100
        virtual void sendRegister (Account *a);
101
102
103
104

        /**
         * Build and send SIP unregistration request
         */
105
        virtual void sendUnregister (Account *a);
106
107
108
109
110
111
112

        /**
         * Place a new call
         * @param id  The call identifier
         * @param toUrl  The Sip address of the recipient of the call
         * @return Call* The current call
         */
113
        virtual Call* newOutgoingCall (const std::string& id, const std::string& toUrl);
114
115
116

        /**
         * Answer the call
117
         * @param c The call
118
         */
119
        virtual void answer (Call *c);
120
121
122
123
124

        /**
         * Hang up the call
         * @param id The call identifier
         */
125
        virtual void hangup (const std::string& id);
126

127
128
129
130
        /**
         * Hang up the call
         * @param id The call identifier
         */
131
        virtual void peerHungup (const std::string& id);
132

133
134
135
136
137
        /**
         * Put the call on hold
         * @param id The call identifier
         * @return bool True on success
         */
138
        virtual void onhold (const std::string& id);
139
140
141
142
143
144

        /**
         * Put the call off hold
         * @param id The call identifier
         * @return bool True on success
         */
145
        virtual void offhold (const std::string& id);
146
147
148
149
150
151

        /**
         * Transfer the call
         * @param id The call identifier
         * @param to The recipient of the transfer
         */
152
        virtual void transfer (const std::string& id, const std::string& to);
153

154
155
156
157
158
159
        /**
         * Attended transfer
         * @param The transfered call id
         * @param The target call id
         * @return True on success
         */
160
        virtual bool attendedTransfer(const std::string&, const std::string&);
161

162
163
164
165
166
        /**
         * Refuse the call
         * @param id The call identifier
         * @return bool True on success
         */
167
        virtual bool refuse (const std::string& id);
168
169

        /**
170
         * Send DTMF refering to account configuration
171
172
173
         * @param id The call identifier
         * @param code  The char code
         */
Rafaël Carré's avatar
Rafaël Carré committed
174
        virtual void carryingDTMFdigits (const std::string& id, char code);
175

176
177
178
179
180
        /**
         * Start a new SIP call using the IP2IP profile
         * @param The call id
         * @param The target sip uri
         */
181
        bool SIPNewIpToIpCall (const std::string& id, const std::string& to);
182

183
184
185
186
        /**
         * Tell the user that the call was answered
         * @param
         */
Julien Bonjean's avatar
Julien Bonjean committed
187
        void SIPCallAnswered (SIPCall *call, pjsip_rx_data *rdata);
188
189
190

        /**
         * Handling 5XX/6XX error
Julien Bonjean's avatar
Julien Bonjean committed
191
         * @param
192
         */
Julien Bonjean's avatar
Julien Bonjean committed
193
        void SIPCallServerFailure (SIPCall *call);
194
195
196
197
198

        /**
         * Peer close the connection
         * @param
         */
Julien Bonjean's avatar
Julien Bonjean committed
199
        void SIPCallClosed (SIPCall *call);
200

201
        pj_caching_pool *getMemoryPoolFactory();
202

Alexandre Savard's avatar
Alexandre Savard committed
203
        /**
204
205
206
         * SIPCall accessor
         * @param id  The call identifier
         * @return SIPCall*	  A pointer on SIPCall object
Alexandre Savard's avatar
Alexandre Savard committed
207
         */
208
        SIPCall* getSIPCall (const std::string& id);
Julien Bonjean's avatar
Julien Bonjean committed
209
210
211

        /**
         * Return the codec protocol used for this call
Rafaël Carré's avatar
Rafaël Carré committed
212
         * @param c The call identifier
Julien Bonjean's avatar
Julien Bonjean committed
213
         */
214
        std::string getCurrentCodecName(Call *c) const;
Julien Bonjean's avatar
Julien Bonjean committed
215

216
217
218
        /**
         * Retrive useragent name from account
         */
219
        std::string getUseragentName (SIPAccount *) const;
220

Julien Bonjean's avatar
Julien Bonjean committed
221
222
        /**
         * List all the interfaces on the system and return
223
224
225
226
227
228
         * a vector list containing their IPV4 address.
         * @param void
         * @return std::vector<std::string> A std::string vector
         * of IPV4 address available on all of the interfaces on
         * the system.
         */
229
        static std::vector<std::string> getAllIpInterface (void);
230

Julien Bonjean's avatar
Julien Bonjean committed
231
232
233
234
235
236
237
238
        /**
        * List all the interfaces on the system and return
        * a vector list containing their name (eth0, eth0:1 ...).
        * @param void
        * @return std::vector<std::string> A std::string vector
        * of interface name available on all of the interfaces on
        * the system.
        */
239
        static std::vector<std::string> getAllIpInterfaceByName (void);
240

Julien Bonjean's avatar
Julien Bonjean committed
241
        /**
242
243
244
245
246
247
248
         * List all the interfaces on the system and return
         * a vector list containing their name (eth0, eth0:1 ...).
         * @param void
         * @return std::vector<std::string> A std::string vector
         * of interface name available on all of the interfaces on
         * the system.
         */
249
        static std::string getInterfaceAddrFromName (const std::string &ifaceName);
250

Julien Bonjean's avatar
Julien Bonjean committed
251
252
253
254
        /**
         * Initialize the transport selector
         * @param transport		A transport associated with an account
         *
Rafaël Carré's avatar
Rafaël Carré committed
255
         * @return          	A pointer to the transport selector structure
Julien Bonjean's avatar
Julien Bonjean committed
256
         */
Rafaël Carré's avatar
Rafaël Carré committed
257
        pjsip_tpselector *initTransportSelector (pjsip_transport *, pj_pool_t *);
258

259
        /**
260
         * This function unset the transport for a given account.
Julien Bonjean's avatar
Julien Bonjean committed
261
         */
262
        void shutdownSipTransport (SIPAccount *account);
263

264
        /**
265
266
         * Send a SIP message to a call identified by its callid
         *
267
         * @param The InstantMessaging module which contains formating, parsing and sending method
268
269
270
271
         * @param The Id of the call to send the message to
         * @param The actual message to be transmitted
         * @param The sender of this message (could be another participant of a conference)
         */
Rafaël Carré's avatar
Rafaël Carré committed
272
        void sendTextMessage (sfl::InstantMessaging *module, const std::string& callID, const std::string& message, const std::string& from);
273

274
275
276
        /**
         * Create the default UDP transport according ot Ip2Ip profile settings
         */
277
        void createDefaultSipUdpTransport();
278

279
    private:
280
281
282
283
284
285
        /**
         * Start a SIP Call
         * @param call  The current call
         * @return true if all is correct
         */
        bool SIPStartCall (SIPCall* call);
Rafaël Carré's avatar
Rafaël Carré committed
286
287

        void dtmfSend (SIPCall *call, char code, DtmfType type);
288
289
290
291
292
293
294

        /* Assignment Operator */
        SIPVoIPLink& operator= (const SIPVoIPLink& rh);

	    /* Copy Constructor */
        SIPVoIPLink (const SIPVoIPLink& rh);

295
        SIPVoIPLink ();
Emmanuel Milou's avatar
Emmanuel Milou committed
296

297
298
299
        /**
         * Resolve public address for this account
         */
Rafaël Carré's avatar
Rafaël Carré committed
300
        pj_status_t stunServerResolve (SIPAccount *);
301

Julien Bonjean's avatar
Julien Bonjean committed
302
        /**
303
         * Create the default TLS listener.
Julien Bonjean's avatar
Julien Bonjean committed
304
         */
305
        void createTlsListener (SIPAccount*, pjsip_tpfactory **listener);
Julien Bonjean's avatar
Julien Bonjean committed
306
307
308
309

        /**
         * General Sip transport creation method according to the
         * transport type specified in account settings
310
         * @param account The account for which a transport must be created.
Julien Bonjean's avatar
Julien Bonjean committed
311
         */
312
        void createSipTransport (SIPAccount *account);
Julien Bonjean's avatar
Julien Bonjean committed
313
314
315

        /**
        * Create SIP UDP transport from account's setting
316
        * @param account The account for which a transport must be created.
Julien Bonjean's avatar
Julien Bonjean committed
317
        */
318
        void createUdpTransport (SIPAccount *account);
319

Julien Bonjean's avatar
Julien Bonjean committed
320
321
        /**
         * Create a TLS transport from the default TLS listener from
Rafaël Carré's avatar
Rafaël Carré committed
322
         * @param account The account for which a transport must be created.
323
         */
324
        void createTlsTransport (SIPAccount *, std::string remoteAddr);
Julien Bonjean's avatar
Julien Bonjean committed
325
326
327

        /**
         * Create a UDP transport using stun server to resove public address
Rafaël Carré's avatar
Rafaël Carré committed
328
         * @param account The account for which a transport must be created.
Julien Bonjean's avatar
Julien Bonjean committed
329
         */
330
        void createStunTransport (SIPAccount *account);
Julien Bonjean's avatar
Julien Bonjean committed
331

332
333
334
335
336
337
338
339
        /**
         * Get the correct address to use (ie advertised) from
         * a uri. The corresponding transport that should be used
         * with that uri will be discovered.
         *
         * @param uri The uri from which we want to discover the address to use
         * @param transport The transport to use to discover the address
         */
340
        void findLocalAddressFromUri (const std::string& uri, pjsip_transport *transport, std::string &address, std::string &port);
341

Julien Bonjean's avatar
Julien Bonjean committed
342
343
344
345
        /**
         * UDP Transports are stored in this map in order to retreive them in case
         * several accounts would share the same port number.
         */
346
        std::map<pj_uint16_t, pjsip_transport*> transportMap_;
Julien Bonjean's avatar
Julien Bonjean committed
347

348
349
350
        /**
         * Threading object
         */
351
        EventThread *evThread_;
Julien Bonjean's avatar
Julien Bonjean committed
352

353
        friend class SIPTest;
jpbl's avatar
jpbl committed
354
355
};

356

yanmorin's avatar
   
yanmorin committed
357
#endif