audio_rtp_factory.h 6.06 KB
Newer Older
1
/*
2
 *  Copyright (C) 2004, 2005, 2006, 2008, 2009, 2010, 2011 Savoir-Faire Linux Inc.
3
 *  Author: Pierre-Luc Bacon <pierre-luc.bacon@savoirfairelinux.com>
4
 *  Author: Alexandre Savard <alexandre.savard@savoirfairelinux.com>
Julien Bonjean's avatar
Julien Bonjean committed
5
 *
6
7
8
9
10
11
12
13
 *  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
 *  the Free Software Foundation; either version 3 of the License, or
 *  (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.
Julien Bonjean's avatar
Julien Bonjean committed
14
 *
15
16
17
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19
20
21
22
23
24
25
26
27
28
 *
 *  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.
29
30
 */

31
32
#ifndef __AUDIO_RTP_FACTORY_H__
#define __AUDIO_RTP_FACTORY_H__
33

34
#include <ccrtp/CryptoContext.h>
35
36
#include <stdexcept>
#include <tr1/array>
37
#include "cc_thread.h"
38
#include "audio_rtp_session.h"
39
#include "audio_srtp_session.h"
40
#include "noncopyable.h"
41

42
#include "sip/sdes_negotiator.h"
43
44

class SdesNegotiator;
45
class SIPCall;
46

47
namespace sfl {
48

49
#if HAVE_ZRTP
Julien Bonjean's avatar
Julien Bonjean committed
50
class AudioZrtpSession;
51
#endif
52
class AudioCodec;
53

54
class UnsupportedRtpSessionType : public std::logic_error {
Julien Bonjean's avatar
Julien Bonjean committed
55
    public:
56
        UnsupportedRtpSessionType(const std::string& msg = "") : std::logic_error(msg) {}
Julien Bonjean's avatar
Julien Bonjean committed
57
58
};

59
class AudioRtpFactoryException : public std::logic_error {
Julien Bonjean's avatar
Julien Bonjean committed
60
    public:
61
        AudioRtpFactoryException(const std::string& msg = "") : std::logic_error(msg) {}
Julien Bonjean's avatar
Julien Bonjean committed
62
};
63

64
class AudioRtpFactory {
Julien Bonjean's avatar
Julien Bonjean committed
65
    public:
66
        AudioRtpFactory(SIPCall *ca);
67
68
        ~AudioRtpFactory();

69
        void initConfig();
Julien Bonjean's avatar
Julien Bonjean committed
70
71
72
73
74

        /**
         * 	Lazy instantiation method. Create a new RTP session of a given
         * type according to the content of the configuration file.
         * @param ca A pointer on a SIP call
75
         * @return A new AudioRtpSession object
Julien Bonjean's avatar
Julien Bonjean committed
76
         */
77
        void initSession();
Julien Bonjean's avatar
Julien Bonjean committed
78
79
80

        /**
         * Start the audio rtp thread of the type specified in the configuration
81
         * file. initAudioSymmetricRtpSession must have been called prior to that.
Julien Bonjean's avatar
Julien Bonjean committed
82
83
         * @param None
         */
84
        void start(AudioCodec*);
Julien Bonjean's avatar
Julien Bonjean committed
85
86
87

        /**
         * Stop the audio rtp thread of the type specified in the configuration
88
         * file. initAudioSymmetricRtpSession must have been called prior to that.
Julien Bonjean's avatar
Julien Bonjean committed
89
90
91
92
         * @param None
         */
        void stop();

93
94
95
96
97
98
99
100
        /**
         * Return the RTP payload currently used for this session
         */
        int getSessionMedia();

        /**
         * Dynamically update session media
         */
101
        void updateSessionMedia(AudioCodec *);
102

Julien Bonjean's avatar
Julien Bonjean committed
103
104
105
106
        /**
         * Update current RTP destination address with one stored in call
         * @param None
         */
107
        void updateDestinationIpAddress();
Julien Bonjean's avatar
Julien Bonjean committed
108

109
        bool isSdesEnabled() const {
110
            return srtpEnabled_ and keyExchangeProtocol_ == SDES;
Julien Bonjean's avatar
Julien Bonjean committed
111
112
113
114
115
        }

        /**
         * Manually set the srtpEnable option (usefull for RTP fallback)
         */
116
        void setSrtpEnabled(bool enable) {
117
            srtpEnabled_ = enable;
Julien Bonjean's avatar
Julien Bonjean committed
118
119
        }

120
#if HAVE_ZRTP
Julien Bonjean's avatar
Julien Bonjean committed
121
122
123
124
125
        /**
         * Get the current AudioZrtpSession. Throws an AudioRtpFactoryException
         * if the current rtp thread is null, or if it's not of the correct type.
         * @return The current AudioZrtpSession thread.
         */
126
127
        sfl::AudioZrtpSession* getAudioZrtpSession();
#endif
Julien Bonjean's avatar
Julien Bonjean committed
128

129
        void initLocalCryptoInfo();
130
        void initLocalCryptoInfoOnOffHold();
131

Julien Bonjean's avatar
Julien Bonjean committed
132
133
134
135
        /**
         * Set remote cryptographic info. Should be called after negotiation in SDP
         * offer/answer session.
         */
136
        void setRemoteCryptoInfo(sfl::SdesNegotiator& nego);
Julien Bonjean's avatar
Julien Bonjean committed
137

138
139
        void setDtmfPayloadType(unsigned int);

Julien Bonjean's avatar
Julien Bonjean committed
140
141
142
143
144
145
        /**
         * Send DTMF over RTP (RFC2833). The timestamp and sequence number must be
         * incremented as if it was microphone audio. This function change the payload type of the rtp session,
         * send the appropriate DTMF digit using this payload, discard coresponding data from mainbuffer and get
         * back the codec payload for further audio processing.
         */
146
        void sendDtmfDigit(int digit);
Julien Bonjean's avatar
Julien Bonjean committed
147

148
149
150
151
        void saveLocalContext();

        void restoreLocalContext();

Julien Bonjean's avatar
Julien Bonjean committed
152
    private:
153
        NON_COPYABLE(AudioRtpFactory);
154
        enum KeyExchangeProtocol { NONE, SDES, ZRTP };
155
156
        AudioRtpSession *rtpSession_;
        ost::Mutex audioRtpThreadMutex_;
Julien Bonjean's avatar
Julien Bonjean committed
157

158
        // Field used when initializing audio rtp session
Julien Bonjean's avatar
Julien Bonjean committed
159
        // May be set manually or from config using initAudioRtpConfig
160
        bool srtpEnabled_;
Julien Bonjean's avatar
Julien Bonjean committed
161
162
163

        // Field used when initializinga udio rtp session
        // May be set manually or from config using initAudioRtpConfig
164
        bool helloHashEnabled_;
165

166
        /** local master key for outgoing packet encryption **/
167
        std::vector<uint8> cachedLocalMasterKey_;
168

169
        /** local master salt for outgoing packet encryption **/
170
        std::vector<uint8> cachedLocalMasterSalt_;
171

172
        /** remote master key for incoming packet decryption **/
173
        std::vector<uint8> cachedRemoteMasterKey_;
174

175
        /** remote master salt for incoming packet decryption **/
176
        std::vector<uint8> cachedRemoteMasterSalt_;
177
178
179
180

        /** Used to make sure remote crypto context not initialized twice. */
        bool remoteOfferIsSet_;

181
        SIPCall *ca_;
182
        KeyExchangeProtocol keyExchangeProtocol_;
183
};
184
185
}
#endif // __AUDIO_RTP_FACTORY_H__