sipcall.h 6.93 KB
Newer Older
yanmorin's avatar
 
yanmorin committed
1
/*
Adrien Béraud's avatar
Adrien Béraud committed
2
 *  Copyright (C) 2004-2015 Savoir-Faire Linux Inc.
3
 *  Author: Alexandre Bourget <alexandre.bourget@savoirfairelinux.com>
yanmorin's avatar
 
yanmorin committed
4 5
 *  Author: Yan Morin <yan.morin@savoirfairelinux.com>
 *  Author : Laurielle Lea <laurielle.lea@savoirfairelinux.com>
Guillaume Roguez's avatar
Guillaume Roguez committed
6
 *  Author : Guillaume Roguez <guillaume.roguez@savoirfairelinux.com>
jpbl's avatar
jpbl committed
7
 *
yanmorin's avatar
 
yanmorin committed
8 9
 *  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
10
 *  the Free Software Foundation; either version 3 of the License, or
yanmorin's avatar
 
yanmorin committed
11
 *  (at your option) any later version.
jpbl's avatar
jpbl committed
12
 *
yanmorin's avatar
 
yanmorin committed
13 14 15 16
 *  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.
jpbl's avatar
jpbl committed
17
 *
yanmorin's avatar
 
yanmorin committed
18 19
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
20
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
21 22 23 24 25 26 27 28 29 30 31
 *
 *  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
32
 */
Guillaume Roguez's avatar
Guillaume Roguez committed
33

34 35
#ifndef __SIPCALL_H__
#define __SIPCALL_H__
jpbl's avatar
jpbl committed
36

37 38 39 40
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

jpbl's avatar
jpbl committed
41
#include "call.h"
Guillaume Roguez's avatar
Guillaume Roguez committed
42
#include "sip_utils.h"
43

Adrien Béraud's avatar
Adrien Béraud committed
44
#ifdef RING_VIDEO
Guillaume Roguez's avatar
Guillaume Roguez committed
45
#include "media/video/video_rtp_session.h"
46 47
#endif

48
#include "noncopyable.h"
49

50 51
#include "pjsip/sip_config.h"

52 53
#include <memory>

54 55
struct pjsip_evsub;
struct pjsip_inv_session;
56
struct pjmedia_sdp_session;
57

Guillaume Roguez's avatar
Guillaume Roguez committed
58 59
namespace ring {

60
class Sdp;
61
class SIPAccountBase;
Adrien Béraud's avatar
Adrien Béraud committed
62
class SipTransport;
Guillaume Roguez's avatar
Guillaume Roguez committed
63
class AudioRtpSession;
64

Stepan Salenikovich's avatar
Stepan Salenikovich committed
65 66 67 68
namespace upnp {
class Controller;
}

yanmorin's avatar
 
yanmorin committed
69
/**
70
 * @file sipcall.h
Julien Bonjean's avatar
Julien Bonjean committed
71
 * @brief SIPCall are SIP implementation of a normal Call
yanmorin's avatar
 
yanmorin committed
72
 */
Guillaume Roguez's avatar
Guillaume Roguez committed
73 74
class SIPCall : public Call
{
Julien Bonjean's avatar
Julien Bonjean committed
75
    public:
Guillaume Roguez's avatar
Guillaume Roguez committed
76
        static const char* const LINK_TYPE;
Julien Bonjean's avatar
Julien Bonjean committed
77

Guillaume Roguez's avatar
Guillaume Roguez committed
78
    protected:
Julien Bonjean's avatar
Julien Bonjean committed
79
        /**
Guillaume Roguez's avatar
Guillaume Roguez committed
80
         * Constructor (protected)
Julien Bonjean's avatar
Julien Bonjean committed
81 82 83 84
         * @param id	The call identifier
         * @param type  The type of the call. Could be Incoming
         *						 Outgoing
         */
85
        SIPCall(SIPAccountBase& account, const std::string& id, Call::CallType type);
Julien Bonjean's avatar
Julien Bonjean committed
86

Guillaume Roguez's avatar
Guillaume Roguez committed
87
    public:
Julien Bonjean's avatar
Julien Bonjean committed
88 89 90
        /**
         * Destructor
         */
91
        ~SIPCall();
Julien Bonjean's avatar
Julien Bonjean committed
92

93
        /**
94
         * Return the SDP's manager of this call
95
         */
96 97
        Sdp& getSDP() {
            return *sdp_;
Julien Bonjean's avatar
Julien Bonjean committed
98 99
        }

100 101 102 103
        const char* getLinkType() const override {
            return LINK_TYPE;
        }

104
        /**
Guillaume Roguez's avatar
Guillaume Roguez committed
105
         * Returns a pointer to the AudioRtpSession object
106
         */
Guillaume Roguez's avatar
Guillaume Roguez committed
107
        AudioRtpSession& getAVFormatRTP() const {
108 109
            return *avformatrtp_;
        }
Julien Bonjean's avatar
Julien Bonjean committed
110

Adrien Béraud's avatar
Adrien Béraud committed
111
#ifdef RING_VIDEO
112 113 114
        /**
         * Returns a pointer to the VideoRtp object
         */
Guillaume Roguez's avatar
Guillaume Roguez committed
115
        video::VideoRtpSession& getVideoRtp () {
116
            return videortp_;
117
        }
118
#endif
119

120 121 122
        /**
         * The invite session to be reused in case of transfer
         */
123 124 125 126 127
        struct InvSessionDeleter {
                void operator()(pjsip_inv_session*) const noexcept;
        };

        std::unique_ptr<pjsip_inv_session, InvSessionDeleter> inv;
128

Adrien Béraud's avatar
Adrien Béraud committed
129 130 131 132 133 134
        void setSecure(bool sec);

        bool isSecure() const {
            return srtpEnabled_;
        }

135 136
        void setCallMediaLocal(const pj_sockaddr& localIP);

137 138
        void generateMediaPorts();

139
        void setContactHeader(pj_str_t *contact);
140

141
        void setTransport(const std::shared_ptr<SipTransport>& t);
Adrien Béraud's avatar
Adrien Béraud committed
142 143 144 145 146

        inline const std::shared_ptr<SipTransport>& getTransport() {
            return transport_;
        }

147 148
        void sendSIPInfo(const char *const body, const char *const subtype);

149
        void answer() override;
150

151
        void hangup(int reason) override;
152

153
        void refuse() override;
154

155
        void transfer(const std::string& to) override;
156

157
        bool attendedTransfer(const std::string& to) override;
158

159
        bool onhold() override;
160

161
        bool offhold() override;
162

163
        void switchInput(const std::string& resource) override;
164

165
        void peerHungup() override;
166

167
        void carryingDTMFdigits(char code) override;
168

169
#if HAVE_INSTANT_MESSAGING
170
        void sendTextMessage(const std::map<std::string, std::string>& messages,
171
                             const std::string& from) override;
172 173
#endif

174
        SIPAccountBase& getSIPAccount() const;
175 176 177

        void updateSDPFromSTUN();

Adrien Béraud's avatar
Adrien Béraud committed
178 179 180 181 182 183
        /**
         * Tell the user that the call is ringing
         * @param
         */
        void onPeerRinging();

184 185 186 187 188 189 190
        /**
         * Tell the user that the call was answered
         * @param
         */
        void onAnswered();

        /**
Guillaume Roguez's avatar
Guillaume Roguez committed
191 192
         * To call in case of server/internal error
         * @param cause Optionnal error code
193
         */
Guillaume Roguez's avatar
Guillaume Roguez committed
194
        void onFailure(signed cause=0);
195 196 197 198 199 200 201

        /**
         * Peer close the connection
         * @param
         */
        void onClosed();

202 203 204 205
        void setupLocalSDPFromIce();

        bool startIce();

206
        void startAllMedia();
207

Guillaume Roguez's avatar
Guillaume Roguez committed
208 209
        void onMediaUpdate();

210 211
        void onReceiveOffer(const pjmedia_sdp_session *offer);

Stepan Salenikovich's avatar
Stepan Salenikovich committed
212 213
        void openPortsUPnP();

Alexandre Lision's avatar
Alexandre Lision committed
214
        void muteMedia(const std::string& mediaType, bool isMuted) override;
Éloi Bail's avatar
Éloi Bail committed
215

216 217 218
        void restartMediaSender() override;

        bool useVideoCodec(const AccountVideoCodecInfo* codec) const override;
Éloi Bail's avatar
Éloi Bail committed
219

Alexandre Lision's avatar
Alexandre Lision committed
220
        virtual std::map<std::string, std::string> getDetails() const override;
221

222 223
        bool initIceTransport(bool master, unsigned channel_num=4) override;

224
        void terminateSipSession(int status);
Julien Bonjean's avatar
Julien Bonjean committed
225
    private:
Guillaume Roguez's avatar
Guillaume Roguez committed
226
        NON_COPYABLE(SIPCall);
227

Guillaume Roguez's avatar
Guillaume Roguez committed
228
        void stopAllMedia();
229

230 231 232 233 234
        /**
         * Transfer method used for both type of transfer
         */
        bool transferCommon(pj_str_t *dst);

235
        bool internalOffHold(const std::function<void()> &SDPUpdateFunc);
236

237 238
        int SIPSessionReinvite();

Guillaume Roguez's avatar
Guillaume Roguez committed
239
        std::vector<IceCandidate> getAllRemoteCandidates();
240

Guillaume Roguez's avatar
Guillaume Roguez committed
241
        std::unique_ptr<AudioRtpSession> avformatrtp_;
Julien Bonjean's avatar
Julien Bonjean committed
242

Adrien Béraud's avatar
Adrien Béraud committed
243
#ifdef RING_VIDEO
244 245 246
        /**
         * Video Rtp Session factory
         */
Guillaume Roguez's avatar
Guillaume Roguez committed
247
        video::VideoRtpSession videortp_;
248 249

        std::string videoInput_;
250
#endif
251

Adrien Béraud's avatar
Adrien Béraud committed
252 253
        bool srtpEnabled_ {false};

Adrien Béraud's avatar
Adrien Béraud committed
254 255 256 257 258 259 260
        /**
         * Hold the transport used for SIP communication.
         * Will be different from the account registration transport for
         * non-IP2IP calls.
         */
        std::shared_ptr<SipTransport> transport_ {};

261
        /**
262
         * The SDP session
263
         */
264
        std::unique_ptr<Sdp> sdp_;
265
        bool peerHolding_ {false};
266

Adrien Béraud's avatar
Adrien Béraud committed
267 268
        char contactBuffer_[PJSIP_MAX_URL_SIZE] {};
        pj_str_t contactHeader_ {contactBuffer_, 0};
Stepan Salenikovich's avatar
Stepan Salenikovich committed
269 270

        std::unique_ptr<ring::upnp::Controller> upnp_;
jpbl's avatar
jpbl committed
271 272
};

Guillaume Roguez's avatar
Guillaume Roguez committed
273 274
} // namespace ring

275
#endif // __SIPCALL_H__